Yasec

Build Status License

Yet another stupid environment config (YASEC) creates settings from environment variables. (Envconig-rs fork)

Features

I implemented everything what I require when I develop an application. Feel free to open an issue of a feature you miss as well as a pull request.

Macro attributes

Usage

You can achieve this with the following code without boilerplate:

```rust

[macro_use]

extern crate yasec_derive; extern crate yasec;

use std::error::Error as StdError; use yasec::Yasec;

[derive(Yasec)]

pub struct DB { pub host: String, pub port: u16, }

[derive(Yasec)]

pub struct Vendor { #[yasec(from = "APIKEY")] pub key: String, #[yasec(from = "APISECRET")] pub secret: String, }

[derive(Yasec)]

pub struct Config { db: DB, vendor: Vendor, #[yasec(default = 8080)] listenport: u16, callbackurl: Option, mode: Mode, }

pub enum Mode { Client, Server, }

impl Yasec for Mode { fn parse(s: &str) -> Result> { match s { "CLIENT" => Ok(Self::Client), "SERVER" => Ok(Self::Server), _ => Err(yasec::ParseError::new(s).into()), } } }

fn main() { // Assuming the following environment variables are set std::env::setvar("DBHOST", "127.0.0.1"); std::env::setvar("DBPORT", "5432"); std::env::setvar("APIKEY", "0912xn819b8s1029s"); std::env::setvar("APISECRET", "zyYWn5pPtLcDSaFWQEu0nf1cf0eYNN8j"); std::env::setvar("MODE", "SERVER"); std::env::removevar("LISTENPORT"); std::env::removevar("CALLBACK_URL");

// 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);
assert_eq!(config.vendor.key, "0912xn819b8s1029s");
assert_eq!(config.vendor.secret, "zyYWn5pPtLcDSaFWQEu0nf1cf0eYNN8j");
assert_eq!(config.listen_port, 8080);
assert_eq!(config.callback_url, None);
match config.mode {
    Mode::Server => (),
    _ => panic!("Unexpected value of Mode"),
}

} ```

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

Licensed under MIT