diff options
author | Ryan Leckey <leckey.ryan@gmail.com> | 2017-02-02 12:03:55 -0800 |
---|---|---|
committer | Ryan Leckey <leckey.ryan@gmail.com> | 2017-02-02 12:03:55 -0800 |
commit | 115fe07e2c11aa72e91a5ce9b028ed1c1ff7d806 (patch) | |
tree | 48ccdb6417c3c7c9593a9c447157e3eba724f1a8 /src/env.rs | |
parent | d913d951f1ff040a543f9ac859a0f300ce4d8434 (diff) |
Add support for Table/Array and deep merging of configuration values
Diffstat (limited to 'src/env.rs')
-rw-r--r-- | src/env.rs | 38 |
1 files changed, 26 insertions, 12 deletions
@@ -1,6 +1,6 @@ use std::env; use std::error::Error; -use std::borrow::Cow; +use std::collections::HashMap; use source; use value::Value; @@ -27,18 +27,32 @@ impl source::SourceBuilder for Environment { } impl source::Source for Environment { - fn get<'a>(&self, key: &str) -> Option<Cow<'a, Value>> { - let mut env_key = String::new(); - - // Apply prefix - if let Some(ref prefix) = self.prefix { - env_key.push_str(prefix); - env_key.push('_'); + fn collect(&self) -> HashMap<String, Value> { + // Iterate through environment variables + let mut r = HashMap::new(); + + // Make prefix pattern + let prefix_pat = if let Some(ref prefix) = self.prefix { + Some(prefix.clone() + "_".into()) + } else { None }; + + for (key, value) in env::vars() { + let mut key = key.to_string(); + + // Check if key matches prefix + if let Some(ref prefix_pat) = prefix_pat { + if key.starts_with(prefix_pat) { + // Remove the prefix from the key + key = key[prefix_pat.len()..].to_string(); + } else { + // Skip this key + continue; + } + } + + r.insert(key, Value::String(value)); } - env_key.push_str(&key.to_uppercase()); - - // Attempt to retreive environment variable and coerce into a Value - env::var(env_key.clone()).ok().map(Value::from).map(Cow::Owned) + r } } |