summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRyan Leckey <ryan@launchbadge.com>2018-09-25 23:45:11 -0700
committerRyan Leckey <ryan@launchbadge.com>2018-09-25 23:45:11 -0700
commit8cfde7d3c6767e39e4ca35f99f453f810f6d1926 (patch)
tree7d2e4ae3734c9c11d5d5b94bf399dfd5da3149e4
parenta1cd868c0224f16d921a25f04cbc39cb03a5471d (diff)
Use a build config on Environment instead of a feature flag for #78
-rw-r--r--Cargo.toml1
-rw-r--r--src/env.rs11
-rw-r--r--tests/env.rs5
3 files changed, 12 insertions, 5 deletions
diff --git a/Cargo.toml b/Cargo.toml
index cdca1dd..7b783d6 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -19,7 +19,6 @@ json = ["serde_json"]
yaml = ["yaml-rust"]
hjson = ["serde-hjson"]
ini = ["rust-ini"]
-ignore-empty-env-vars = []
[dependencies]
lazy_static = "1.0"
diff --git a/src/env.rs b/src/env.rs
index d570932..ce67069 100644
--- a/src/env.rs
+++ b/src/env.rs
@@ -19,6 +19,9 @@ pub struct Environment {
/// Consider a nested configuration such as `redis.password`, a separator of `_` would allow
/// an environment key of `REDIS_PASSWORD` to match.
separator: Option<String>,
+
+ /// Ignore empty env values (treat as unset).
+ ignore_empty: bool,
}
impl Environment {
@@ -42,6 +45,11 @@ impl Environment {
self.separator = Some(s.into());
self
}
+
+ pub fn ignore_empty(mut self, ignore: bool) -> Self {
+ self.ignore_empty = ignore;
+ self
+ }
}
impl Default for Environment {
@@ -49,6 +57,7 @@ impl Default for Environment {
Environment {
prefix: None,
separator: None,
+ ignore_empty: false,
}
}
}
@@ -75,7 +84,7 @@ impl Source for Environment {
for (key, value) in env::vars() {
// Treat empty environment variables as unset
- if cfg!(feature = "ignore-empty-env-vars") && value == "" {
+ if self.ignore_empty && value == "" {
continue;
}
diff --git a/tests/env.rs b/tests/env.rs
index d1cb11a..bb1a1ec 100644
--- a/tests/env.rs
+++ b/tests/env.rs
@@ -54,7 +54,7 @@ fn test_prefix_with_variant_forms_of_spelling() {
fn test_separator_behavior() {
env::set_var("C_B_A", "abc");
- let mut environment = Environment::with_prefix("C").separator("_");
+ let environment = Environment::with_prefix("C").separator("_");
assert!(environment.collect().unwrap().contains_key("b.a"));
@@ -62,11 +62,10 @@ fn test_separator_behavior() {
}
#[test]
-#[cfg(feature = "ignore-empty-env-vars")]
fn test_empty_value_is_ignored() {
env::set_var("C_A_B", "");
- let environment = Environment::new();
+ let environment = Environment::new().ignore_empty(true);
assert!(!environment.collect().unwrap().contains_key("c_a_b"));