From cbb9ef88ea6f6452614dd8bbffce7203b1358a55 Mon Sep 17 00:00:00 2001 From: Ryan Leckey Date: Mon, 2 Jul 2018 15:33:38 -0700 Subject: Run rustfmt (nightly) --- src/config.rs | 12 +-- src/de.rs | 12 +-- src/env.rs | 11 ++- src/error.rs | 10 +- src/file/format/hjson.rs | 2 +- src/file/format/ini.rs | 19 ++-- src/file/format/mod.rs | 5 +- src/file/format/toml.rs | 2 +- src/file/format/yaml.rs | 4 +- src/file/mod.rs | 17 ++-- src/file/source/file.rs | 25 ++--- src/file/source/mod.rs | 4 +- src/file/source/string.rs | 6 +- src/lib.rs | 18 ++-- src/path/mod.rs | 155 ++++++++++++++---------------- src/path/parser.rs | 53 +++++------ src/ser.rs | 235 +++++++++++++++++++++++++++++++--------------- src/source.rs | 4 +- src/value.rs | 6 +- 19 files changed, 341 insertions(+), 259 deletions(-) (limited to 'src') 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 { self.try_into() } diff --git a/src/de.rs b/src/de.rs index 89c21e0..89d0c0c 100644 --- a/src/de.rs +++ b/src/de.rs @@ -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(self, _name: &'static str, visitor: V) -> Result where - V: de::Visitor<'de> + V: de::Visitor<'de>, { visitor.visit_newtype_struct(self) } diff --git a/src/env.rs b/src/env.rs index b26dc56..5beead5 100644 --- a/src/env.rs +++ b/src/env.rs @@ -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 = 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 @@ -97,7 +97,8 @@ where fn collect(&self) -> Result> { // 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)] diff --git a/src/lib.rs b/src/lib.rs index f18d937..9561cf1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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::::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) => { @@ -171,7 +157,7 @@ impl Expression { Some(value) => { match value.kind { ValueKind::Array(_) => (), - _ => *value = Vec::::new().into() + _ => *value = Vec::::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::::new().into() + _ => *parent = Vec::::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, map!(is_a!( @@ -28,24 +28,20 @@ named!(ident, map!(raw_ident, Expression::Identifier)); #[allow(cyclomatic_complexity)] fn postfix(expr: Expression) -> Box IResult> { 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 { // 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 { } // 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); } diff --git a/src/ser.rs b/src/ser.rs index 0366472..32b99ac 100644 --- a/src/ser.rs +++ b/src/ser.rs @@ -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(&mut self, value: T) -> Result<()> - where T: Into + Display + where + T: Into + 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)> { 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 { 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(self, value: &T) -> Result - 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 - { + fn serialize_unit_variant( + self, + _name: &'static str, + _variant_index: u32, + variant: &'static str, + ) -> Result { self.serialize_str(&variant.to_lowercase()) } fn serialize_newtype_struct(self, _name: &'static str, value: &T) -> Result - where T: ?Sized + ser::Serialize + where + T: ?Sized + ser::Serialize, { value.serialize(self) } - fn serialize_newtype_variant(self, _name: &'static str, _variant_index: u32, variant: &'static str, value: &T) - -> Result - where T: ?Sized + ser::Serialize + fn serialize_newtype_variant( + self, + _name: &'static str, + _variant_index: u32, + variant: &'static str, + value: &T, + ) -> Result + 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)) } - fn serialize_tuple_struct(self, _name: &'static str, len: usize) - -> Result - { + fn serialize_tuple_struct( + self, + _name: &'static str, + len: usize, + ) -> Result { self.serialize_seq(Some(len)) } - fn serialize_tuple_variant(self, name: &'static str, _variant_index: u32, variant: &'static str, _len: usize) - -> Result - { + fn serialize_tuple_variant( + self, + name: &'static str, + _variant_index: u32, + variant: &'static str, + _len: usize, + ) -> Result { self.push_key(&variant.to_lowercase()); Ok(self) } @@ -221,9 +247,13 @@ impl<'a> ser::Serializer for &'a mut ConfigSerializer { self.serialize_map(Some(len)) } - fn serialize_struct_variant(self, _name: &'static str, _variant_index: u32, variant: &'static str, len: usize) - -> Result - { + fn serialize_struct_variant( + self, + _name: &'static str, + _variant_index: u32, + variant: &'static str, + len: usize, + ) -> Result { self.push_key(&variant.to_lowercase()); Ok(self) } @@ -234,7 +264,8 @@ impl<'a> ser::SerializeSeq for &'a mut ConfigSerializer { type Error = ConfigError; fn serialize_element(&mut self, value: &T) -> Result<()> - where T: ?Sized + ser::Serialize + where + T: ?Sized + ser::Serialize, { self.inc_last_key_index()?; value.serialize(&mut **self)?; @@ -251,7 +282,8 @@ impl<'a> ser::SerializeTuple for &'a mut ConfigSerializer { type Error = ConfigError; fn serialize_element(&mut self, value: &T) -> Result<()> - where T: ?Sized + ser::Serialize + where + T: ?Sized + ser::Serialize, { self.inc_last_key_index()?; value.serialize(&mut **self)?; @@ -268,7 +300,8 @@ impl<'a> ser::SerializeTupleStruct for &'a mut ConfigSerializer { type Error = ConfigError; fn serialize_field(&mut self, value: &T) -> Result<()> - where T: ?Sized + ser::Serialize + where + T: ?Sized + ser::Serialize, { self.inc_last_key_index()?; value.serialize(&mut **self)?; @@ -285,7 +318,8 @@ impl<'a> ser::SerializeTupleVariant for &'a mut ConfigSerializer { type Error = ConfigError; fn serialize_field(&mut self, value: &T) -> Result<()> - where T: ?Sized + ser::Serialize + where + T: ?Sized + ser::Serialize, { self.inc_last_key_index()?; value.serialize(&mut **self)?; @@ -303,7 +337,8 @@ impl<'a> ser::SerializeMap for &'a mut ConfigSerializer { type Error = ConfigError; fn serialize_key(&mut self, key: &T) -> Result<()> - where T: ?Sized + ser::Serialize + where + T: ?Sized + ser::Serialize, { let key_serializer = StringKeySerializer; let key = key.serialize(key_serializer)?; @@ -312,7 +347,8 @@ impl<'a> ser::SerializeMap for &'a mut ConfigSerializer { } fn serialize_value(&mut self, value: &T) -> Result<()> - where T: ?Sized + ser::Serialize + where + T: ?Sized + ser::Serialize, { value.serialize(&mut **self)?; self.pop_key(); @@ -329,7 +365,8 @@ impl<'a> ser::SerializeStruct for &'a mut ConfigSerializer { type Error = ConfigError; fn serialize_field(&mut self, key: &'static str, value: &T) -> Result<()> - where T: ?Sized + ser::Serialize + where + T: ?Sized + ser::Serialize, { self.push_key(key); value.serialize(&mut **self)?; @@ -347,7 +384,8 @@ impl<'a> ser::SerializeStructVariant for &'a mut ConfigSerializer { type Error = ConfigError; fn serialize_field(&mut self, key: &'static str, value: &T) -> Result<()> - where T: ?Sized + ser::Serialize + where + T: ?Sized + ser::Serialize, { self.push_key(key); value.serialize(&mut **self)?; @@ -435,7 +473,8 @@ impl ser::Serializer for StringKeySerializer { } fn serialize_some(self, value: &T) -> Result - where T: ?Sized + ser::Serialize + where + T: ?Sized + ser::Serialize, { value.serialize(self) } @@ -448,63 +487,101 @@ impl ser::Serializer for StringKeySerializer { self.serialize_unit() } - fn serialize_unit_variant(self, _name: &str, _variant_index: u32, variant: &str) -> Result { + fn serialize_unit_variant( + self, + _name: &str, + _variant_index: u32, + variant: &str, + ) -> Result { Ok(variant.to_lowercase()) } fn serialize_newtype_struct(self, _name: &str, value: &T) -> Result - where T: ?Sized + ser::Serialize + where + T: ?Sized + ser::Serialize, { value.serialize(self) } - fn serialize_newtype_variant(self, _name: &str, _variant_index: u32, _variant: &str, value: &T) - -> Result - where T: ?Sized + ser::Serialize + fn serialize_newtype_variant( + self, + _name: &str, + _variant_index: u32, + _variant: &str, + value: &T, + ) -> Result + where + T: ?Sized + ser::Serialize, { value.serialize(self) } fn serialize_seq(self, _len: Option) -> Result { - Err(ConfigError::Message("seq can't serialize to string key".to_string())) + Err(ConfigError::Message( + "seq can't serialize to string key".to_string(), + )) } fn serialize_tuple(self, _len: usize) -> Result { - Err(ConfigError::Message("tuple can't serialize to string key".to_string())) + Err(ConfigError::Message( + "tuple can't serialize to string key".to_string(), + )) } fn serialize_tuple_struct(self, name: &str, _len: usize) -> Result { - Err(ConfigError::Message(format!("tuple struct {} can't serialize to string key", name))) + Err(ConfigError::Message(format!( + "tuple struct {} can't serialize to string key", + name + ))) } - fn serialize_tuple_variant(self, name: &str, _variant_index: u32, variant: &str, _len: usize) - -> Result - { - Err(ConfigError::Message(format!("tuple variant {}::{} can't serialize to string key", name, variant))) + fn serialize_tuple_variant( + self, + name: &str, + _variant_index: u32, + variant: &str, + _len: usize, + ) -> Result { + Err(ConfigError::Message(format!( + "tuple variant {}::{} can't serialize to string key", + name, variant + ))) } fn serialize_map(self, _len: Option) -> Result { - Err(ConfigError::Message("map can't serialize to string key".to_string())) + Err(ConfigError::Message( + "map can't serialize to string key".to_string(), + )) } fn serialize_struct(self, name: &str, _len: usize) -> Result { - Err(ConfigError::Message(format!("struct {} can't serialize to string key", name))) - } - - fn serialize_struct_variant(self, name: &str, _variant_index: u32, variant: &str, _len: usize) - -> Result - { - Err(ConfigError::Message(format!("struct variant {}::{} can't serialize to string key", name, variant))) + Err(ConfigError::Message(format!( + "struct {} can't serialize to string key", + name + ))) + } + + fn serialize_struct_variant( + self, + name: &str, + _variant_index: u32, + variant: &str, + _len: usize, + ) -> Result { + Err(ConfigError::Message(format!( + "struct variant {}::{} can't serialize to string key", + name, variant + ))) } } - impl ser::SerializeSeq for StringKeySerializer { type Ok = String; type Error = ConfigError; fn serialize_element(&mut self, value: &T) -> Result<()> - where T: ?Sized + ser::Serialize + where + T: ?Sized + ser::Serialize, { unreachable!() } @@ -519,7 +596,8 @@ impl ser::SerializeTuple for StringKeySerializer { type Error = ConfigError; fn serialize_element(&mut self, value: &T) -> Result<()> - where T: ?Sized + ser::Serialize + where + T: ?Sized + ser::Serialize, { unreachable!() } @@ -534,7 +612,8 @@ impl ser::SerializeTupleStruct for StringKeySerializer { type Error = ConfigError; fn serialize_field(&mut self, value: &T) -> Result<()> - where T: ?Sized + ser::Serialize + where + T: ?Sized + ser::Serialize, { unreachable!() } @@ -549,7 +628,8 @@ impl ser::SerializeTupleVariant for StringKeySerializer { type Error = ConfigError; fn serialize_field(&mut self, value: &T) -> Result<()> - where T: ?Sized + ser::Serialize + where + T: ?Sized + ser::Serialize, { unreachable!() } @@ -564,13 +644,15 @@ impl ser::SerializeMap for StringKeySerializer { type Error = ConfigError; fn serialize_key(&mut self, key: &T) -> Result<()> - where T: ?Sized + ser::Serialize + where + T: ?Sized + ser::Serialize, { unreachable!() } fn serialize_value(&mut self, value: &T) -> Result<()> - where T: ?Sized + ser::Serialize + where + T: ?Sized + ser::Serialize, { unreachable!() } @@ -585,7 +667,8 @@ impl ser::SerializeStruct for StringKeySerializer { type Error = ConfigError; fn serialize_field(&mut self, key: &'static str, value: &T) -> Result<()> - where T: ?Sized + ser::Serialize + where + T: ?Sized + ser::Serialize, { unreachable!() } @@ -600,7 +683,8 @@ impl ser::SerializeStructVariant for StringKeySerializer { type Error = ConfigError; fn serialize_field(&mut self, key: &'static str, value: &T) -> Result<()> - where T: ?Sized + ser::Serialize + where + T: ?Sized + ser::Serialize, { unreachable!() } @@ -623,7 +707,10 @@ mod test { seq: Vec, } - let test = Test { int: 1, seq: vec!["a".to_string(), "b".to_string()] }; + let test = Test { + int: 1, + seq: vec!["a".to_string(), "b".to_string()], + }; let config = Config::try_from(&test).unwrap(); let actual: Test = config.try_into().unwrap(); diff --git a/src/source.rs b/src/source.rs index 7f689a6..e4c4972 100644 --- a/src/source.rs +++ b/src/source.rs @@ -1,9 +1,9 @@ use error::*; +use path; +use std::collections::HashMap; use std::fmt::Debug; use std::str::FromStr; use value::{Value, ValueKind}; -use std::collections::HashMap; -use path; /// Describes a generic _source_ of configuration properties. pub trait Source: Debug { diff --git a/src/value.rs b/src/value.rs index 4a3186b..1bba78c 100644 --- a/src/value.rs +++ b/src/value.rs @@ -1,8 +1,8 @@ -use std::collections::HashMap; -use std::fmt::Display; -use std::fmt; use error::*; use serde::de::{Deserialize, Deserializer, Visitor}; +use std::collections::HashMap; +use std::fmt; +use std::fmt::Display; /// Underlying kind of the configuration value. #[derive(Debug, Clone, PartialEq)] -- cgit v1.2.3