From 312f32905f518fa59e1daf3fc8224dadf584b484 Mon Sep 17 00:00:00 2001 From: Ryan Leckey Date: Tue, 13 Jun 2017 18:54:39 -0700 Subject: Add more tests on files --- src/config.rs | 63 +++++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 46 insertions(+), 17 deletions(-) (limited to 'src/config.rs') diff --git a/src/config.rs b/src/config.rs index 730b261..0d84906 100644 --- a/src/config.rs +++ b/src/config.rs @@ -42,6 +42,10 @@ pub struct Config { } impl Config { + pub fn new() -> Self { + Config::default() + } + /// Merge in a configuration property source. pub fn merge(&mut self, source: T) -> Result<()> where T: 'static, @@ -104,27 +108,11 @@ impl Config { Ok(()) } + /// Deserialize the entire configuration. pub fn deserialize<'de, T: Deserialize<'de>>(&self) -> Result { T::deserialize(self.cache.clone()) } - pub fn get<'de, T: Deserialize<'de>>(&self, key: &'de str) -> Result { - // Parse the key into a path expression - let expr: path::Expression = key.to_lowercase().parse()?; - - // Traverse the cache using the path to (possibly) retrieve a value - let value = expr.get(&self.cache).cloned(); - - match value { - Some(value) => { - // Deserialize the received value into the requested type - T::deserialize(ValueWithKey::new(value, key)) - } - - None => Err(ConfigError::NotFound(key.into())), - } - } - pub fn set_default(&mut self, key: &str, value: T) -> Result<()> where T: Into { @@ -162,4 +150,45 @@ impl Config { self.refresh() } + + pub fn get<'de, T: Deserialize<'de>>(&self, key: &'de str) -> Result { + // Parse the key into a path expression + let expr: path::Expression = key.to_lowercase().parse()?; + + // Traverse the cache using the path to (possibly) retrieve a value + let value = expr.get(&self.cache).cloned(); + + match value { + Some(value) => { + // Deserialize the received value into the requested type + T::deserialize(ValueWithKey::new(value, key)) + } + + None => Err(ConfigError::NotFound(key.into())), + } + } + + pub fn get_str(&self, key: &str) -> Result { + self.get(key).and_then(Value::into_str) + } + + pub fn get_int(&self, key: &str) -> Result { + self.get(key).and_then(Value::into_int) + } + + pub fn get_float(&self, key: &str) -> Result { + self.get(key).and_then(Value::into_float) + } + + pub fn get_bool(&self, key: &str) -> Result { + self.get(key).and_then(Value::into_bool) + } + + pub fn get_table(&self, key: &str) -> Result> { + self.get(key).and_then(Value::into_table) + } + + pub fn get_array(&self, key: &str) -> Result> { + self.get(key).and_then(Value::into_array) + } } -- cgit v1.2.3