pub struct Environment { /* private fields */ }
Expand description
An environment source collects a dictionary of environment variables values into a hierarchical config Value type. We have to be aware how the config tree is created from the environment dictionary, therefore we are mindful about prefixes for the environment keys, level separators, encoding form (kebab, snake case) etc.
Implementations§
Source§impl Environment
impl Environment
Sourcepub fn with_prefix(s: &str) -> Self
pub fn with_prefix(s: &str) -> Self
Optional prefix that will limit access to the environment to only keys that begin with the defined prefix.
A prefix with a separator of _
is tested to be present on each key before its considered
to be part of the source environment.
For example, the key CONFIG_DEBUG
would become DEBUG
with a prefix of config
.
Sourcepub fn prefix_separator(self, s: &str) -> Self
pub fn prefix_separator(self, s: &str) -> Self
Optional character sequence that separates the prefix from the rest of the key
Sourcepub fn separator(self, s: &str) -> Self
pub fn separator(self, s: &str) -> Self
Optional character sequence that separates each key segment in an environment key pattern.
Consider a nested configuration such as redis.password
, a separator of _
would allow
an environment key of REDIS_PASSWORD
to match.
Sourcepub fn list_separator(self, s: &str) -> Self
pub fn list_separator(self, s: &str) -> Self
When set and try_parsing
is true, then all environment variables will be parsed as Vec<String>
instead of String
.
See
with_list_parse_key
when you want to use Vec<String>
in combination with String
.
Sourcepub fn with_list_parse_key(self, key: &str) -> Self
pub fn with_list_parse_key(self, key: &str) -> Self
Add a key which should be parsed as a list when collecting Value
s from the environment.
Once list_separator
is set, the type for string is Vec<String>
.
To switch the default type back to type Strings you need to provide the keys which should be Vec<String>
using this function.
Sourcepub fn ignore_empty(self, ignore: bool) -> Self
pub fn ignore_empty(self, ignore: bool) -> Self
Ignore empty env values (treat as unset).
Sourcepub fn try_parsing(self, try_parsing: bool) -> Self
pub fn try_parsing(self, try_parsing: bool) -> Self
Note: enabling try_parsing
can reduce performance it will try and parse
each environment variable 3 times (bool, i64, f64)
pub fn keep_prefix(self, keep: bool) -> Self
Sourcepub fn source(self, source: Option<Map<String, String>>) -> Self
pub fn source(self, source: Option<Map<String, String>>) -> Self
Alternate source for the environment. This can be used when you want to test your own code using this source, without the need to change the actual system environment variables.
§Example
#[test]
fn test_config() -> Result<(), config::ConfigError> {
#[derive(Clone, Debug, Deserialize)]
struct MyConfig {
pub my_string: String,
}
let source = Environment::default()
.source(Some({
let mut env = HashMap::new();
env.insert("MY_STRING".into(), "my-value".into());
env
}));
let config: MyConfig = Config::builder()
.add_source(source)
.build()?
.try_into()?;
assert_eq!(config.my_string, "my-value");
Ok(())
}
Trait Implementations§
Source§impl Clone for Environment
impl Clone for Environment
Source§fn clone(&self) -> Environment
fn clone(&self) -> Environment
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more