summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEduardo Canellas <eduardocanellas98@gmail.com>2021-06-08 15:07:35 -0300
committerEduardo Canellas <eduardocanellas98@gmail.com>2021-06-09 17:55:28 -0300
commit55e82428931dc6e9e6e0f920d7c200da09f011f3 (patch)
tree77eba736644a549005d52b4ac192ab7b216b9f97
parent06bf59b799db5d2c66f605829d08d624c9b60129 (diff)
refactor(env): optimize and reduce allocations
-rw-r--r--src/env.rs22
1 files changed, 6 insertions, 16 deletions
diff --git a/src/env.rs b/src/env.rs
index 557231e..3d95e17 100644
--- a/src/env.rs
+++ b/src/env.rs
@@ -83,21 +83,14 @@ impl Source for Environment {
let mut m = HashMap::new();
let uri: String = "the environment".into();
- let separator = match self.separator {
- Some(ref separator) => separator,
- _ => "",
- };
-
- let group_separator = match self.separator {
- Some(ref separator) => separator,
- _ => "_",
- };
+ let separator = self.separator.as_deref().unwrap_or("");
+ let group_separator = self.separator.as_deref().unwrap_or("_");
// Define a prefix pattern to test and exclude from keys
let prefix_pattern = self
.prefix
.as_ref()
- .map(|prefix| format!("{}{}", prefix.clone(), group_separator));
+ .map(|prefix| format!("{}{}", prefix, group_separator).to_lowercase());
for (key, value) in env::vars() {
// Treat empty environment variables as unset
@@ -105,14 +98,11 @@ impl Source for Environment {
continue;
}
- let mut key = key.to_string();
+ let mut key = key.to_lowercase();
// Check for prefix
if let Some(ref prefix_pattern) = prefix_pattern {
- if key
- .to_lowercase()
- .starts_with(&prefix_pattern.to_lowercase())
- {
+ if key.starts_with(prefix_pattern) {
// Remove this prefix from the key
key = key[prefix_pattern.len()..].to_string();
} else {
@@ -141,7 +131,7 @@ impl Source for Environment {
ValueKind::String(value)
};
- m.insert(key.to_lowercase(), Value::new(Some(&uri), value));
+ m.insert(key, Value::new(Some(&uri), value));
}
Ok(m)