From 14224be23dc2f253a240b85214927d97e1160669 Mon Sep 17 00:00:00 2001 From: Ryan Leckey Date: Sun, 30 Jul 2017 13:20:36 -0700 Subject: Remove ConfigResult; close #36 --- examples/global/Cargo.toml | 7 + examples/global/src/main.rs | 26 +++ examples/hierarchical-env/src/settings.rs | 16 +- examples/pattern/src/main.rs | 16 +- examples/simple/src/main.rs | 8 +- examples/watch/Settings.toml | 2 +- examples/watch/src/main.rs | 10 +- src/config.rs | 268 ++++-------------------------- src/de.rs | 23 ++- src/env.rs | 6 +- src/error.rs | 65 ++++---- src/file/format/json.rs | 21 +-- src/file/format/mod.rs | 9 +- src/file/format/toml.rs | 7 +- src/file/format/yaml.rs | 5 +- src/file/mod.rs | 23 +-- src/file/source/file.rs | 71 ++++---- src/file/source/mod.rs | 7 +- src/file/source/string.rs | 15 +- src/lib.rs | 1 - src/path/mod.rs | 121 +++++++------- src/source.rs | 7 +- src/value.rs | 262 +++++++++++++++++------------ tests/datetime.rs | 5 +- tests/errors.rs | 21 ++- tests/file.rs | 21 ++- tests/file_json.rs | 12 +- tests/file_toml.rs | 12 +- tests/file_yaml.rs | 14 +- tests/get.rs | 22 ++- tests/merge.rs | 5 +- tests/set.rs | 3 +- 32 files changed, 536 insertions(+), 575 deletions(-) create mode 100644 examples/global/Cargo.toml create mode 100644 examples/global/src/main.rs diff --git a/examples/global/Cargo.toml b/examples/global/Cargo.toml new file mode 100644 index 0000000..ec24740 --- /dev/null +++ b/examples/global/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "global" +version = "0.1.0" + +[dependencies] +config = { path = "../../" } +lazy_static = "^0.2.8" diff --git a/examples/global/src/main.rs b/examples/global/src/main.rs new file mode 100644 index 0000000..4fe0864 --- /dev/null +++ b/examples/global/src/main.rs @@ -0,0 +1,26 @@ +#[macro_use] +extern crate lazy_static; + +extern crate config; + +use std::error::Error; +use std::sync::RwLock; +use config::Config; + +lazy_static! { + static ref SETTINGS: RwLock = RwLock::new(Config::default()); +} + +fn try_main() -> Result<(), Box> { + // Set property + SETTINGS.write()?.set("property", 42)?; + + // Get property + println!("property: {}", SETTINGS.read()?.get::("property")?); + + Ok(()) +} + +fn main() { + try_main().unwrap() +} diff --git a/examples/hierarchical-env/src/settings.rs b/examples/hierarchical-env/src/settings.rs index 43897b3..9e07054 100644 --- a/examples/hierarchical-env/src/settings.rs +++ b/examples/hierarchical-env/src/settings.rs @@ -1,5 +1,5 @@ use std::env; -use config::{Config, File, Environment}; +use config::{ConfigError, Config, File, Environment}; #[derive(Debug, Deserialize)] struct Database { @@ -37,34 +37,34 @@ pub struct Settings { } impl Settings { - pub fn new() -> Self { + pub fn new() -> Result { let mut s = Config::new(); // Start off by merging in the "default" configuration file - s.merge(File::with_name("config/default")).unwrap(); + s.merge(File::with_name("config/default"))?; // Add in the current environment file // Default to 'development' env // Note that this file is _optional_ let env = env::var("RUN_MODE").unwrap_or("development".into()); - s.merge(File::with_name(&format!("config/{}", env)).required(false)).unwrap(); + s.merge(File::with_name(&format!("config/{}", env)).required(false))?; // Add in a local configuration file // This file shouldn't be checked in to git - s.merge(File::with_name("config/local").required(false)).unwrap(); + s.merge(File::with_name("config/local").required(false))?; // Add in settings from the environment (with a prefix of APP) // Eg.. `APP_DEBUG=1 ./target/app` would set the `debug` key - s.merge(Environment::with_prefix("app")).unwrap(); + s.merge(Environment::with_prefix("app"))?; // You may also programmatically change settings - s.set("database.url", "postgres://").unwrap(); + s.set("database.url", "postgres://")?; // Now that we're done, let's access our configuration println!("debug: {:?}", s.get_bool("debug")); println!("database: {:?}", s.get::("database.url")); // You can deserialize (and thus freeze) the entire configuration as - s.deserialize().unwrap() + s.deserialize() } } diff --git a/examples/pattern/src/main.rs b/examples/pattern/src/main.rs index aaf164d..f88f1ed 100644 --- a/examples/pattern/src/main.rs +++ b/examples/pattern/src/main.rs @@ -10,12 +10,12 @@ fn main() { // Option 1 // -------- // Gather all conf files from conf/ manually - let settings = Config::default() + let mut settings = Config::default(); + settings // File::with_name(..) is shorthand for File::from(Path::new(..)) - .merge(File::with_name("conf/00-default.toml")) - .merge(File::from(Path::new("conf/05-some.yml"))) - .merge(File::from(Path::new("conf/99-extra.json"))) - .unwrap(); + .merge(File::with_name("conf/00-default.toml")).unwrap() + .merge(File::from(Path::new("conf/05-some.yml"))).unwrap() + .merge(File::from(Path::new("conf/99-extra.json"))).unwrap(); // Print out our settings (as a HashMap) println!("\n{:?} \n\n-----------", @@ -24,7 +24,8 @@ fn main() { // Option 2 // -------- // Gather all conf files from conf/ manually, but put in 1 merge call. - let settings = Config::default() + let mut settings = Config::default(); + settings .merge(vec![File::with_name("conf/00-default.toml"), File::from(Path::new("conf/05-some.yml")), File::from(Path::new("conf/99-extra.json"))]) @@ -37,7 +38,8 @@ fn main() { // Option 3 // -------- // Gather all conf files from conf/ using glob and put in 1 merge call. - let settings = Config::default() + let mut settings = Config::default(); + settings .merge(glob("conf/*") .unwrap() .map(|path| File::from(path.unwrap())) diff --git a/examples/simple/src/main.rs b/examples/simple/src/main.rs index debad02..fb66e03 100644 --- a/examples/simple/src/main.rs +++ b/examples/simple/src/main.rs @@ -3,13 +3,13 @@ extern crate config; use std::collections::HashMap; fn main() { - let settings = config::Config::default() + let mut settings = config::Config::default(); + settings // Add in `./Settings.toml` - .merge(config::File::with_name("Settings")) + .merge(config::File::with_name("Settings")).unwrap() // Add in settings from the environment (with a prefix of APP) // Eg.. `APP_DEBUG=1 ./target/app` would set the `debug` key - .merge(config::Environment::with_prefix("APP")) - .unwrap(); + .merge(config::Environment::with_prefix("APP")).unwrap(); // Print out our settings (as a HashMap) println!("{:?}", diff --git a/examples/watch/Settings.toml b/examples/watch/Settings.toml index f7881bb..8443f7b 100644 --- a/examples/watch/Settings.toml +++ b/examples/watch/Settings.toml @@ -1,3 +1,3 @@ -debug = true +debug = false port = 8080 host = "0.0.0.0" diff --git a/examples/watch/src/main.rs b/examples/watch/src/main.rs index 70fb7ef..0976f74 100644 --- a/examples/watch/src/main.rs +++ b/examples/watch/src/main.rs @@ -12,10 +12,12 @@ use std::sync::mpsc::channel; use std::time::Duration; lazy_static! { - static ref SETTINGS: RwLock = RwLock::new(Config::default() - .merge(File::with_name("Settings.toml")) - .unwrap() - ); + static ref SETTINGS: RwLock = RwLock::new({ + let mut settings = Config::default(); + settings.merge(File::with_name("Settings.toml")).unwrap(); + + settings + }); } fn show() { diff --git a/src/config.rs b/src/config.rs index 5b669a0..0ff2601 100644 --- a/src/config.rs +++ b/src/config.rs @@ -51,17 +51,20 @@ impl Config { } /// Merge in a configuration property source. - pub fn merge(&mut self, source: T) -> ConfigResult - where T: 'static, - T: Source + Send + Sync + pub fn merge(&mut self, source: T) -> Result<&mut Config> + where + T: 'static, + T: Source + Send + Sync, { match self.kind { - ConfigKind::Mutable { ref mut sources, .. } => { + ConfigKind::Mutable { + ref mut sources, .. + } => { sources.push(Box::new(source)); } ConfigKind::Frozen => { - return ConfigResult::Err(ConfigError::Frozen); + return Err(ConfigError::Frozen); } } @@ -73,7 +76,7 @@ impl Config { /// /// Configuration is automatically refreshed after a mutation /// operation (`set`, `merge`, `set_default`, etc.). - pub fn refresh(&mut self) -> ConfigResult { + pub fn refresh(&mut self) -> Result<&mut Config> { self.cache = match self.kind { // TODO: We need to actually merge in all the stuff ConfigKind::Mutable { @@ -89,9 +92,7 @@ impl Config { } // Add sources - if let Err(error) = sources.collect_to(&mut cache) { - return ConfigResult::Err(error); - } + sources.collect_to(&mut cache)?; // Add overrides for (key, val) in overrides { @@ -102,11 +103,11 @@ impl Config { } ConfigKind::Frozen => { - return ConfigResult::Err(ConfigError::Frozen); + return Err(ConfigError::Frozen); } }; - ConfigResult::Ok(self) + Ok(self) } /// Deserialize the entire configuration. @@ -114,41 +115,35 @@ impl Config { T::deserialize(self.cache.clone()) } - pub fn set_default(&mut self, key: &str, value: T) -> ConfigResult - where T: Into + pub fn set_default(&mut self, key: &str, value: T) -> Result<&mut Config> + where + T: Into, { match self.kind { - ConfigKind::Mutable { ref mut defaults, .. } => { - defaults.insert(match key.to_lowercase().parse() { - Ok(expr) => expr, - Err(error) => { - return ConfigResult::Err(error); - } - }, - value.into()); + ConfigKind::Mutable { + ref mut defaults, .. + } => { + defaults.insert(key.to_lowercase().parse()?, value.into()); } - ConfigKind::Frozen => return ConfigResult::Err(ConfigError::Frozen), + ConfigKind::Frozen => return Err(ConfigError::Frozen), }; self.refresh() } - pub fn set(&mut self, key: &str, value: T) -> ConfigResult - where T: Into + pub fn set(&mut self, key: &str, value: T) -> Result<&mut Config> + where + T: Into, { match self.kind { - ConfigKind::Mutable { ref mut overrides, .. } => { - overrides.insert(match key.to_lowercase().parse() { - Ok(expr) => expr, - Err(error) => { - return ConfigResult::Err(error); - } - }, - value.into()); + ConfigKind::Mutable { + ref mut overrides, .. + } => { + overrides.insert(key.to_lowercase().parse()?, value.into()); } - ConfigKind::Frozen => return ConfigResult::Err(ConfigError::Frozen), + ConfigKind::Frozen => return Err(ConfigError::Frozen), }; self.refresh() @@ -195,210 +190,3 @@ impl Config { self.get(key).and_then(Value::into_array) } } - -/// Holds the result of configuration alteration functions. -/// A manual alias of Result to enable a chained API and error forwarding. -pub enum ConfigResult<'a> { - Ok(&'a mut Config), - Err(ConfigError), -} - -#[inline] -fn unwrap_failed(msg: &str, error: E) -> ! { - panic!("{}: {:?}", msg, error) -} - -impl<'a> ConfigResult<'a> { - /// Forwards `Config::merge` - pub fn merge(self, source: T) -> ConfigResult<'a> - where T: 'static, - T: Source + Send + Sync - { - match self { - // If OK, Proceed to nested method - ConfigResult::Ok(instance) => instance.merge(source), - - // Else, Forward the error - error => error, - } - } - - /// Forwards `Config::set_default` - pub fn set_default(self, key: &str, value: T) -> ConfigResult<'a> - where T: Into, - T: 'static - { - match self { - // If OK, Proceed to nested method - ConfigResult::Ok(instance) => instance.set_default(key, value), - - // Else, Forward the error - error => error, - } - } - - /// Forwards `Config::set` - pub fn set(self, key: &str, value: T) -> ConfigResult<'a> - where T: Into, - T: 'static - { - match self { - // If OK, Proceed to nested method - ConfigResult::Ok(instance) => instance.set(key, value), - - // Else, Forward the error - error => error, - } - } - - /// Deserialize the entire configuration. - pub fn deserialize<'de, T: Deserialize<'de>>(self) -> Result { - self.and_then(|instance| instance.deserialize()) - } - - /// Creates a `Result` out of this `ConfigResult` - #[inline] - pub fn to_result(self) -> Result { - match self { - ConfigResult::Ok(value) => Ok(value.clone()), - ConfigResult::Err(error) => Err(error), - } - } - - /// Forwards `Result::is_ok` - #[inline] - pub fn is_ok(&self) -> bool { - match *self { - ConfigResult::Ok(_) => true, - ConfigResult::Err(_) => false, - } - } - - /// Forwards `Result::is_err` - #[inline] - pub fn is_err(&self) -> bool { - !self.is_ok() - } - - /// Forwards `Result::ok` - #[inline] - pub fn ok(self) -> Option { - match self { - ConfigResult::Ok(x) => Some(x.clone()), - ConfigResult::Err(_) => None, - } - } - - /// Forwards `Result::err` - #[inline] - pub fn err(self) -> Option { - match self { - ConfigResult::Ok(_) => None, - ConfigResult::Err(x) => Some(x), - } - } - - /// Forwards `Result::map` - #[inline] - pub fn map(self, op: F) -> Result - where - F: FnOnce(Config) -> U, - { - match self { - ConfigResult::Ok(x) => Ok(op(x.clone())), - ConfigResult::Err(error) => Err(error), - } - } - - /// Forwards `Result::map_err` - #[inline] - pub fn map_err(self, op: O) -> ::std::result::Result - where - O: FnOnce(ConfigError) -> F, - { - match self { - ConfigResult::Err(error) => Err(op(error)), - ConfigResult::Ok(value) => Ok(value.clone()), - } - } - - /// Forwards `Result::and` - #[inline] - pub fn and(self, res: Result) -> Result - { - match self { - ConfigResult::Ok(_) => res, - ConfigResult::Err(error) => Err(error), - } - } - - /// Forwards `Result::and_then` - #[inline] - pub fn and_then(self, op: F) -> Result - where - F: FnOnce(Config) -> Result, - { - match self { - ConfigResult::Ok(value) => op(value.clone()), - ConfigResult::Err(error) => Err(error), - } - } - - /// Forwards `Result::or` - #[inline] - pub fn or(self, res: ::std::result::Result) -> ::std::result::Result - { - match self { - ConfigResult::Ok(value) => Ok(value.clone()), - ConfigResult::Err(_) => res, - } - } - - /// Forwards `Result::or_else` - #[inline] - pub fn or_else(self, op: O) -> ::std::result::Result - where - O: FnOnce(ConfigError) -> ::std::result::Result, - { - match self { - ConfigResult::Ok(value) => Ok(value.clone()), - ConfigResult::Err(error) => op(error), - } - } - - /// Forwards `Result::unwrap` - #[inline] - pub fn unwrap(self) -> Config { - match self { - ConfigResult::Ok(instance) => instance.clone(), - ConfigResult::Err(error) => unwrap_failed("called `ConfigResult::unwrap()` on an `Err` value", error), - } - } - - /// Forwards `Result::expect` - #[inline] - pub fn expect(self, msg: &str) -> Config { - match self { - ConfigResult::Ok(instance) => instance.clone(), - ConfigResult::Err(error) => unwrap_failed(msg, error), - } - } - - /// Forwards `Result::unwrap_err` - #[inline] - pub fn unwrap_err(self) -> ConfigError { - match self { - ConfigResult::Ok(t) => unwrap_failed("called `ConfigResult::unwrap_err()` on an `Ok` value", t), - ConfigResult::Err(e) => e, - } - } - - /// Forwards `Result::expect_err` - #[inline] - pub fn expect_err(self, msg: &str) -> ConfigError { - match self { - ConfigResult::Ok(t) => unwrap_failed(msg, t), - ConfigResult::Err(e) => e, - } - } -} diff --git a/src/de.rs b/src/de.rs index 6487f8b..2ff49e9 100644 --- a/src/de.rs +++ b/src/de.rs @@ -1,5 +1,5 @@ use serde::de; -use value::{Value, ValueWithKey, ValueKind}; +use value::{Value, ValueKind, ValueWithKey}; use error::*; use std::borrow::Cow; use std::iter::Peekable; @@ -13,7 +13,8 @@ impl<'de> de::Deserializer<'de> for ValueWithKey<'de> { #[inline] fn deserialize_any(self, visitor: V) -> Result - where V: de::Visitor<'de> + where + V: de::Visitor<'de>, { // Deserialize based on the underlying type match self.0.kind { @@ -101,7 +102,8 @@ impl<'de> de::Deserializer<'de> for ValueWithKey<'de> { #[inline] fn deserialize_option(self, visitor: V) -> Result - where V: de::Visitor<'de> + where + V: de::Visitor<'de>, { // Match an explicit nil as None and everything else as Some match self.0.kind { @@ -122,7 +124,8 @@ impl<'de> de::Deserializer<'de> for Value { #[inline] fn deserialize_any(self, visitor: V) -> Result - where V: de::Visitor<'de> + where + V: de::Visitor<'de>, { // Deserialize based on the underlying type match self.kind { @@ -210,7 +213,8 @@ impl<'de> de::Deserializer<'de> for Value { #[inline] fn deserialize_option(self, visitor: V) -> Result - where V: de::Visitor<'de> + where + V: de::Visitor<'de>, { // Match an explicit nil as None and everything else as Some match self.kind { @@ -265,7 +269,8 @@ impl<'de> de::SeqAccess<'de> for SeqAccess { type Error = ConfigError; fn next_element_seed(&mut self, seed: T) -> Result> - where T: de::DeserializeSeed<'de> + where + T: de::DeserializeSeed<'de>, { match self.elements.next() { Some(value) => seed.deserialize(value).map(Some), @@ -299,7 +304,8 @@ impl<'de> de::MapAccess<'de> for MapAccess { type Error = ConfigError; fn next_key_seed(&mut self, seed: K) -> Result> - where K: de::DeserializeSeed<'de> + where + K: de::DeserializeSeed<'de>, { if self.index >= self.elements.len() { return Ok(None); @@ -313,7 +319,8 @@ impl<'de> de::MapAccess<'de> for MapAccess { } fn next_value_seed(&mut self, seed: V) -> Result - where V: de::DeserializeSeed<'de> + where + V: de::DeserializeSeed<'de>, { de::DeserializeSeed::deserialize(seed, self.elements.remove(0).1) } diff --git a/src/env.rs b/src/env.rs index abadea2..aec4de4 100644 --- a/src/env.rs +++ b/src/env.rs @@ -88,8 +88,10 @@ impl Source for Environment { // Replace `separator` with `.` key = key.replace(&self.separator, "."); - m.insert(key.to_lowercase(), - Value::new(Some(&uri), ValueKind::String(value))); + m.insert( + key.to_lowercase(), + Value::new(Some(&uri), ValueKind::String(value)), + ); } Ok(m) diff --git a/src/error.rs b/src/error.rs index 514a5e2..eff3c7c 100644 --- a/src/error.rs +++ b/src/error.rs @@ -13,7 +13,7 @@ pub enum Unexpected { Str(String), Unit, Seq, - Map + Map, } impl fmt::Display for Unexpected { @@ -50,7 +50,7 @@ pub enum ConfigError { /// The captured error from attempting to parse the file in its desired format. /// This is the actual error object from the library used for the parsing. - cause: Box + cause: Box, }, /// Value could not be converted into the requested type. @@ -81,27 +81,34 @@ pub enum ConfigError { impl ConfigError { // FIXME: pub(crate) #[doc(hidden)] - pub fn invalid_type(origin: Option, unexpected: Unexpected, expected: &'static str) -> Self { + pub fn invalid_type( + origin: Option, + unexpected: Unexpected, + expected: &'static str, + ) -> Self { ConfigError::Type { origin: origin, unexpected: unexpected, expected: expected, key: None, - } + } } // FIXME: pub(crate) #[doc(hidden)] pub fn extend_with_key(self, key: &str) -> Self { match self { - ConfigError::Type { origin, unexpected, expected, .. } => { - ConfigError::Type { - origin: origin, - unexpected: unexpected, - expected: expected, - key: Some(key.into()), - } - } + ConfigError::Type { + origin, + unexpected, + expected, + .. + } => ConfigError::Type { + origin: origin, + unexpected: unexpected, + expected: expected, + key: Some(key.into()), + }, _ => self, } @@ -113,7 +120,7 @@ pub type Result = result::Result; // Forward Debug to Display for readable panic! messages impl fmt::Debug for ConfigError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", *self) } } @@ -121,25 +128,23 @@ impl fmt::Debug for ConfigError { impl fmt::Display for ConfigError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match *self { - ConfigError::Frozen | ConfigError::PathParse(_) => { - write!(f, "{}", self.description()) - } + ConfigError::Frozen | ConfigError::PathParse(_) => write!(f, "{}", self.description()), - ConfigError::Message(ref s) => { - write!(f, "{}", s) - } + ConfigError::Message(ref s) => write!(f, "{}", s), - ConfigError::Foreign(ref cause) => { - write!(f, "{}", cause) - } + ConfigError::Foreign(ref cause) => write!(f, "{}", cause), ConfigError::NotFound(ref key) => { write!(f, "configuration property {:?} not found", key) } - ConfigError::Type { ref origin, ref unexpected, expected, ref key } => { - write!(f, "invalid type: {}, expected {}", - unexpected, expected)?; + ConfigError::Type { + ref origin, + ref unexpected, + expected, + ref key, + } => { + write!(f, "invalid type: {}, expected {}", unexpected, expected)?; if let Some(ref key) = *key { write!(f, " for key `{}`", key)?; @@ -171,7 +176,9 @@ impl Error for ConfigError { ConfigError::Frozen => "configuration is frozen", ConfigError::NotFound(_) => "configuration property not found", ConfigError::Type { .. } => "invalid type", - ConfigError::Foreign(ref cause) | ConfigError::FileParse { ref cause, .. } => cause.description(), + ConfigError::Foreign(ref cause) | ConfigError::FileParse { ref cause, .. } => { + cause.description() + } ConfigError::PathParse(ref kind) => kind.description(), _ => "configuration error", @@ -180,9 +187,11 @@ impl Error for ConfigError { fn cause(&self) -> Option<&Error> { match *self { - ConfigError::Foreign(ref cause) | ConfigError::FileParse { ref cause, .. } => Some(cause.as_ref()), + ConfigError::Foreign(ref cause) | ConfigError::FileParse { ref cause, .. } => { + Some(cause.as_ref()) + } - _ => None + _ => None, } } } diff --git a/src/file/format/json.rs b/src/file/format/json.rs index ead99f8..caf62f5 100644 --- a/src/file/format/json.rs +++ b/src/file/format/json.rs @@ -4,7 +4,10 @@ use std::collections::HashMap; use std::error::Error; use value::{Value, ValueKind}; -pub fn parse(uri: Option<&String>, text: &str) -> Result, Box> { +pub fn parse( + uri: Option<&String>, + text: &str, +) -> Result, Box> { // Parse a JSON object value from the text // TODO: Have a proper error fire if the root of a file is ever not a Table let value = from_json_value(uri, &serde_json::from_str(text)?); @@ -19,15 +22,13 @@ fn from_json_value(uri: Option<&String>, value: &serde_json::Value) -> Value { match *value { serde_json::Value::String(ref value) => Value::new(uri, ValueKind::String(value.clone())), - serde_json::Value::Number(ref value) => { - if let Some(value) = value.as_i64() { - Value::new(uri, ValueKind::Integer(value)) - } else if let Some(value) = value.as_f64() { - Value::new(uri, ValueKind::Float(value)) - } else { - unreachable!(); - } - } + serde_json::Value::Number(ref value) => if let Some(value) = value.as_i64() { + Value::new(uri, ValueKind::Integer(value)) + } else if let Some(value) = value.as_f64() { + Value::new(uri, ValueKind::Float(value)) + } else { + unreachable!(); + }, serde_json::Value::Bool(value) => Value::new(uri, ValueKind::Boolean(value)), diff --git a/src/file/format/mod.rs b/src/file/format/mod.rs index 5aa1acb..a90dfda 100644 --- a/src/file/format/mod.rs +++ b/src/file/format/mod.rs @@ -63,10 +63,11 @@ impl FileFormat { // TODO: pub(crate) #[doc(hidden)] #[allow(unused_variables)] - pub fn parse(&self, - uri: Option<&String>, - text: &str) - -> Result, Box> { + pub fn parse( + &self, + uri: Option<&String>, + text: &str, + ) -> Result, Box> { match *self { #[cfg(feature = "toml")] FileFormat::Toml => toml::parse(uri, text), diff --git a/src/file/format/toml.rs b/src/file/format/toml.rs index 4307e48..9977126 100644 --- a/src/file/format/toml.rs +++ b/src/file/format/toml.rs @@ -1,10 +1,13 @@ use toml; use source::Source; -use std::collections::{HashMap, BTreeMap}; +use std::collections::{BTreeMap, HashMap}; use std::error::Error; use value::{Value, ValueKind}; -pub fn parse(uri: Option<&String>, text: &str) -> Result, Box> { +pub fn parse( + uri: Option<&String>, + text: &str, +) -> Result, Box> { // Parse a TOML value from the provided text // TODO: Have a proper error fire if the root of a file is ever not a Table let value = from_toml_value(uri, &toml::from_str(text)?); diff --git a/src/file/format/yaml.rs b/src/file/format/yaml.rs index 9bcf9d4..87240ac 100644 --- a/src/file/format/yaml.rs +++ b/src/file/format/yaml.rs @@ -6,7 +6,10 @@ use std::collections::HashMap; use std::mem; use value::{Value, ValueKind}; -pub fn parse(uri: Option<&String>, text: &str) -> Result, Box> { +pub fn parse( + uri: Option<&String>, + text: &str, +) -> Result, Box> { // Parse a YAML object from file let mut docs = yaml::YamlLoader::load_from_str(text)?; let root = match docs.len() { diff --git a/src/file/mod.rs b/src/file/mod.rs index 45deb92..6586ede 100644 --- a/src/file/mod.rs +++ b/src/file/mod.rs @@ -12,7 +12,8 @@ pub use self::format::FileFormat; #[derive(Clone, Debug)] pub struct File - where T: FileSource +where + T: FileSource, { source: T, @@ -86,8 +87,9 @@ impl File { } impl Source for File - where T: 'static, - T: Sync + Send +where + T: 'static, + T: Sync + Send, { fn clone_into_box(&self) -> Box { Box::new((*self).clone()) @@ -96,8 +98,9 @@ impl Source for File fn collect(&self) -> Result> { // Coerce the file contents to a string let (uri, contents, format) = match self.source - .resolve(self.format) - .map_err(|err| ConfigError::Foreign(err)) { + .resolve(self.format) + .map_err(|err| ConfigError::Foreign(err)) + { Ok((uri, contents, format)) => (uri, contents, format), Err(error) => { @@ -111,10 +114,10 @@ impl Source for File // Parse the string using the given format format.parse(uri.as_ref(), &contents).map_err(|cause| { - ConfigError::FileParse { - uri: uri, - cause: cause, - } - }) + ConfigError::FileParse { + uri: uri, + cause: cause, + } + }) } } diff --git a/src/file/source/file.rs b/src/file/source/file.rs index 7bf29be..80cd1dd 100644 --- a/src/file/source/file.rs +++ b/src/file/source/file.rs @@ -2,7 +2,7 @@ use std::str::FromStr; use std::result; use std::error::Error; -use std::path::{PathBuf, Path}; +use std::path::{Path, PathBuf}; use file::format::ALL_EXTENSIONS; use std::io::{self, Read}; use std::fs; @@ -24,9 +24,10 @@ impl FileSourceFile { FileSourceFile { name: name } } - fn find_file(&self, - format_hint: Option) - -> Result<(PathBuf, FileFormat), Box> { + fn find_file( + &self, + format_hint: Option, + ) -> Result<(PathBuf, FileFormat), Box> { // First check for an _exact_ match let mut filename = env::current_dir()?.as_path().join(self.name.clone()); if filename.is_file() { @@ -35,55 +36,61 @@ impl FileSourceFile { None => { for (format, extensions) in ALL_EXTENSIONS.iter() { if extensions.contains(&filename - .extension() - .unwrap_or_default() - .to_string_lossy() - .as_ref()) { + .extension() + .unwrap_or_default() + .to_string_lossy() + .as_ref()) + { return Ok((filename, *format)); } } - Err(Box::new(io::Error::new(io::ErrorKind::NotFound, - format!("configuration file \"{}\" is not of a registered file format", - filename.to_string_lossy())))) + Err(Box::new(io::Error::new( + io::ErrorKind::NotFound, + format!( + "configuration file \"{}\" is not of a registered file format", + filename.to_string_lossy() + ), + ))) } }; } match format_hint { - Some(format) => { - for ext in format.extensions() { - filename.set_extension(ext); + Some(format) => for ext in format.extensions() { + filename.set_extension(ext); - if filename.is_file() { - return Ok((filename, format)); - } + if filename.is_file() { + return Ok((filename, format)); } - } + }, - None => { - for (format, extensions) in ALL_EXTENSIONS.iter() { - for ext in format.extensions() { - filename.set_extension(ext); + None => for (format, extensions) in ALL_EXTENSIONS.iter() { + for ext in format.extensions() { + filename.set_extension(ext); - if filename.is_file() { - return Ok((filename, *format)); - } + if filename.is_file() { + return Ok((filename, *format)); } } - } + }, } - Err(Box::new(io::Error::new(io::ErrorKind::NotFound, - format!("configuration file \"{}\" not found", - self.name.to_string_lossy())))) + Err(Box::new(io::Error::new( + io::ErrorKind::NotFound, + format!( + "configuration file \"{}\" not found", + self.name.to_string_lossy() + ), + ))) } } impl FileSource for FileSourceFile { - fn resolve(&self, - format_hint: Option) - -> Result<(Option, String, FileFormat), Box> { + fn resolve( + &self, + format_hint: Option, + ) -> Result<(Option, String, FileFormat), Box> { // Find file let (filename, format) = self.find_file(format_hint)?; diff --git a/src/file/source/mod.rs b/src/file/source/mod.rs index f164d27..4d9950f 100644 --- a/src/file/source/mod.rs +++ b/src/file/source/mod.rs @@ -9,7 +9,8 @@ use super::FileFormat; /// Describes where the file is sourced pub trait FileSource: Debug + Clone { - fn resolve(&self, - format_hint: Option) - -> Result<(Option, String, FileFormat), Box>; + fn resolve( + &self, + format_hint: Option, + ) -> Result<(Option, String, FileFormat), Box>; } diff --git a/src/file/source/string.rs b/src/file/source/string.rs index 5dbd1b9..9c89231 100644 --- a/src/file/source/string.rs +++ b/src/file/source/string.rs @@ -3,7 +3,7 @@ use std::result; use std::error::Error; use source::Source; -use super::{FileSource, FileFormat}; +use super::{FileFormat, FileSource}; /// Describes a file sourced from a string #[derive(Clone, Debug)] @@ -16,9 +16,14 @@ impl<'a> From<&'a str> for FileSourceString { } impl FileSource for FileSourceString { - fn resolve(&self, - format_hint: Option) - -> Result<(Option, String, FileFormat), Box> { - Ok((None, self.0.clone(), format_hint.expect("from_str requires a set file format"))) + fn resolve( + &self, + format_hint: Option, + ) -> Result<(Option, String, FileFormat), Box> { + Ok(( + None, + self.0.clone(), + format_hint.expect("from_str requires a set file format"), + )) } } diff --git a/src/lib.rs b/src/lib.rs index c1fb07b..bed60b4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -15,7 +15,6 @@ //! //! See the [examples](https://github.com/mehcode/config-rs/tree/master/examples) for //! general usage information. - #![allow(dead_code)] #![allow(unused_imports)] #![allow(unused_variables)] diff --git a/src/path/mod.rs b/src/path/mod.rs index 046888e..258c4c4 100644 --- a/src/path/mod.rs +++ b/src/path/mod.rs @@ -58,87 +58,78 @@ impl Expression { } } - Expression::Subscript(expr, index) => { - match expr.get(root) { - Some(value) => { - match value.kind { - ValueKind::Array(ref array) => { - let index = sindex_to_uindex(index, array.len()); - - if index >= array.len() { - None - } else { - Some(&array[index]) - } - } + Expression::Subscript(expr, index) => match expr.get(root) { + Some(value) => match value.kind { + ValueKind::Array(ref array) => { + let index = sindex_to_uindex(index, array.len()); - _ => None, + if index >= array.len() { + None + } else { + Some(&array[index]) } } _ => None, - } - } + }, + + _ => None, + }, } } pub fn get_mut<'a>(&self, root: &'a mut Value) -> Option<&'a mut Value> { match *self { - Expression::Identifier(ref id) => { - match root.kind { - ValueKind::Table(ref mut map) => { - Some(map.entry(id.clone()).or_insert_with(|| Value::new(None, ValueKind::Nil))) - } - - _ => None, - } - } + Expression::Identifier(ref id) => match root.kind { + ValueKind::Table(ref mut map) => Some( + map.entry(id.clone()) + .or_insert_with(|| Value::new(None, ValueKind::Nil)), + ), + + _ => None, + }, + + Expression::Child(ref expr, ref key) => match expr.get_mut(root) { + Some(value) => match value.kind { + ValueKind::Table(ref mut map) => Some( + map.entry(key.clone()) + .or_insert_with(|| Value::new(None, ValueKind::Nil)), + ), - Expression::Child(ref expr, ref key) => { - match expr.get_mut(root) { - Some(value) => { - match value.kind { - ValueKind::Table(ref mut map) => { - Some(map.entry(key.clone()).or_insert_with(|| Value::new(None, ValueKind::Nil))) - } - - _ => { - *value = HashMap::::new().into(); + _ => { + *value = HashMap::::new().into(); - if let ValueKind::Table(ref mut map) = value.kind { - Some(map.entry(key.clone()).or_insert_with(|| Value::new(None, ValueKind::Nil))) - } else { - unreachable!(); - } - } + if let ValueKind::Table(ref mut map) = value.kind { + Some( + map.entry(key.clone()) + .or_insert_with(|| Value::new(None, ValueKind::Nil)), + ) + } else { + unreachable!(); } } + }, - _ => None, - } - } + _ => None, + }, - Expression::Subscript(ref expr, index) => { - match expr.get_mut(root) { - Some(value) => { - match value.kind { - ValueKind::Array(ref mut array) => { - let index = sindex_to_uindex(index, array.len()); - - if index >= array.len() { - array.resize((index + 1) as usize, Value::new(None, ValueKind::Nil)); - } + Expression::Subscript(ref expr, index) => match expr.get_mut(root) { + Some(value) => match value.kind { + ValueKind::Array(ref mut array) => { + let index = sindex_to_uindex(index, array.len()); - Some(&mut array[index]) - } - - _ => None, + if index >= array.len() { + array.resize((index + 1) as usize, Value::new(None, ValueKind::Nil)); } + + Some(&mut array[index]) } _ => None, - } - } + }, + + _ => None, + }, } } @@ -147,7 +138,7 @@ impl Expression { Expression::Identifier(ref id) => { // Ensure that root is a table match root.kind { - ValueKind::Table(_) => { } + ValueKind::Table(_) => {} _ => { *root = HashMap::::new().into(); @@ -158,7 +149,8 @@ impl Expression { ValueKind::Table(ref incoming_map) => { // Pull out another table let mut target = if let ValueKind::Table(ref mut map) = root.kind { - map.entry(id.clone()).or_insert_with(|| HashMap::::new().into()) + map.entry(id.clone()) + .or_insert_with(|| HashMap::::new().into()) } else { unreachable!(); }; @@ -202,7 +194,10 @@ impl Expression { let uindex = sindex_to_uindex(index, array.len()); if uindex >= array.len() { - array.resize((uindex + 1) as usize, Value::new(None, ValueKind::Nil)); + array.resize( + (uindex + 1) as usize, + Value::new(None, ValueKind::Nil), + ); } array[uindex] = value.clone(); diff --git a/src/source.rs b/src/source.rs index 9bf28c5..7f689a6 100644 --- a/src/source.rs +++ b/src/source.rs @@ -62,9 +62,10 @@ impl Source for Vec> { } impl Source for Vec - where T: Source + Sync + Send, - T: Clone, - T: 'static +where + T: Source + Sync + Send, + T: Clone, + T: 'static, { fn clone_into_box(&self) -> Box { Box::new((*self).clone()) diff --git a/src/value.rs b/src/value.rs index 7c29bb7..3b700cf 100644 --- a/src/value.rs +++ b/src/value.rs @@ -26,7 +26,8 @@ impl Default for ValueKind { } impl From> for ValueKind - where T: Into +where + T: Into, { fn from(value: Option) -> Self { match value { @@ -67,7 +68,8 @@ impl From for ValueKind { } impl From> for ValueKind - where T: Into +where + T: Into, { fn from(values: HashMap) -> Self { let mut r = HashMap::new(); @@ -81,7 +83,8 @@ impl From> for ValueKind } impl From> for ValueKind - where T: Into +where + T: Into, { fn from(values: Vec) -> Self { let mut l = Vec::new(); @@ -138,7 +141,8 @@ pub struct Value { impl Value { /// Create a new value instance that will remember its source uri. pub fn new(origin: Option<&String>, kind: V) -> Self - where V: Into + where + V: Into, { Value { origin: origin.cloned(), @@ -165,24 +169,30 @@ impl Value { "0" | "false" | "off" | "no" => Ok(false), // Unexpected string value - s => { - Err(ConfigError::invalid_type(self.origin.clone(), - Unexpected::Str(s.into()), - "a boolean")) - } + s => Err(ConfigError::invalid_type( + self.origin.clone(), + Unexpected::Str(s.into()), + "a boolean", + )), } } // Unexpected type - ValueKind::Nil => { - Err(ConfigError::invalid_type(self.origin.clone(), Unexpected::Unit, "a boolean")) - } - ValueKind::Table(_) => { - Err(ConfigError::invalid_type(self.origin.clone(), Unexpected::Map, "a boolean")) - } - ValueKind::Array(_) => { - Err(ConfigError::invalid_type(self.origin.clone(), Unexpected::Seq, "a boolean")) - } + ValueKind::Nil => Err(ConfigError::invalid_type( + self.origin.clone(), + Unexpected::Unit, + "a boolean", + )), + ValueKind::Table(_) => Err(ConfigError::invalid_type( + self.origin.clone(), + Unexpected::Map, + "a boolean", + )), + ValueKind::Array(_) => Err(ConfigError::invalid_type( + self.origin.clone(), + Unexpected::Seq, + "a boolean", + )), } } @@ -198,11 +208,13 @@ impl Value { "false" | "off" | "no" => Ok(0), _ => { s.parse().map_err(|_| { - // Unexpected string - ConfigError::invalid_type(self.origin.clone(), - Unexpected::Str(s.clone()), - "an integer") - }) + // Unexpected string + ConfigError::invalid_type( + self.origin.clone(), + Unexpected::Str(s.clone()), + "an integer", + ) + }) } } } @@ -211,15 +223,21 @@ impl Value { ValueKind::Float(value) => Ok(value.round() as i64), // Unexpected type - ValueKind::Nil => { - Err(ConfigError::invalid_type(self.origin.clone(), Unexpected::Unit, "an integer")) - } - ValueKind::Table(_) => { - Err(ConfigError::invalid_type(self.origin.clone(), Unexpected::Map, "an integer")) - } - ValueKind::Array(_) => { - Err(ConfigError::invalid_type(self.origin.clone(), Unexpected::Seq, "an integer")) - } + ValueKind::Nil => Err(ConfigError::invalid_type( + self.origin.clone(), + Unexpected::Unit, + "an integer", + )), + ValueKind::Table(_) => Err(ConfigError::invalid_type( + self.origin.clone(), + Unexpected::Map, + "an integer", + )), + ValueKind::Array(_) => Err(ConfigError::invalid_type( + self.origin.clone(), + Unexpected::Seq, + "an integer", + )), } } @@ -235,11 +253,13 @@ impl Value { "false" | "off" | "no" => Ok(0.0), _ => { s.parse().map_err(|_| { - // Unexpected string - ConfigError::invalid_type(self.origin.clone(), - Unexpected::Str(s.clone()), - "a floating point") - }) + // Unexpected string + ConfigError::invalid_type( + self.origin.clone(), + Unexpected::Str(s.clone()), + "a floating point", + ) + }) } } } @@ -248,21 +268,21 @@ impl Value { ValueKind::Boolean(value) => Ok(if value { 1.0 } else { 0.0 }), // Unexpected type - ValueKind::Nil => { - Err(ConfigError::invalid_type(self.origin.clone(), - Unexpected::Unit, - "a floating point")) - } - ValueKind::Table(_) => { - Err(ConfigError::invalid_type(self.origin.clone(), - Unexpected::Map, - "a floating point")) - } - ValueKind::Array(_) => { - Err(ConfigError::invalid_type(self.origin.clone(), - Unexpected::Seq, - "a floating point")) - } + ValueKind::Nil => Err(ConfigError::invalid_type( + self.origin.clone(), + Unexpected::Unit, + "a floating point", + )), + ValueKind::Table(_) => Err(ConfigError::invalid_type( + self.origin.clone(), + Unexpected::Map, + "a floating point", + )), + ValueKind::Array(_) => Err(ConfigError::invalid_type( + self.origin.clone(), + Unexpected::Seq, + "a floating point", + )), } } @@ -277,15 +297,21 @@ impl Value { ValueKind::Float(value) => Ok(value.to_string()), // Cannot convert - ValueKind::Nil => { - Err(ConfigError::invalid_type(self.origin, Unexpected::Unit, "a string")) - } - ValueKind::Table(_) => { - Err(ConfigError::invalid_type(self.origin, Unexpected::Map, "a string")) - } - ValueKind::Array(_) => { - Err(ConfigError::invalid_type(self.origin, Unexpected::Seq, "a string")) - } + ValueKind::Nil => Err(ConfigError::invalid_type( + self.origin, + Unexpected::Unit, + "a string", + )), + ValueKind::Table(_) => Err(ConfigError::invalid_type( + self.origin, + Unexpected::Map, + "a string", + )), + ValueKind::Array(_) => Err(ConfigError::invalid_type( + self.origin, + Unexpected::Seq, + "a string", + )), } } @@ -296,24 +322,36 @@ impl Value { ValueKind::Array(value) => Ok(value), // Cannot convert - ValueKind::Float(value) => { - Err(ConfigError::invalid_type(self.origin, Unexpected::Float(value), "an array")) - } - ValueKind::String(value) => { - Err(ConfigError::invalid_type(self.origin, Unexpected::Str(value), "an array")) - } - ValueKind::Integer(value) => { - Err(ConfigError::invalid_type(self.origin, Unexpected::Integer(value), "an array")) - } - ValueKind::Boolean(value) => { - Err(ConfigError::invalid_type(self.origin, Unexpected::Bool(value), "an array")) - } - ValueKind::Nil => { - Err(ConfigError::invalid_type(self.origin, Unexpected::Unit, "an array")) - } - ValueKind::Table(_) => { - Err(ConfigError::invalid_type(self.origin, Unexpected::Map, "an array")) - } + ValueKind::Float(value) => Err(ConfigError::invalid_type( + self.origin, + Unexpected::Float(value), + "an array", + )), + ValueKind::String(value) => Err(ConfigError::invalid_type( + self.origin, + Unexpected::Str(value), + "an array", + )), + ValueKind::Integer(value) => Err(ConfigError::invalid_type( + self.origin, + Unexpected::Integer(value), + "an array", + )), + ValueKind::Boolean(value) => Err(ConfigError::invalid_type( + self.origin, + Unexpected::Bool(value), + "an array", + )), + ValueKind::Nil => Err(ConfigError::invalid_type( + self.origin, + Unexpected::Unit, + "an array", + )), + ValueKind::Table(_) => Err(ConfigError::invalid_type( + self.origin, + Unexpected::Map, + "an array", + )), } } @@ -324,24 +362,36 @@ impl Value { ValueKind::Table(value) => Ok(value), // Cannot convert - ValueKind::Float(value) => { - Err(ConfigError::invalid_type(self.origin, Unexpected::Float(value), "a map")) - } - ValueKind::String(value) => { - Err(ConfigError::invalid_type(self.origin, Unexpected::Str(value), "a map")) - } - ValueKind::Integer(value) => { - Err(ConfigError::invalid_type(self.origin, Unexpected::Integer(value), "a map")) - } - ValueKind::Boolean(value) => { - Err(ConfigError::invalid_type(self.origin, Unexpected::Bool(value), "a map")) - } - ValueKind::Nil => { - Err(ConfigError::invalid_type(self.origin, Unexpected::Unit, "a map")) - } - ValueKind::Array(_) => { - Err(ConfigError::invalid_type(self.origin, Unexpected::Seq, "a map")) - } + ValueKind::Float(value) => Err(ConfigError::invalid_type( + self.origin, + Unexpected::Float(value), + "a map", + )), + ValueKind::String(value) => Err(ConfigError::invalid_type( + self.origin, + Unexpected::Str(value), + "a map", + )), + ValueKind::Integer(value) => Err(ConfigError::invalid_type( + self.origin, + Unexpected::Integer(value), + "a map", + )), + ValueKind::Boolean(value) => Err(ConfigError::invalid_type( + self.origin, + Unexpected::Bool(value), + "a map", + )), + ValueKind::Nil => Err(ConfigError::invalid_type( + self.origin, + Unexpected::Unit, + "a map", + )), + ValueKind::Array(_) => Err(ConfigError::invalid_type( + self.origin, + Unexpected::Seq, + "a map", + )), } } } @@ -349,7 +399,8 @@ impl Value { impl<'de> Deserialize<'de> for Value { #[inline] fn deserialize(deserializer: D) -> ::std::result::Result - where D: Deserializer<'de> + where + D: Deserializer<'de>, { struct ValueVisitor; @@ -413,7 +464,8 @@ impl<'de> Deserialize<'de> for Value { #[inline] fn visit_str(self, value: &str) -> ::std::result::Result - where E: ::serde::de::Error + where + E: ::serde::de::Error, { self.visit_string(String::from(value)) } @@ -430,7 +482,8 @@ impl<'de> Deserialize<'de> for Value { #[inline] fn visit_some(self, deserializer: D) -> ::std::result::Result - where D: Deserializer<'de> + where + D: Deserializer<'de>, { Deserialize::deserialize(deserializer) } @@ -442,7 +495,8 @@ impl<'de> Deserialize<'de> for Value { #[inline] fn visit_seq(self, mut visitor: V) -> ::std::result::Result - where V: ::serde::de::SeqAccess<'de> + where + V: ::serde::de::SeqAccess<'de>, { let mut vec = Array::new(); @@ -454,7 +508,8 @@ impl<'de> Deserialize<'de> for Value { } fn visit_map(self, mut visitor: V) -> ::std::result::Result - where V: ::serde::de::MapAccess<'de> + where + V: ::serde::de::MapAccess<'de>, { let mut values = Table::new(); @@ -471,7 +526,8 @@ impl<'de> Deserialize<'de> for Value { } impl From for Value - where T: Into +where + T: Into, { fn from(value: T) -> Self { Value { diff --git a/tests/datetime.rs b/tests/datetime.rs index f2dc9fe..8a50c01 100644 --- a/tests/datetime.rs +++ b/tests/datetime.rs @@ -2,7 +2,7 @@ extern crate config; extern crate chrono; use config::*; -use chrono::{DateTime, Utc, TimeZone}; +use chrono::{DateTime, TimeZone, Utc}; fn make() -> Config { Config::default() @@ -14,12 +14,14 @@ fn make() -> Config { "#, FileFormat::Json, )) + .unwrap() .merge(File::from_str( r#" yaml_datetime: 2017-06-12T10:58:30Z "#, FileFormat::Yaml, )) + .unwrap() .merge(File::from_str( r#" toml_datetime = 2017-05-11T14:55:15Z @@ -27,6 +29,7 @@ fn make() -> Config { FileFormat::Toml, )) .unwrap() + .clone() } #[test] diff --git a/tests/errors.rs b/tests/errors.rs index 835ead1..77a58ab 100644 --- a/tests/errors.rs +++ b/tests/errors.rs @@ -16,8 +16,10 @@ fn test_error_parse() { let res = c.merge(File::new("tests/Settings-invalid", FileFormat::Toml)); assert!(res.is_err()); - assert_eq!(res.unwrap_err().to_string(), - "invalid number at line 2 in tests/Settings-invalid.toml".to_string()); + assert_eq!( + res.unwrap_err().to_string(), + "invalid number at line 2 in tests/Settings-invalid.toml".to_string() + ); } #[test] @@ -27,9 +29,12 @@ fn test_error_type() { let res = c.get::("boolean_s_parse"); assert!(res.is_err()); - assert_eq!(res.unwrap_err().to_string(), - "invalid type: string \"fals\", expected a boolean for key `boolean_s_parse` in tests/Settings.toml" - .to_string()); + assert_eq!( + res.unwrap_err().to_string(), + "invalid type: string \"fals\", expected a boolean for key \ + `boolean_s_parse` in tests/Settings.toml" + .to_string() + ); } #[test] @@ -40,6 +45,8 @@ fn test_error_type_detached() { let res = value.try_into::(); assert!(res.is_err()); - assert_eq!(res.unwrap_err().to_string(), - "invalid type: string \"fals\", expected a boolean".to_string()); + assert_eq!( + res.unwrap_err().to_string(), + "invalid type: string \"fals\", expected a boolean".to_string() + ); } diff --git a/tests/file.rs b/tests/file.rs index 04a7361..5f49e7f 100644 --- a/tests/file.rs +++ b/tests/file.rs @@ -5,7 +5,9 @@ use config::*; #[test] fn test_file_not_required() { let mut c = Config::default(); - let res = c.merge(File::new("tests/NoSettings", FileFormat::Yaml).required(false)); + let res = c.merge( + File::new("tests/NoSettings", FileFormat::Yaml).required(false), + ); assert!(res.is_ok()); } @@ -16,15 +18,17 @@ fn test_file_required_not_found() { let res = c.merge(File::new("tests/NoSettings", FileFormat::Yaml)); assert!(res.is_err()); - assert_eq!(res.unwrap_err().to_string(), - "configuration file \"tests/NoSettings\" not found" - .to_string()); + assert_eq!( + res.unwrap_err().to_string(), + "configuration file \"tests/NoSettings\" not found".to_string() + ); } #[test] fn test_file_auto() { let mut c = Config::default(); - c.merge(File::with_name("tests/Settings-production")).unwrap(); + c.merge(File::with_name("tests/Settings-production")) + .unwrap(); assert_eq!(c.get("debug").ok(), Some(false)); assert_eq!(c.get("production").ok(), Some(true)); @@ -36,9 +40,10 @@ fn test_file_auto_not_found() { let res = c.merge(File::with_name("tests/NoSettings")); assert!(res.is_err()); - assert_eq!(res.unwrap_err().to_string(), - "configuration file \"tests/NoSettings\" not found" - .to_string()); + assert_eq!( + res.unwrap_err().to_string(), + "configuration file \"tests/NoSettings\" not found".to_string() + ); } #[test] diff --git a/tests/file_json.rs b/tests/file_json.rs index 983be86..1d35cae 100644 --- a/tests/file_json.rs +++ b/tests/