Giter VIP home page Giter VIP logo

envy's Introduction

envy Github Actions Coverage Status Software License crates.io Latest API docs

deserialize environment variables into typesafe structs

๐Ÿ“ฆ install

Add the following to your Cargo.toml file.

[dependencies]
envy = "0.4"

๐Ÿคธ usage

A typical envy usage looks like the following. Assuming your rust program looks something like this...

๐Ÿ’ก These examples use Serde's derive feature

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

... export some environment variables

$ FOO=8080 BAR=true BAZ=hello yourapp

You should be able to access a completely typesafe config struct deserialized from env vars.

Envy assumes an env var exists for each struct field with a matching name in all uppercase letters. i.e. A struct field foo_bar would map to an env var named FOO_BAR.

Structs with Option type fields will successfully be deserialized when their associated env var is absent.

Envy also supports deserializing Vecs from comma separated env var values.

Because envy is built on top of serde, you can use all of serde's attributes to your advantage.

For instance let's say your app requires a field but would like a sensible default when one is not provided.

/// provides default value for zoom if ZOOM env var is not set
fn default_zoom() -> u16 {
  32
}

#[derive(Deserialize, Debug)]
struct Config {
  foo: u16,
  bar: bool,
  baz: String,
  boom: Option<u64>,
  #[serde(default="default_zoom")]
  zoom: u16
}

The following will yield an application configured with a zoom of 32

$ FOO=8080 BAR=true BAZ=hello yourapp

The following will yield an application configured with a zoom of 10

$ FOO=8080 BAR=true BAZ=hello ZOOM=10 yourapp

The common pattern for prefixing env var names for a specific app is supported using the envy::prefixed(prefix) interface. Asumming your env vars are prefixed with APP_ the above example may instead look like

use serde::Deserialize;

#[derive(Deserialize, Debug)]
struct Config {
  foo: u16,
  bar: bool,
  baz: String,
  boom: Option<u64>
}

fn main() {
    match envy::prefixed("APP_").from_env::<Config>() {
       Ok(config) => println!("{:#?}", config),
       Err(error) => panic!("{:#?}", error)
    }
}

the expectation would then be to export the same environment variables prefixed with APP_

$ APP_FOO=8080 APP_BAR=true APP_BAZ=hello yourapp

๐Ÿ‘ญ Consider this crate a cousin of envy-store, a crate for deserializing AWS parameter store values into typesafe structs and recap, a crate for deserializing named regex capture groups into typesafe structs.

Doug Tangren (softprops) 2016-2019

envy's People

Contributors

softprops avatar rhysd avatar tobz1000 avatar magnet avatar d-e-s-o avatar enrichman avatar kimond avatar barskern avatar rnestler avatar zsiciarz avatar dependabot-preview[bot] avatar j-brn avatar walfie avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.