pub struct Environments<S, T> where
S: Ord, { /* fields omitted */ }
Hold environment specific data as a map from your environment hierarchy key to data struct
containg the config for that particular environment.
#[derive(Debug, Deserialize, Getters, Serialize)]
struct RuntimeEnv {
#[get]
name: String,
#[get]
key: Option<String>,
}
let toml = r#"[envs.prod]
name = "Production"
key = "abcd-123-efg-45"
[envs.stage]
name = "Stage"
[envs.test]
name = "Test"
[envs.dev]
name = "Development"
[envs.local]
name = "Local"
"#;
let mut buffer = String::new();
let mut cursor = Cursor::new(toml);
let envs: Environments<Environment, RuntimeEnv> = Environments::from_reader(&mut cursor, &mut buffer)?;
env::set_var("env", "prod");
let mut current = envs.current()?;
assert_eq!(current.name(), "Production");
assert_eq!(current.key(), &Some("abcd-123-efg-45".to_string()));
env::set_var("env", "stage");
current = envs.current()?;
assert_eq!(current.name(), "Stage");
assert_eq!(current.key(), &None);
env::set_var("env", "test");
current = envs.current()?;
assert_eq!(current.name(), "Test");
assert_eq!(current.key(), &None);
env::set_var("env", "dev");
current = envs.current()?;
assert_eq!(current.name(), "Development");
assert_eq!(current.key(), &None);
env::set_var("env", "local");
current = envs.current()?;
assert_eq!(current.name(), "Local");
assert_eq!(current.key(), &None);
Load the environments from a path.
Load the environments from a reader.
Get the current environment
Formats the value using the given formatter. Read more