Skip to content

Commit

Permalink
Propagate Cow into Source
Browse files Browse the repository at this point in the history
  • Loading branch information
mehcode committed Jan 29, 2017
1 parent 7d87075 commit c1bcf6e
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 25 deletions.
10 changes: 5 additions & 5 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

Expand Down Expand Up @@ -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>> {
Expand Down
5 changes: 3 additions & 2 deletions src/env.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::env;
use std::error::Error;
use std::borrow::Cow;

use source;
use value::Value;
Expand All @@ -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
Expand All @@ -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)
}
}
12 changes: 6 additions & 6 deletions src/file/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 3 additions & 1 deletion src/file/nil.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use std::borrow::Cow;

use source::Source;
use value::Value;

// Nil source that does nothing to easily allow for optional files
pub struct Nil {}

impl Source for Nil {
fn get(&self, _: &str) -> Option<Value> {
fn get<'a>(&self, _: &str) -> Option<Cow<'a, Value>> {
None
}
}
12 changes: 6 additions & 6 deletions src/file/toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion src/source.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
5 changes: 1 addition & 4 deletions src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit c1bcf6e

Please sign in to comment.