diff options
author | Ryan Leckey <leckey.ryan@gmail.com> | 2018-07-02 15:33:38 -0700 |
---|---|---|
committer | Ryan Leckey <leckey.ryan@gmail.com> | 2018-07-02 15:33:38 -0700 |
commit | cbb9ef88ea6f6452614dd8bbffce7203b1358a55 (patch) | |
tree | fd4f67587f9a36c517a3a3f1ff7ef727697d5fef /src | |
parent | bbc78d85610dec79aa4af4dd360131c7880efb5f (diff) |
Run rustfmt (nightly)
Diffstat (limited to 'src')
-rw-r--r-- | src/config.rs | 12 | ||||
-rw-r--r-- | src/de.rs | 12 | ||||
-rw-r--r-- | src/env.rs | 11 | ||||
-rw-r--r-- | src/error.rs | 10 | ||||
-rw-r--r-- | src/file/format/hjson.rs | 2 | ||||
-rw-r--r-- | src/file/format/ini.rs | 19 | ||||
-rw-r--r-- | src/file/format/mod.rs | 5 | ||||
-rw-r--r-- | src/file/format/toml.rs | 2 | ||||
-rw-r--r-- | src/file/format/yaml.rs | 4 | ||||
-rw-r--r-- | src/file/mod.rs | 17 | ||||
-rw-r--r-- | src/file/source/file.rs | 25 | ||||
-rw-r--r-- | src/file/source/mod.rs | 4 | ||||
-rw-r--r-- | src/file/source/string.rs | 6 | ||||
-rw-r--r-- | src/lib.rs | 18 | ||||
-rw-r--r-- | src/path/mod.rs | 155 | ||||
-rw-r--r-- | src/path/parser.rs | 53 | ||||
-rw-r--r-- | src/ser.rs | 235 | ||||
-rw-r--r-- | src/source.rs | 4 | ||||
-rw-r--r-- | src/value.rs | 6 |
19 files changed, 341 insertions, 259 deletions
diff --git a/src/config.rs b/src/config.rs index 60f8551..f19e59a 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,16 +1,16 @@ +use serde::de::{Deserialize, Deserializer}; +use serde::ser::{Serialize, Serializer}; use std::collections::HashMap; +use std::fmt::Debug; use std::ops::Deref; use std::str::FromStr; -use std::fmt::Debug; -use serde::de::{Deserialize, Deserializer}; -use serde::ser::{Serialize, Serializer}; use error::*; -use source::Source; use ser::ConfigSerializer; +use source::Source; -use value::{Value, ValueKind, ValueWithKey}; use path; +use value::{Value, ValueKind, ValueWithKey}; #[derive(Clone, Debug)] enum ConfigKind { @@ -199,7 +199,7 @@ impl Config { Ok(serializer.output) } - #[deprecated(since="0.7.0", note="please use 'try_into' instead")] + #[deprecated(since = "0.7.0", note = "please use 'try_into' instead")] pub fn deserialize<'de, T: Deserialize<'de>>(self) -> Result<T> { self.try_into() } @@ -1,11 +1,11 @@ -use serde::de; -use value::{Value, ValueKind, ValueWithKey}; -use error::*; use config::Config; +use error::*; +use serde::de; use std::borrow::Cow; -use std::iter::Peekable; -use std::collections::HashMap; use std::collections::hash_map::Drain; +use std::collections::HashMap; +use std::iter::Peekable; +use value::{Value, ValueKind, ValueWithKey}; // TODO: Use a macro or some other magic to reduce the code duplication here @@ -226,7 +226,7 @@ impl<'de> de::Deserializer<'de> for Value { fn deserialize_newtype_struct<V>(self, _name: &'static str, visitor: V) -> Result<V::Value> where - V: de::Visitor<'de> + V: de::Visitor<'de>, { visitor.visit_newtype_struct(self) } @@ -1,7 +1,7 @@ -use std::env; -use std::collections::HashMap; use error::*; use source::Source; +use std::collections::HashMap; +use std::env; use value::{Value, ValueKind}; #[derive(Clone, Debug)] @@ -64,7 +64,7 @@ impl Source for Environment { let separator = match self.separator { Some(ref separator) => separator, - _ => "" + _ => "", }; // Define a prefix pattern to test and exclude from keys @@ -78,7 +78,10 @@ impl Source for Environment { // Check for prefix if let Some(ref prefix_pattern) = prefix_pattern { - if key.to_lowercase().starts_with(&prefix_pattern.to_lowercase()) { + if key + .to_lowercase() + .starts_with(&prefix_pattern.to_lowercase()) + { // Remove this prefix from the key key = key[prefix_pattern.len()..].to_string(); } else { diff --git a/src/error.rs b/src/error.rs index 56c827a..1d348b6 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,10 +1,10 @@ -use std::error::Error; -use std::borrow::Cow; -use std::result; -use std::fmt; +use nom; use serde::de; use serde::ser; -use nom; +use std::borrow::Cow; +use std::error::Error; +use std::fmt; +use std::result; #[derive(Debug)] pub enum Unexpected { diff --git a/src/file/format/hjson.rs b/src/file/format/hjson.rs index 3d4ad1b..bb21e38 100644 --- a/src/file/format/hjson.rs +++ b/src/file/format/hjson.rs @@ -27,7 +27,7 @@ fn from_hjson_value(uri: Option<&String>, value: &serde_hjson::Value) -> Value { serde_hjson::Value::U64(value) => Value::new(uri, ValueKind::Integer(value as i64)), serde_hjson::Value::F64(value) => Value::new(uri, ValueKind::Float(value)), - + serde_hjson::Value::Bool(value) => Value::new(uri, ValueKind::Boolean(value)), serde_hjson::Value::Object(ref table) => { diff --git a/src/file/format/ini.rs b/src/file/format/ini.rs index b7c0f71..b4b4ada 100644 --- a/src/file/format/ini.rs +++ b/src/file/format/ini.rs @@ -1,8 +1,8 @@ +use ini::Ini; use source::Source; use std::collections::HashMap; use std::error::Error; use value::{Value, ValueKind}; -use ini::Ini; pub fn parse( uri: Option<&String>, @@ -15,15 +15,22 @@ pub fn parse( Some(ref sec) => { let mut sec_map: HashMap<String, Value> = HashMap::new(); for (k, v) in prop.iter() { - sec_map.insert(k.to_lowercase().clone(), - Value::new(uri, ValueKind::String(v.clone()))); + sec_map.insert( + k.to_lowercase().clone(), + Value::new(uri, ValueKind::String(v.clone())), + ); } - map.insert(sec.to_lowercase().clone(), Value::new(uri, ValueKind::Table(sec_map))); + map.insert( + sec.to_lowercase().clone(), + Value::new(uri, ValueKind::Table(sec_map)), + ); } None => { for (k, v) in prop.iter() { - map.insert(k.to_lowercase().clone(), - Value::new(uri, ValueKind::String(v.clone()))); + map.insert( + k.to_lowercase().clone(), + Value::new(uri, ValueKind::String(v.clone())), + ); } } } diff --git a/src/file/format/mod.rs b/src/file/format/mod.rs index c01c386..39b3813 100644 --- a/src/file/format/mod.rs +++ b/src/file/format/mod.rs @@ -3,9 +3,9 @@ #![allow(unused_mut)] use source::Source; -use value::Value; -use std::error::Error; use std::collections::HashMap; +use std::error::Error; +use value::Value; #[cfg(feature = "toml")] mod toml; @@ -42,7 +42,6 @@ pub enum FileFormat { /// INI (parsed with rust_ini) #[cfg(feature = "ini")] Ini, - } lazy_static! { diff --git a/src/file/format/toml.rs b/src/file/format/toml.rs index 9977126..da7782f 100644 --- a/src/file/format/toml.rs +++ b/src/file/format/toml.rs @@ -1,7 +1,7 @@ -use toml; use source::Source; use std::collections::{BTreeMap, HashMap}; use std::error::Error; +use toml; use value::{Value, ValueKind}; pub fn parse( diff --git a/src/file/format/yaml.rs b/src/file/format/yaml.rs index 87240ac..5ec8ca6 100644 --- a/src/file/format/yaml.rs +++ b/src/file/format/yaml.rs @@ -1,10 +1,10 @@ -use yaml_rust as yaml; use source::Source; +use std::collections::HashMap; use std::error::Error; use std::fmt; -use std::collections::HashMap; use std::mem; use value::{Value, ValueKind}; +use yaml_rust as yaml; pub fn parse( uri: Option<&String>, diff --git a/src/file/mod.rs b/src/file/mod.rs index 6586ede..fe4f5b4 100644 --- a/src/file/mod.rs +++ b/src/file/mod.rs @@ -1,14 +1,14 @@ mod format; pub mod source; -use source::Source; use error::*; -use value::Value; +use source::Source; use std::collections::HashMap; use std::path::{Path, PathBuf}; +use value::Value; -use self::source::FileSource; pub use self::format::FileFormat; +use self::source::FileSource; #[derive(Clone, Debug)] pub struct File<T> @@ -97,7 +97,8 @@ where fn collect(&self) -> Result<HashMap<String, Value>> { // Coerce the file contents to a string - let (uri, contents, format) = match self.source + let (uri, contents, format) = match self + .source .resolve(self.format) .map_err(|err| ConfigError::Foreign(err)) { @@ -113,11 +114,11 @@ where }; // Parse the string using the given format - format.parse(uri.as_ref(), &contents).map_err(|cause| { - ConfigError::FileParse { + format + .parse(uri.as_ref(), &contents) + .map_err(|cause| ConfigError::FileParse { uri: uri, cause: cause, - } - }) + }) } } diff --git a/src/file/source/file.rs b/src/file/source/file.rs index 80cd1dd..a413a1f 100644 --- a/src/file/source/file.rs +++ b/src/file/source/file.rs @@ -1,16 +1,16 @@ -use std::str::FromStr; -use std::result; use std::error::Error; +use std::result; +use std::str::FromStr; -use std::path::{Path, PathBuf}; use file::format::ALL_EXTENSIONS; -use std::io::{self, Read}; -use std::fs; use std::env; +use std::fs; +use std::io::{self, Read}; use std::iter::Iterator; +use std::path::{Path, PathBuf}; -use source::Source; use super::{FileFormat, FileSource}; +use source::Source; /// Describes a file sourced from a file #[derive(Clone, Debug)] @@ -35,12 +35,13 @@ impl FileSourceFile { Some(format) => Ok((filename, format)), None => { for (format, extensions) in ALL_EXTENSIONS.iter() { - if extensions.contains(&filename - .extension() - .unwrap_or_default() - .to_string_lossy() - .as_ref()) - { + if extensions.contains( + &filename + .extension() + .unwrap_or_default() + .to_string_lossy() + .as_ref(), + ) { return Ok((filename, *format)); } } diff --git a/src/file/source/mod.rs b/src/file/source/mod.rs index 4d9950f..5da69f1 100644 --- a/src/file/source/mod.rs +++ b/src/file/source/mod.rs @@ -1,11 +1,11 @@ pub mod file; pub mod string; -use std::fmt::Debug; use std::error::Error; +use std::fmt::Debug; -use source::Source; use super::FileFormat; +use source::Source; /// Describes where the file is sourced pub trait FileSource: Debug + Clone { diff --git a/src/file/source/string.rs b/src/file/source/string.rs index 9c89231..a2f66cb 100644 --- a/src/file/source/string.rs +++ b/src/file/source/string.rs @@ -1,9 +1,9 @@ -use std::str::FromStr; -use std::result; use std::error::Error; +use std::result; +use std::str::FromStr; -use source::Source; use super::{FileFormat, FileSource}; +use source::Source; /// Describes a file sourced from a string #[derive(Clone, Debug)] @@ -51,19 +51,19 @@ extern crate serde_hjson; #[cfg(feature = "ini")] extern crate ini; -mod error; -mod value; +mod config; mod de; -mod ser; +mod env; +mod error; +mod file; mod path; +mod ser; mod source; -mod config; -mod file; -mod env; +mod value; pub use config::Config; +pub use env::Environment; pub use error::ConfigError; -pub use value::Value; -pub use source::Source; pub use file::{File, FileFormat}; -pub use env::Environment; +pub use source::Source; +pub use value::Value; diff --git a/src/path/mod.rs b/src/path/mod.rs index f042992..7fe6e44 100644 --- a/src/path/mod.rs +++ b/src/path/mod.rs @@ -1,7 +1,7 @@ -use std::str::FromStr; -use std::collections::HashMap; -use nom::ErrorKind; use error::*; +use nom::ErrorKind; +use std::collections::HashMap; +use std::str::FromStr; use value::{Value, ValueKind}; mod parser; @@ -58,111 +58,97 @@ 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) => map.get_mut(id), + Expression::Identifier(ref id) => match root.kind { + ValueKind::Table(ref mut map) => map.get_mut(id), - _ => None, - } + _ => None, }, - Expression::Child(ref expr, ref key) => { - match expr.get_mut(root) { - Some(value) => { - match value.kind { - ValueKind::Table(ref mut map) => map.get_mut(key), - - _ => None - } - }, + Expression::Child(ref expr, ref key) => match expr.get_mut(root) { + Some(value) => match value.kind { + ValueKind::Table(ref mut map) => map.get_mut(key), _ => 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()); + 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() { - None - } else { - Some(&mut array[index]) - } + if index >= array.len() { + None + } else { + Some(&mut array[index]) } - - _ => None, - }, + } _ => None, - } + }, + + _ => None, }, } } pub fn get_mut_forcibly<'a>(&self, root: &'a mut Value) -> Option<&'a mut Value> { match *self { - Expression::Identifier(ref id) => { - match root.kind { + 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_forcibly(root) { + Some(value) => match value.kind { ValueKind::Table(ref mut map) => Some( - map.entry(id.clone()) + map.entry(key.clone()) .or_insert_with(|| Value::new(None, ValueKind::Nil)), ), - _ => None, - } - }, - - Expression::Child(ref expr, ref key) => { - match expr.get_mut_forcibly(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::<String, Value>::new().into(); - _ => { - *value = HashMap::<String, Value>::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) => { @@ -171,7 +157,7 @@ impl Expression { Some(value) => { match value.kind { ValueKind::Array(_) => (), - _ => *value = Vec::<Value>::new().into() + _ => *value = Vec::<Value>::new().into(), } match value.kind { @@ -179,18 +165,21 @@ impl Expression { let index = sindex_to_uindex(index, array.len()); if index >= array.len() { - array.resize((index + 1) as usize, Value::new(None, ValueKind::Nil)); + array.resize( + (index + 1) as usize, + Value::new(None, ValueKind::Nil), + ); } Some(&mut array[index]) } - _ => None + _ => None, } - }, - _ => None + } + _ => None, } - }, + } } } @@ -252,7 +241,7 @@ impl Expression { if let Some(parent) = expr.get_mut_forcibly(root) { match parent.kind { ValueKind::Array(_) => (), - _ => *parent = Vec::<Value>::new().into() + _ => *parent = Vec::<Value>::new().into(), } match parent.kind { @@ -268,7 +257,7 @@ impl Expression { array[uindex] = value.clone(); } - _ => () + _ => (), } } } diff --git a/src/path/parser.rs b/src/path/parser.rs index 1ecd6e7..a1660ca 100644 --- a/src/path/parser.rs +++ b/src/path/parser.rs @@ -1,7 +1,7 @@ -use nom::{ErrorKind, digit, IResult}; -use nom::types::CompleteStr; -use std::str::{FromStr, from_utf8}; use super::Expression; +use nom::types::CompleteStr; +use nom::{digit, ErrorKind, IResult}; +use std::str::{from_utf8, FromStr}; named!(raw_ident<CompleteStr, String>, map!(is_a!( @@ -28,24 +28,20 @@ named!(ident<CompleteStr, Expression>, map!(raw_ident, Expression::Identifier)); #[allow(cyclomatic_complexity)] fn postfix(expr: Expression) -> Box<Fn(CompleteStr) -> IResult<CompleteStr, Expression>> { Box::new(move |i: CompleteStr| { - alt!(i, - do_parse!( - tag!(".") >> - id: raw_ident >> - (Expression::Child(Box::new(expr.clone()), id)) - ) | - delimited!( - char!('['), - do_parse!( - negative: opt!(tag!("-")) >> - num: integer >> - (Expression::Subscript( - Box::new(expr.clone()), - num * (if negative.is_none() { 1 } else { -1 }) - )) - ), - char!(']') - ) + alt!( + i, + do_parse!(tag!(".") >> id: raw_ident >> (Expression::Child(Box::new(expr.clone()), id))) + | delimited!( + char!('['), + do_parse!( + negative: opt!(tag!("-")) >> num: integer + >> (Expression::Subscript( + Box::new(expr.clone()), + num * (if negative.is_none() { 1 } else { -1 }), + )) + ), + char!(']') + ) ) }) } @@ -62,7 +58,7 @@ pub fn from_str(input: &str) -> Result<Expression, ErrorKind> { // Forward Incomplete and Error result => { - return result.map(|(_,o)| o).map_err(|e| e.into_error_kind()); + return result.map(|(_, o)| o).map_err(|e| e.into_error_kind()); } } } @@ -71,14 +67,14 @@ pub fn from_str(input: &str) -> Result<Expression, ErrorKind> { } // Forward Incomplete and Error - result => result.map(|(_,o)| o).map_err(|e| e.into_error_kind()), + result => result.map(|(_, o)| o).map_err(|e| e.into_error_kind()), } } #[cfg(test)] mod test { - use super::*; use super::Expression::*; + use super::*; #[test] fn test_id() { @@ -100,11 +96,10 @@ mod test { assert_eq!(parsed, expected); let parsed: Expression = from_str("abcd.efgh.ijkl").unwrap(); - let expected = Child(Box::new( - Child(Box::new( - Identifier("abcd".into()) - ), "efgh".into()) - ), "ijkl".into()); + let expected = Child( + Box::new(Child(Box::new(Identifier("abcd".into())), "efgh".into())), + "ijkl".into(), + ); assert_eq!(parsed, expected); } @@ -1,10 +1,10 @@ -use std::mem; -use std::fmt::Display; use serde::ser; +use std::fmt::Display; +use std::mem; -use Config; -use value::{Value, ValueKind, ValueWithKey}; use error::*; +use value::{Value, ValueKind, ValueWithKey}; +use Config; #[derive(Default, Debug)] pub struct ConfigSerializer { @@ -14,12 +14,18 @@ pub struct ConfigSerializer { impl ConfigSerializer { fn serialize_primitive<T>(&mut self, value: T) -> Result<()> - where T: Into<Value> + Display + where + T: Into<Value> + Display, { let key = match self.last_key_index_pair() { Some((key, Some(index))) => format!("{}[{}]", key, index), Some((key, None)) => key.to_string(), - None => return Err(ConfigError::Message(format!("key is not found for value {}", value))) + None => { + return Err(ConfigError::Message(format!( + "key is not found for value {}", + value + ))) + } }; self.output.set(&key, value.into())?; Ok(()) @@ -28,7 +34,8 @@ impl ConfigSerializer { fn last_key_index_pair(&self) -> Option<(&str, Option<usize>)> { let len = self.keys.len(); if len > 0 { - self.keys.get(len - 1) + self.keys + .get(len - 1) .map(|&(ref key, opt)| (key.as_str(), opt)) } else { None @@ -38,13 +45,13 @@ impl ConfigSerializer { fn inc_last_key_index(&mut self) -> Result<()> { let len = self.keys.len(); if len > 0 { - self.keys.get_mut(len - 1) - .map(|pair| { - pair.1 = pair.1.map(|i| i + 1).or(Some(0)) - }) - .ok_or(ConfigError::Message( - format!("last key is not found in {} keys", len) - )) + self.keys + .get_mut(len - 1) + .map(|pair| pair.1 = pair.1.map(|i| i + 1).or(Some(0))) + .ok_or(ConfigError::Message(format!( + "last key is not found in {} keys", + len + ))) } else { Err(ConfigError::Message("keys is empty".to_string())) } @@ -58,7 +65,7 @@ impl ConfigSerializer { format!("{}[{}].{}", prev_key, index, key) } else { format!("{}.{}", prev_key, key) - } + }; } } key.to_string() @@ -119,9 +126,11 @@ impl<'a> ser::Serializer for &'a mut ConfigSerializer { fn serialize_u64(self, v: u64) -> Result<Self::Ok> { if v > (i64::max_value() as u64) { - Err(ConfigError::Message( - format!("value {} is greater than the max {}", v, i64::max_value()) - )) + Err(ConfigError::Message(format!( + "value {} is greater than the max {}", + v, + i64::max_value() + ))) } else { self.serialize_i64(v as i64) } @@ -157,7 +166,8 @@ impl<'a> ser::Serializer for &'a mut ConfigSerializer { } fn serialize_some<T>(self, value: &T) -> Result<Self::Ok> - where T: ?Sized + ser::Serialize + where + T: ?Sized + ser::Serialize, { value.serialize(self) } @@ -170,21 +180,31 @@ impl<'a> ser::Serializer for &'a mut ConfigSerializer { self.serialize_unit() } - fn serialize_unit_variant(self, _name: &'static str, _variant_index: u32, variant: &'static str) - -> Result<Self::Ok> - { + fn serialize_unit_variant( + self, + _name: &'static str, + _variant_index: u32, + variant: &'static str, + ) -> Result<Self::Ok> { self.serialize_str(&variant.to_lowercase()) } fn serialize_newtype_struct<T>(self, _name: &'static str, value: &T) -> Result<Self::Ok> - where T: ?Sized + ser::Serialize + where + T: ?Sized + ser::Serialize, { value.serialize(self) } - fn serialize_newtype_variant<T>(self, _name: &'static str, _variant_index: u32, variant: &'static str, value: &T) - -> Result<Self::Ok> - where T: ?Sized + ser::Serialize + fn serialize_newtype_variant<T>( + self, + _name: &'static str, + _variant_index: u32, + variant: &'static str, + value: &T, + ) -> Result<Self::Ok> + where + T: ?Sized + ser::Serialize, { self.push_key(&variant.to_lowercase()); value.serialize(&mut *self)?; @@ -200,15 +220,21 @@ impl<'a> ser::Serializer for &'a mut ConfigSerializer { self.serialize_seq(Some(len)) } - f |