de_env 1.0.0-rc

Deserialize environment variables through serde
Documentation
de_env-1.0.0-rc has been yanked.

🦀 de_env

Deserialize environment variables through serde.


You may be looking for:

Example

Assuming we have a LOG and PORT environment variable:

#[derive(serde::Deserialize, Debug)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
struct Config {
    log: String,
    port: u16
}

let config: Config = de_env::from_env().unwrap();

println!("{config:#?}");

Primitives

Strings & chars

The input is checked for UTF-8 validity and returned as is.

Numbers

If the input is valid Unicode, integers and floats are parsed with their respective FromStr implementations.

Booleans

Boolean parsing is case-insensitive.

If the truthy-falsy feature is enabled (default):

  • Truthy values:
    • true or its shorthand t
    • yes or its shorthand y
    • on
    • 1
  • Falsy values:
    • false or its shorthand f
    • no or its shorthand n
    • off
    • 0

If the truthy-falsy feature is disabled, only true and false are considered valid booleans.

Enums

Only unit variants can be deserialized.

Assuming we have a LEVEL environment variable set to HIGH, MEDIUM or LOW:

#[derive(serde::Deserialize, Debug)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
enum Level {
    High,
    Medium,
    Low
}

#[derive(serde::Deserialize, Debug)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
struct Pool {
    level: Level,
}

let pool: Pool = de_env::from_env().unwrap();

println!("{pool:#?}");

Unsupported types

  • Nested structs
  • Nested enums
  • Nested maps
  • Non-unit enum variants
  • Tuples
  • Sequences
  • Byte Arrays