[−][src]Crate envy
Envy is a library for deserializing environment variables into typesafe structs
Examples
A typical usecase for envy is deserializing configuration store in an process' environment into a struct whose fields map to the names of env vars.
Serde makes it easy to provide a deserializable struct with its deriveable Deserialize procedural macro.
Simply ask for an instance of that struct from envy's from_env
function.
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) } }
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
use serde::Deserialize; #[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 prefix 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 stored in 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 |