From 4b9519d20788f9da6b82a94e036cae7c77cb4798 Mon Sep 17 00:00:00 2001 From: Ryan Leckey Date: Mon, 30 Jan 2017 14:56:49 -0800 Subject: :shirt: --- src/config.rs | 2 +- src/file/json.rs | 4 ++- src/lib.rs | 46 +++++++++++++++++++++++++++++++++ src/value.rs | 77 +++++++++++++++++++++++++------------------------------- 4 files changed, 84 insertions(+), 45 deletions(-) diff --git a/src/config.rs b/src/config.rs index b88b812..b3e296b 100644 --- a/src/config.rs +++ b/src/config.rs @@ -91,7 +91,7 @@ impl<'a> ConfigStore<'a> { // Check sources for source in &mut sources.iter().rev() { if let Some(value) = source.get(key) { - return Some(value) + return Some(value); } } diff --git a/src/file/json.rs b/src/file/json.rs index 587d54a..5f1c3a2 100644 --- a/src/file/json.rs +++ b/src/file/json.rs @@ -21,7 +21,9 @@ impl Content { fn from_json_value<'a>(value: &serde_json::Value) -> Option> { match *value { - serde_json::Value::String(ref value) => Some(Cow::Owned(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() { diff --git a/src/lib.rs b/src/lib.rs index 9cb8f82..3d72620 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,6 +2,52 @@ #![allow(unknown_lints)] +//! Configuration is gathered by building a `Source` and then merging that source into the +//! current state of the configuration. +//! +//! ```rust +//! fn main() { +//! // Add environment variables that begin with RUST_ +//! config::merge(config::Environment::new("RUST")); +//! +//! // Add 'Settings.json' +//! config::merge(config::File::new("Settings", config::FileFormat::Json)); +//! +//! // Add 'Settings.$(RUST_ENV).json` +//! let name = format!("Settings.{}", config::get_str("env").unwrap()); +//! config::merge(config::File::new(&name, config::FileFormat::Json)); +//! } +//! ``` +//! +//! Note that in the above example the calls to `config::merge` could have +//! been re-ordered to influence the priority as each successive merge +//! is evaluated on top of the previous. +//! +//! Configuration values can be retrieved with a call to `config::get` and then +//! coerced into a type with `as_*`. +//! +//! ```toml +//! # Settings.toml +//! debug = 1 +//! ``` +//! +//! ```rust +//! fn main() { +//! // Add 'Settings.toml' (from above) +//! config::merge(config::File::new("Settings", config::FileFormat::Toml)); +//! +//! // Get 'debug' and coerce to a boolean +//! assert_eq!(config::get("debug").unwrap().as_bool(), Some(true)); +//! +//! // You can use a type suffix on `config::get` to simplify +//! assert_eq!(config::get_bool("debug"), Some(true)); +//! assert_eq!(config::get_str("debug"), Some("true")); +//! } +//! ``` +//! +//! See the [examples](https://github.com/mehcode/config-rs/tree/master/examples) for +//! more usage information. + #[cfg(feature = "toml")] extern crate toml; diff --git a/src/value.rs b/src/value.rs index 82aef4c..561394d 100644 --- a/src/value.rs +++ b/src/value.rs @@ -19,65 +19,56 @@ pub enum Value<'a> { 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(Cow::Borrowed(&*value)) - } else if let Value::Integer(value) = *self { - Some(Cow::Owned(value.to_string())) - } else if let Value::Float(value) = *self { - Some(Cow::Owned(value.to_string())) - } else if let Value::Boolean(value) = *self { - Some(Cow::Owned(value.to_string())) - } else { - None + match *self { + Value::String(ref value) => Some(Cow::Borrowed(&*value)), + Value::Integer(value) => Some(value.to_string().into()), + Value::Float(value) => Some(value.to_string().into()), + Value::Boolean(value) => Some(value.to_string().into()), + + _ => unimplemented!() } } /// Gets the underlying type as a boolean, performing a conversion only if neccessary. pub fn as_bool(&self) -> Option { - if let Value::Boolean(value) = *self { - Some(value) - } else if let Value::String(ref value) = *self { - match value.to_lowercase().as_ref() { - "1" | "true" | "on" | "yes" => Some(true), - "0" | "false" | "off" | "no" => Some(false), - _ => None, + match *self { + Value::Boolean(value) => Some(value), + Value::Integer(value) => Some(value != 0), + Value::Float(value) => Some(value != 0.0), + + Value::String(ref value) => { + match value.to_lowercase().as_ref() { + "1" | "true" | "on" | "yes" => Some(true), + "0" | "false" | "off" | "no" => Some(false), + _ => None, + } } - } else if let Value::Integer(value) = *self { - Some(value != 0) - } else if let Value::Float(value) = *self { - Some(value != 0.0) - } else { - None + + _ => unimplemented!() } } /// Gets the underlying type as an integer, performing a conversion only if neccessary. pub fn as_int(&self) -> Option { - if let Value::Integer(value) = *self { - Some(value) - } else if let Value::String(ref value) = *self { - value.parse().ok() - } else if let Value::Boolean(value) = *self { - Some(if value { 1 } else { 0 }) - } else if let Value::Float(value) = *self { - Some(value.round() as i64) - } else { - None + match *self { + Value::Integer(value) => Some(value), + Value::String(ref value) => value.parse().ok(), + Value::Boolean(value) => Some(if value { 1 } else { 0 }), + Value::Float(value) => Some(value.round() as i64), + + _ => unimplemented!() } } /// Gets the underlying type as a floating-point, performing a conversion only if neccessary. pub fn as_float(&self) -> Option { - if let Value::Float(value) = *self { - Some(value) - } else if let Value::String(ref value) = *self { - value.parse().ok() - } else if let Value::Integer(value) = *self { - Some(value as f64) - } else if let Value::Boolean(value) = *self { - Some(if value { 1.0 } else { 0.0 }) - } else { - None + match *self { + Value::Float(value) => Some(value), + Value::String(ref value) => value.parse().ok(), + Value::Integer(value) => Some(value as f64), + Value::Boolean(value) => Some(if value { 1.0 } else { 0.0 }), + + _ => unimplemented!() } } } -- cgit v1.2.3