Expand description
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
A type which filters env vars with a prefix for use as serde field inputs
Enums
Types of errors that may result from failed attempts
to deserialize a type from env vars
Functions
Deserializes a type based on information stored in env variables
Deserializes a type based on an iterable of
(String, String)
representing keys and valuesProduces a instance of
Prefixed
for prefixing env variable namesType Definitions
A type result type specific to
envy::Errors