diff options
-rw-r--r-- | src/file/mod.rs | 19 | ||||
-rw-r--r-- | src/lib.rs | 40 | ||||
-rw-r--r-- | src/value.rs | 8 |
3 files changed, 28 insertions, 39 deletions
diff --git a/src/file/mod.rs b/src/file/mod.rs index 2177a54..6b9fd5c 100644 --- a/src/file/mod.rs +++ b/src/file/mod.rs @@ -76,19 +76,16 @@ impl File { } } - pub fn path(&mut self, path: &str) -> &mut File { - self.path = Some(path.into()); - self + pub fn path(self, path: &str) -> File { + File { path: Some(path.into()), ..self } } - pub fn namespace(&mut self, namespace: &str) -> &mut File { - self.namespace = Some(namespace.into()); - self + pub fn namespace(self, namespace: &str) -> File { + File { namespace: Some(namespace.into()), ..self } } - pub fn required(&mut self, required: bool) -> &mut File { - self.required = required; - self + pub fn required(self, required: bool) -> File { + File { required: required, ..self } } // Find configuration file @@ -149,9 +146,9 @@ impl SourceBuilder for File { // is required fn build(&self) -> Result<Box<Source>, Box<Error>> { if self.required { - self.try_build().or_else(|_| Ok(Box::new(nil::Nil {}))) - } else { self.try_build() + } else { + self.try_build().or_else(|_| Ok(Box::new(nil::Nil {}))) } } } @@ -6,17 +6,17 @@ //! current state of the configuration. //! //! ```rust -//! fn main() { -//! // Add environment variables that begin with RUST_ -//! config::merge(config::Environment::new("RUST")); +//! // Add environment variables that begin with RUST_ +//! config::merge(config::Environment::new("RUST")).unwrap(); //! -//! // Add 'Settings.json' -//! config::merge(config::File::new("Settings", config::FileFormat::Json)); +//! // Add 'Settings.toml' +//! config::merge(config::File::new("Settings", config::FileFormat::Toml) +//! .required(false)).unwrap(); //! -//! // Add 'Settings.$(RUST_ENV).json` -//! let name = format!("Settings.{}", config::get_str("env").unwrap()); -//! config::merge(config::File::new(&name, config::FileFormat::Json)); -//! } +//! // Add 'Settings.$(RUST_ENV).toml` +//! let name = format!("Settings.{}", config::get_str("env").unwrap_or("development".into())); +//! config::merge(config::File::new(&name, config::FileFormat::Toml) +//! .required(false)).unwrap(); //! ``` //! //! Note that in the above example the calls to `config::merge` could have @@ -26,23 +26,15 @@ //! 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")); +//! // Get 'debug' and coerce to a boolean +//! if let Some(value) = config::get("debug") { +//! println!("{:?}", value.as_bool()); //! } +//! +//! // You can use a type suffix +//! println!("{:?}", config::get_bool("debug")); +//! println!("{:?}", config::get_str("debug")); //! ``` //! //! See the [examples](https://github.com/mehcode/config-rs/tree/master/examples) for diff --git a/src/value.rs b/src/value.rs index 561394d..b66c721 100644 --- a/src/value.rs +++ b/src/value.rs @@ -25,7 +25,7 @@ impl<'a> Value<'a> { Value::Float(value) => Some(value.to_string().into()), Value::Boolean(value) => Some(value.to_string().into()), - _ => unimplemented!() + _ => unimplemented!(), } } @@ -44,7 +44,7 @@ impl<'a> Value<'a> { } } - _ => unimplemented!() + _ => unimplemented!(), } } @@ -56,7 +56,7 @@ impl<'a> Value<'a> { Value::Boolean(value) => Some(if value { 1 } else { 0 }), Value::Float(value) => Some(value.round() as i64), - _ => unimplemented!() + _ => unimplemented!(), } } @@ -68,7 +68,7 @@ impl<'a> Value<'a> { Value::Integer(value) => Some(value as f64), Value::Boolean(value) => Some(if value { 1.0 } else { 0.0 }), - _ => unimplemented!() + _ => unimplemented!(), } } } |