Expand description
🦀 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:#?}");Boolean parsing
Boolean parsing is case-insensitive.
If the truthy-falsy feature is enabled (default):
- Truthy values:
trueor its shorthandtyesor its shorthandyon1
- Falsy values:
falseor its shorthandfnoor its shorthandnoff0
If the truthy-falsy feature is disabled, only true and false are
considered valid booleans.
Enum
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
Structs
Represent an error that may arise when deserializing.
Functions
Deserialize an instance of T from the environment variables of the current process.
Deserialize an instance of T from the environment variables of the current process with the
specified prefix.
Deserialize an instance of T from an iterator of name-value String pairs.
Deserialize an instance of T from an iterator of name-value OsString pairs.