#serde #env

de_env

Deserialize environment variables into a struct

1 stable release

1.0.0 May 7, 2022
1.0.0-rc.2 May 5, 2022
0.1.0 May 1, 2022

#1182 in Encoding

Download history 69/week @ 2023-10-17 53/week @ 2023-10-24 60/week @ 2023-10-31 52/week @ 2023-11-07 54/week @ 2023-11-14 54/week @ 2023-11-21 61/week @ 2023-11-28 30/week @ 2023-12-05 38/week @ 2023-12-12 38/week @ 2023-12-19 33/week @ 2023-12-26 16/week @ 2024-01-02 21/week @ 2024-01-09 27/week @ 2024-01-16 24/week @ 2024-01-23 91/week @ 2024-01-30

163 downloads per month

MIT license

25KB
559 lines

de_env helps you easily deserialize environment variables into a struct.

Example

Assuming we have a TIMEOUT, HOST and RETRY environment variable:

#[derive(serde::Deserialize, Debug)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
struct Config {
    timeout: u16,
    host: std::net::IpAddr,
    retry: bool,
}

let config: Config = de_env::from_env()?;

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

lib.rs:

de_env

Deserialize environment variables into a struct.


You may be looking for:

Example

Assuming we have a TIMEOUT and HOST environment variable:

#[derive(serde::Deserialize, Debug)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
struct Config {
    timeout: u16,
    host: std::net::IpAddr,
}

let config: Config = de_env::from_env()?;

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

Supported Primitives

  • String slices
  • Chars
  • Numbers (parsed with their respective FromStr implementations)
  • Booleans (see boolean parsing)

Boolean Parsing

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 LOG_LEVEL environment variable set to INFO or WARN:

#[derive(serde::Deserialize, Debug)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
enum Level {
    Info,
    Warn
}

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

let config: Config = de_env::from_env()?;

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

Unsupported Types

The goal of this crate is to deserialize environment variables into a struct, no other type is supported at top level. Custom types must be able to deserialize from supported primitives.

Dependencies

~110–355KB