summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRyan Leckey <leckey.ryan@gmail.com>2017-01-28 22:07:13 -0800
committerRyan Leckey <leckey.ryan@gmail.com>2017-01-28 22:07:13 -0800
commitc1bcf6ec5537bfd0fb3f1ee8d93a32aa15031cff (patch)
tree05fd0b9a2daae99543a9d5ae3a6e15eebc622055
parent7d870758cbd9ad4181471ad40184d1bac1204e1e (diff)
Propagate Cow into Source
-rw-r--r--src/config.rs10
-rw-r--r--src/env.rs5
-rw-r--r--src/file/json.rs12
-rw-r--r--src/file/nil.rs4
-rw-r--r--src/file/toml.rs12
-rw-r--r--src/source.rs3
-rw-r--r--src/value.rs5
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<Value> {
+ fn get(&self, key: &str) -> Option<Cow<'a, Value>> {
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<Cow<'a, Value>> {
- self.store.get(key).map(Cow::Owned)
+ self.store.get(key)
}
pub fn get_str(&'a self, key: &str) -> Option<Cow<'a, str>> {
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<Value> {
+ fn get<'a>(&self, key: &str) -> Option<Cow<'a, Value>> {
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<Value> {
+fn from_json_value<'a>(value: &serde_json::Value) -> Option<Cow<'a, Value>> {
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<Value> {
+ fn get<'a>(&self, key: &str) -> Option<Cow<'a, Value>> {
// 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<Value> {
+ fn get<'a>(&self, _: &str) -> Option<Cow<'a, Value>> {
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<Value> {
+fn from_toml_value<'a>(value: &toml::Value) -> Option<Cow<'a, Value>> {
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<Value> {
+ fn get<'a>(&self, key: &str) -> Option<Cow<'a, Value>> {
// 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<Value>;
+ fn get<'a>(&self, key: &str) -> Option<Cow<'a, Value>>;
}
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<Cow<'a, str>> {
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 {