Crate envy

source
Expand description

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>,
}

match envy::from_env::<Config>() {
    Ok(config) => println!("{:#?}", config),
    Err(error) => eprintln!("{:#?}", error),
}

Special treatment is given to collections. For config fields that store a Vec of values, use an env var that uses a comma separated value.

All serde modifiers should work as is.

Enums with unit variants can be used as values:


#[derive(Deserialize, Debug, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum Size {
    Small,
    Medium,
    Large,
}

#[derive(Deserialize, Debug)]
struct Config {
    size: Size,
}

// set env var for size as `SIZE=medium`
match envy::from_env::<Config>() {
    Ok(config) => println!("{:#?}", config),
    Err(error) => eprintln!("{:#?}", 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 values
  • Produces a instance of Prefixed for prefixing env variable names

Type Aliases§

  • A type result type specific to envy::Errors