7 releases

0.1.6 Dec 18, 2024
0.1.5 Nov 24, 2024

#33 in #elm

Download history 300/week @ 2024-11-12 176/week @ 2024-11-19 59/week @ 2024-11-26 9/week @ 2024-12-03 25/week @ 2024-12-10 116/week @ 2024-12-17

141 downloads per month
Used in parenv

MIT license

16KB
314 lines

parenv

crates.io version crates.io license crates.io downloads docs.rs documentation

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.
  • To set a suffix value, set the attribute #[parenv(suffix = "_ARG")] on your struct.
use std::{net::SocketAddr, path::PathBuf};

use parenv::Environment;

#[derive(Debug, Environment)]
#[parenv(prefix = "ENV_", suffix = "_ARG")]
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

~225–670KB
~16K SLoC