Yet another stupid environment config (YASEC) creates settings from environment variables. (Envconig-rs fork)
password
then it gets value from environment variable PASSWORD
.
If a configuration field is inside another structure and it has path db.password
it gets its value from variable DB_PASSWORD
.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.
from
- name of an environment variable which provides a field value. Name of the field and name of the parent structures are ignored.default
- default value of a field if an environment variable doesn't exist. If the environment variable exist but has invalid value an error returns.You can achieve this with the following code without boilerplate:
```rust
extern crate yasec_derive; extern crate yasec;
use std::error::Error as StdError; use yasec::Yasec;
pub struct DB { pub host: String, pub port: u16, }
pub struct Vendor { #[yasec(from = "APIKEY")] pub key: String, #[yasec(from = "APISECRET")] pub secret: String, }
pub struct Config {
db: DB,
vendor: Vendor,
#[yasec(default = 8080)]
listenport: u16,
callbackurl: Option
pub enum Mode { Client, Server, }
impl Yasec for Mode {
fn parse(s: &str) -> Result
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"),
}
} ```
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
Licensed under MIT