From f3b6fd435478a129dbf6ede37d8881c7a55efc42 Mon Sep 17 00:00:00 2001 From: Ryan Leckey Date: Fri, 27 Jan 2017 11:14:28 -0800 Subject: Move 'Envrionment' into its own source --- src/config.rs | 64 +---------------------------------------------------------- 1 file changed, 1 insertion(+), 63 deletions(-) (limited to 'src/config.rs') diff --git a/src/config.rs b/src/config.rs index 42e6c5c..e5b25f6 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,14 +1,11 @@ use value::Value; use source::{Source, SourceBuilder}; -use std::env; use std::error::Error; use std::collections::HashMap; #[derive(Default)] pub struct Config { - env_prefix: Option, - defaults: HashMap, overrides: HashMap, sources: Vec>, @@ -28,15 +25,6 @@ impl Config { Ok(()) } - /// Defines a prefix that environment variables - /// must start with to be considered. - /// - /// By default all environment variables are considered. This can lead to unexpected values - /// in configuration (eg. `PATH`). - pub fn set_env_prefix(&mut self, prefix: &str) { - self.env_prefix = Some(prefix.to_uppercase()); - } - /// Sets the default value for this key. The default value is only used /// when no other value is provided. pub fn set_default(&mut self, key: &str, value: T) @@ -59,23 +47,6 @@ impl Config { return Some(value.clone()); } - // Check environment - - // Transform key into an env_key which is uppercased - // and has the optional prefix applied - let mut env_key = String::new(); - - if let Some(ref env_prefix) = self.env_prefix { - env_key.push_str(env_prefix); - env_key.push('_'); - } - - env_key.push_str(&key.to_uppercase()); - - if let Ok(value) = env::var(env_key.clone()) { - return Some(Value::from(value)); - } - // Check sources for source in &mut self.sources.iter().rev() { @@ -118,44 +89,11 @@ mod test { // Retrieval of a non-existent key #[test] fn test_not_found() { - let mut c = Config::new(); + let c = Config::new(); assert_eq!(c.get_int("key"), None); } - // // Environment override - // #[test] - // fn test_env_override() { - // let mut c = Config::new(); - - // c.set_default("key_1", false); - - // env::set_var("KEY_1", "1"); - - // assert_eq!(c.get_bool("key_1"), Some(true)); - - // // TODO(@rust): Is there a way to easily kill this at the end of a test? - // env::remove_var("KEY_1"); - // } - - // // Environment prefix - // #[test] - // fn test_env_prefix() { - // let mut c = Config::new(); - - // env::set_var("KEY_1", "1"); - // env::set_var("CFG_KEY_2", "false"); - - // c.set_env_prefix("CFG"); - - // assert_eq!(c.get_bool("key_1"), None); - // assert_eq!(c.get_bool("key_2"), Some(false)); - - // // TODO(@rust): Is there a way to easily kill this at the end of a test? - // env::remove_var("KEY_1"); - // env::remove_var("CFG_KEY_2"); - // } - // Explicit override #[test] fn test_default_override() { -- cgit v1.2.3