From 8cfde7d3c6767e39e4ca35f99f453f810f6d1926 Mon Sep 17 00:00:00 2001 From: Ryan Leckey Date: Tue, 25 Sep 2018 23:45:11 -0700 Subject: Use a build config on Environment instead of a feature flag for #78 --- Cargo.toml | 1 - src/env.rs | 11 ++++++++++- tests/env.rs | 5 ++--- 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, + + /// 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")); -- cgit v1.2.3