diff options
author | Christian Fochler <mail@christianfochler.de> | 2018-01-28 21:16:21 +0100 |
---|---|---|
committer | Christian Fochler <mail@christianfochler.de> | 2018-01-28 21:16:21 +0100 |
commit | 536f52fed4a22ed158681edce08211845abff985 (patch) | |
tree | 01603ed4f49ab285568d7b3997770b435c54ffac | |
parent | c590f6cc3eca4a100bf769ef957f40ea43b1745c (diff) |
make separator optional
-rw-r--r-- | src/env.rs | 38 |
1 files changed, 21 insertions, 17 deletions
@@ -9,19 +9,16 @@ pub struct Environment { /// Optional prefix that will limit access to the environment to only keys that /// begin with the defined prefix. /// - /// A prefix, followed by `_` (the seperator), - /// is tested to be present on each key before its considered + /// A prefix 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`. + /// For example, the key `CONFIG_DEBUG` would become `DEBUG` with a prefix of `config_`. prefix: Option<String>, - /// The character sequence that separates each key segment in an environment key pattern. + /// 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. - /// - /// The default separator is `_`. - separator: String, + separator: Option<String>, } impl Environment { @@ -36,13 +33,13 @@ impl Environment { } } - pub fn prefix(&mut self, s: String) -> &mut Self { - self.prefix = s.into(); + pub fn prefix(&mut self, s: &str) -> &mut Self { + self.prefix = Some(s.into()); self } - pub fn separator(&mut self, s: String) -> &mut Self { - self.separator = s; + pub fn separator(&mut self, s: &str) -> &mut Self { + self.separator = Some(s.into()); self } } @@ -51,7 +48,7 @@ impl Default for Environment { fn default() -> Environment { Environment { prefix: None, - separator: "_".into(), + separator: None, } } } @@ -65,9 +62,14 @@ impl Source for Environment { let mut m = HashMap::new(); let uri: String = "the environment".into(); - // Define a prefiux pattern to test and exclude from keys + let separator = match self.separator { + Some(ref separator) => separator, + _ => "" + }; + + // Define a prefix pattern to test and exclude from keys let prefix_pattern = match self.prefix { - Some(ref prefix) => Some(prefix.clone() + &self.separator), + Some(ref prefix) => Some(prefix.clone() + &separator), _ => None, }; @@ -76,7 +78,7 @@ impl Source for Environment { // Check for prefix if let Some(ref prefix_pattern) = prefix_pattern { - if key.to_lowercase().starts_with(prefix_pattern) { + if key.to_lowercase().starts_with(&prefix_pattern.to_lowercase()) { // Remove this prefix from the key key = key[prefix_pattern.len()..].to_string(); } else { @@ -85,8 +87,10 @@ impl Source for Environment { } } - // Replace `separator` with `.` - key = key.replace(&self.separator, "."); + // If separator is given replace with `.` + if !separator.is_empty() { + key = key.replace(separator, "."); + } m.insert( key.to_lowercase(), |