Initialize config structure from environment variables in Rust without boilerplate.
Let's say you application relies on the following environment variables:
DB_HOSTDB_PORTAnd you want to initialize Config structure like this one:
rust,ignore
struct Config {
host: String,
port: u16,
}
You can achieve this with the following code without boilerplate:
```rust use envconfig::Envconfig;
pub struct Config { #[envconfig(from = "DBHOST")] pub dbhost: String,
#[envconfig(from = "DB_PORT", default = "5432")]
pub db_port: u16,
}
fn main() { // Assuming the following environment variables are set std::env::setvar("DBHOST", "127.0.0.1");
// Initialize config from environment variables or terminate the process.
let config = Config::init_from_env().unwrap();
assert_eq!(config.db_host, "127.0.0.1");
assert_eq!(config.db_port, 5432);
} ```
When writing tests you should avoid using environment variables. Cargo runs Rust tests in parallel by default which means you can end up with race conditions in your tests if two or more are fighting over an environment variable.
To solve this you can initialise your struct from a HashMap<String, String> in your tests. The HashMap should
match what you expect the real environment variables to be; for example DB_HOST environment variable becomes a
DB_HOST key in your HashMap.
```rust use envconfig::Envconfig;
pub struct Config { #[envconfig(from = "DBHOST")] pub dbhost: String,
#[envconfig(from = "DB_PORT", default = "5432")]
pub db_port: u16,
}
fn testconfigcanbeloadedfromhashmap() { // Create a HashMap that looks like your environment let mut hashmap = HashMap::new(); hashmap.insert("DBHOST".tostring(), "127.0.0.1".to_string());
// Initialize config from a HashMap to avoid test race conditions
let config = Config::init_from_hashmap(&hashmap).unwrap();
assert_eq!(config.db_host, "127.0.0.1");
assert_eq!(config.db_port, 5432);
} ```
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