Module plugx_config::loader::env
source · Expand description
Environment-Variables configuration loader.
This is only usable if you enabled env Cargo feature.
Example
use std::collections::HashMap;
use std::env::set_var;
use url::Url;
use plugx_config::loader::{ConfigurationLoader, env::ConfigurationLoaderEnv};
set_var("MY_APP_NAME_FOO__B_A_R", "Baz");
set_var("MY_APP_NAME_QUX__ABC", "XYZ");
let url = Url::try_from("env://?prefix=MY_APP_NAME_&key_separator=__").expect("A valid URL!");
let mut loader = ConfigurationLoaderEnv::new();
// You could set `prefix` and `key_separator` like this too:
// loader.set_prefix("MY_APP_NAME_");
// loader.set_key_separator("__");
let modifier = |url: &Url, loaded: &mut HashMap<String, _>| {
// Modify loaded configuration if needed
Ok(())
};
loader.set_modifier(Box::new(modifier)); // Note that setting a modifier is optional
// We do not set `whitelist` so we're going to load all plugins configurations:
let mut maybe_whitelist = None;
let result = loader.try_load(&url, maybe_whitelist).unwrap();
let foo = result.get("foo").unwrap();
assert_eq!(foo.maybe_contents(), Some(&"B_A_R=\"Baz\"".to_string()));
let qux = result.get("qux").unwrap();
assert_eq!(qux.maybe_contents(), Some(&"ABC=\"XYZ\"".to_string()));
// Only load (and not modify) `foo` plugin configurations:
let whitelist = ["foo".to_string()].to_vec();
maybe_whitelist = Some(whitelist.as_slice());
let result = loader.try_load(&url, maybe_whitelist).unwrap();
assert!(result.get("foo").is_some());
assert!(result.get("qux").is_none());See loader documentation to known how loaders work.
Structs
- Loads configurations from Environment-Variables.