Expand description
§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
FromStrimplementations) - Booleans (see boolean parsing)
§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.
§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.
Structs§
- Error
- Represent an error that may arise when deserializing.
Functions§
- from_
env - Deserialize an instance of
Tfrom the environment variables of the current process. - from_
env_ prefixed - Deserialize an instance of
Tfrom the environment variables of the current process with the specified prefix. - from_
iter - Deserialize an instance of
Tfrom an iterator of key-value tuple.