Deserialize environment variables into structs
masterUse envy::from_env::<T>() to deserialize environment variables into a typesafe struct.
Key Behaviors:
- Naming Convention: Envy maps struct fields to environment variables using their names in all uppercase letters. For example, a field named
foo_barmaps to the environment variableFOO_BAR. - Optional Fields: Fields defined with the
Option<T>type will successfully deserialize even if the corresponding environment variable is missing. - Collections:
Vec<T>fields can be deserialized from comma-separated environment variable values. - Serde Integration: Since envy is built on
serde, you can use any Serde attributes, such as#[serde(default = "path")], to provide default values for missing environment variables.
use serde::Deserialize;
#[derive(Deserialize, Debug)]
struct Config {
foo: u16,
bar: bool,
baz: String,
boom: Option<u64>
}
fn main() {
match envy::from_env::<Config>() {
Ok(config) => println!("{:#?}", config),
Err(error) => panic!("{:#?}", error)
}
}