#config #env #macro #configuration #environment

macro envconfig_derive

Build a config structure from environment variables without boilerplate

8 releases (breaking)

new 0.8.0 Mar 31, 2020
0.7.0 Feb 20, 2020
0.6.0 Dec 22, 2019
0.5.1 Apr 16, 2019
0.2.0 Sep 12, 2018

#88 in Configuration

Download history 156/week @ 2019-12-10 212/week @ 2019-12-17 29/week @ 2019-12-24 19/week @ 2019-12-31 137/week @ 2020-01-07 65/week @ 2020-01-14 83/week @ 2020-01-21 71/week @ 2020-01-28 127/week @ 2020-02-04 172/week @ 2020-02-11 146/week @ 2020-02-18 414/week @ 2020-02-25 236/week @ 2020-03-03 432/week @ 2020-03-10 289/week @ 2020-03-17 273/week @ 2020-03-24

841 downloads per month
Used in envconfig

MIT license

9KB
164 lines

Envconfig

Build Status License Documentation

Build a config structure from 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;

use envconfig::Envconfig;

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

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

fn main() {
    // Assuming the following environment variables are set
    std::env::set_var("DB_HOST", "127.0.0.1");

    // Initialize config from environment variables or terminate the process.
    let config = Config::init().unwrap();

    assert_eq!(config.db_host, "127.0.0.1");
    assert_eq!(config.db_port, 5432);
}

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

Roadmap

  • - migrate to the latest versions of syn and quote
  • - support Option<T> (issue)
  • - support default attribute (issue)
  • - support nested structures?

License

MIT © Sergey Potapov

Contributors

  • greyblake Potapov Sergey - creator, maintainer.
  • allevo Tommaso Allevi - support nested structures
  • hobofan Maximilian Goisser - update dependencies

Dependencies

~445–760KB
~18K SLoC