From 536f52fed4a22ed158681edce08211845abff985 Mon Sep 17 00:00:00 2001 From: Christian Fochler Date: Sun, 28 Jan 2018 21:16:21 +0100 Subject: make separator optional --- src/env.rs | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/src/env.rs b/src/env.rs index aec4de4..5f7e92f 100644 --- a/src/env.rs +++ b/src/env.rs @@ -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, - /// 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, } 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(), -- cgit v1.2.3