Crate envy [−] [src]
envy is a library for deserializing env vars into typesafe structs
Examples
A typical use case for envy is deserializing configuration stored into an env into a struct whose fields map to the names of env vars.
Serde makes it easy to provide a deserializable struct with the
serde_derive crate. Simply ask for an instance of that
struct from envy's from_env
function.
#[macro_use] extern crate serde_derive; extern crate envy; #[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) } }
Special treatment is given to collections. For config fields that store a Vec
of values,
use and env var that uses a comma separated value
All serde modifier should work as is
If you wish to use enum types use the following
#[macro_use] extern crate serde_derive; extern crate envy; #[derive(Deserialize, Debug, PartialEq)] #[serde(untagged)] #[serde(field_identifier, rename_all = "lowercase")] pub enum Size { Small, Medium, Large } #[derive(Deserialize, Debug)] struct Config { size: Size, } fn main() { // set env var for size as `SIZE=medium` match envy::from_env::<Config>() { Ok(config) => println!("{:#?}", config) Err(error) => panic!("{:#?}", error) } }
Structs
Prefixed |
A type which filters env vars with a prefixed for use as serde field inputs |
Enums
Error |
Types of errors that may result from failed attempts to deserialize a type from env vars |
Functions
from_env |
Deserializes a type based on information based on env variables |
from_iter |
Deserializes a type based on an iterable of |
prefixed |
produces a instance of |
Type Definitions
Result |
A type result type specific to |