envconfig 0.3.0

Build a config object from environment variables without boilerplate.
Documentation

Envconfig

Build Status License Documentation

Build a config structure form environment variables in Rust without boilerplate.

Usage

Let's say you application relies on the following environment variables:

  • DB_HOST
  • DB_PORT

And you want to initialize Config structure like this one:

struct Config {
  host: String,
  port: u16
}

You can achieve this with the following code without boilerplate:

#[macro_use]
extern crate envconfig_derive;
extern crate envconfig;

#[derive(Envconfig)]
pub struct Config {
    #[envconfig(from = "DB_HOST")]
    pub db_host: String,

    #[envconfig(from = "DB_PORT")]
    pub db_port: u16,
}

// Initialize config from environment variables or terminate the process.
let config = Config::init().unwrap_or_else(|err| {
    eprintln!("{}", err);
    ::std::process::exit(1);
});

Running tests

Tests do some manipulation with environment variables, so to prevent flaky tests they have to be executed in a single thread:

cargo test -- --test-threads=1

License

MIT © Sergey Potapov

Contributors

  • greyblake Potapov Sergey - creator, maintainer.