From c1bcf6ec5537bfd0fb3f1ee8d93a32aa15031cff Mon Sep 17 00:00:00 2001 From: Ryan Leckey Date: Sat, 28 Jan 2017 22:07:13 -0800 Subject: Propagate Cow into Source --- src/config.rs | 10 +++++----- src/env.rs | 5 +++-- src/file/json.rs | 12 ++++++------ src/file/nil.rs | 4 +++- src/file/toml.rs | 12 ++++++------ src/source.rs | 3 ++- src/value.rs | 5 +---- 7 files changed, 26 insertions(+), 25 deletions(-) diff --git a/src/config.rs b/src/config.rs index a1a36cb..b88b812 100644 --- a/src/config.rs +++ b/src/config.rs @@ -81,23 +81,23 @@ impl<'a> ConfigStore<'a> { } } - fn get(&self, key: &str) -> Option { + fn get(&self, key: &str) -> Option> { if let ConfigStore::Mutable { ref overrides, ref sources, ref defaults } = *self { // Check explicit override if let Some(value) = overrides.get(key) { - return Some(value.clone()); + return Some(Cow::Borrowed(value)); } // Check sources for source in &mut sources.iter().rev() { if let Some(value) = source.get(key) { - return Some(value); + return Some(value) } } // Check explicit defaults if let Some(value) = defaults.get(key) { - return Some(value.clone()); + return Some(Cow::Borrowed(value)); } } @@ -154,7 +154,7 @@ impl<'a> Config<'a> { } pub fn get(&self, key: &str) -> Option> { - self.store.get(key).map(Cow::Owned) + self.store.get(key) } pub fn get_str(&'a self, key: &str) -> Option> { diff --git a/src/env.rs b/src/env.rs index 612b97d..9743722 100644 --- a/src/env.rs +++ b/src/env.rs @@ -1,5 +1,6 @@ use std::env; use std::error::Error; +use std::borrow::Cow; use source; use value::Value; @@ -26,7 +27,7 @@ impl source::SourceBuilder for Environment { } impl source::Source for Environment { - fn get(&self, key: &str) -> Option { + fn get<'a>(&self, key: &str) -> Option> { let mut env_key = String::new(); // Apply prefix @@ -38,6 +39,6 @@ impl source::Source for Environment { env_key.push_str(&key.to_uppercase()); // Attempt to retreive environment variable and coerce into a Value - env::var(env_key.clone()).ok().map(Value::from) + env::var(env_key.clone()).ok().map(Value::from).map(Cow::Owned) } } diff --git a/src/file/json.rs b/src/file/json.rs index 165bdbe..587d54a 100644 --- a/src/file/json.rs +++ b/src/file/json.rs @@ -19,28 +19,28 @@ impl Content { } } -fn from_json_value(value: &serde_json::Value) -> Option { +fn from_json_value<'a>(value: &serde_json::Value) -> Option> { match *value { - serde_json::Value::String(ref value) => Some(Value::String(Cow::Borrowed(value))), + serde_json::Value::String(ref value) => Some(Cow::Owned(Value::String(Cow::Borrowed(value)))), serde_json::Value::Number(ref value) => { if let Some(value) = value.as_i64() { - Some(Value::Integer(value)) + Some(Cow::Owned(Value::Integer(value))) } else if let Some(value) = value.as_f64() { - Some(Value::Float(value)) + Some(Cow::Owned(Value::Float(value))) } else { None } } - serde_json::Value::Bool(value) => Some(Value::Boolean(value)), + serde_json::Value::Bool(value) => Some(Cow::Owned(Value::Boolean(value))), _ => None, } } impl Source for Content { - fn get(&self, key: &str) -> Option { + fn get<'a>(&self, key: &str) -> Option> { // TODO: Key segment iteration is not something that should be here directly let key_delim = '.'; let key_segments = key.split(key_delim); diff --git a/src/file/nil.rs b/src/file/nil.rs index c9e799a..f494af4 100644 --- a/src/file/nil.rs +++ b/src/file/nil.rs @@ -1,3 +1,5 @@ +use std::borrow::Cow; + use source::Source; use value::Value; @@ -5,7 +7,7 @@ use value::Value; pub struct Nil {} impl Source for Nil { - fn get(&self, _: &str) -> Option { + fn get<'a>(&self, _: &str) -> Option> { None } } diff --git a/src/file/toml.rs b/src/file/toml.rs index 2fd5bd3..b2a8fe8 100644 --- a/src/file/toml.rs +++ b/src/file/toml.rs @@ -20,19 +20,19 @@ impl Content { } } -fn from_toml_value(value: &toml::Value) -> Option { +fn from_toml_value<'a>(value: &toml::Value) -> Option> { match *value { - toml::Value::String(ref value) => Some(Value::String(Cow::Borrowed(value))), - toml::Value::Float(value) => Some(Value::Float(value)), - toml::Value::Integer(value) => Some(Value::Integer(value)), - toml::Value::Boolean(value) => Some(Value::Boolean(value)), + toml::Value::String(ref value) => Some(Cow::Owned(Value::String(Cow::Borrowed(value)))), + toml::Value::Float(value) => Some(Cow::Owned(Value::Float(value))), + toml::Value::Integer(value) => Some(Cow::Owned(Value::Integer(value))), + toml::Value::Boolean(value) => Some(Cow::Owned(Value::Boolean(value))), _ => None, } } impl Source for Content { - fn get(&self, key: &str) -> Option { + fn get<'a>(&self, key: &str) -> Option> { // TODO: Key segment iteration is not something that should be here directly let key_delim = '.'; let key_segments = key.split(key_delim); diff --git a/src/source.rs b/src/source.rs index 1a8d08f..bc1dd40 100644 --- a/src/source.rs +++ b/src/source.rs @@ -1,9 +1,10 @@ use std::error::Error; +use std::borrow::Cow; use value::Value; pub trait Source { - fn get(&self, key: &str) -> Option; + fn get<'a>(&self, key: &str) -> Option>; } pub trait SourceBuilder { diff --git a/src/value.rs b/src/value.rs index ad173e8..82aef4c 100644 --- a/src/value.rs +++ b/src/value.rs @@ -20,10 +20,7 @@ impl<'a> Value<'a> { /// Gets the underlying value as a string, performing a conversion only if neccessary. pub fn as_str(&'a self) -> Option> { if let Value::String(ref value) = *self { - Some(match *value { - Cow::Borrowed(v) => Cow::Borrowed(v), - Cow::Owned(ref v) => Cow::Borrowed(v), - }) + Some(Cow::Borrowed(&*value)) } else if let Value::Integer(value) = *self { Some(Cow::Owned(value.to_string())) } else if let Value::Float(value) = *self { -- cgit v1.2.3