#env-var #variables #style #environment #error-reporting #macro-derive #clap

parenv

Environment variable parser with a clap style derive macro and elm style error reporting

4 releases

new 0.1.3 Nov 15, 2024
0.1.2 Nov 15, 2024
0.1.1 Nov 14, 2024
0.1.0 Nov 14, 2024

#884 in Rust patterns

Download history 315/week @ 2024-11-10

315 downloads per month

MIT license

4KB

parenv

Environment variable parser with a clap style derive macro and elm style error reporting.

Installation

cargo add parenv

Usage

Here are some important features you should know about.

  • parenv relies on the FromStr trait to parse environment variables into the specified type.
  • The documentation comment on each field is used as the description for the corresponding environment variable.
  • To make a field optional, wrap the type with an Option.
  • To set a prefix value, set the attribute #[parenv(prefix = "ENV_")] on your struct.
use std::{net::SocketAddr, path::PathBuf};

use parenv::Environment;

#[derive(Debug, Environment)]
#[parenv(prefix = "ENV_")]
struct Env {
    /// The cat
    cat: Option<u8>,
    /// The dog
    dog: SocketAddr,
    /// The file
    file: PathBuf,
}

fn main() {
    let env = Env::parse();
    dbg!(env.cat, env.dog, env.file);
}

Demo

asciicast

Dependencies

~4–10MB
~110K SLoC