Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run cargo-fmt #155

Merged
merged 1 commit into from
Oct 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,7 @@ impl Config {
match value {
Some(value) => {
// Deserialize the received value into the requested type
T::deserialize(value)
.map_err(|e| e.extend_with_key(key))
T::deserialize(value).map_err(|e| e.extend_with_key(key))
}

None => Err(ConfigError::NotFound(key.into())),
Expand Down
40 changes: 23 additions & 17 deletions src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use error::*;
use serde::de;
use std::collections::{HashMap, VecDeque};
use std::iter::Enumerate;
use value::{Value, ValueKind, Table};
use value::{Table, Value, ValueKind};

impl<'de> de::Deserializer<'de> for Value {
type Error = ConfigError;
Expand Down Expand Up @@ -125,7 +125,11 @@ impl<'de> de::Deserializer<'de> for Value {
where
V: de::Visitor<'de>,
{
visitor.visit_enum(EnumAccess{ value: self, name, variants })
visitor.visit_enum(EnumAccess {
value: self,
name,
variants,
})
}

forward_to_deserialize_any! {
Expand Down Expand Up @@ -178,11 +182,10 @@ impl<'de> de::SeqAccess<'de> for SeqAccess {
T: de::DeserializeSeed<'de>,
{
match self.elements.next() {
Some((idx, value)) => {
seed.deserialize(value)
.map(Some)
.map_err(|e| e.prepend_index(idx))
}
Some((idx, value)) => seed
.deserialize(value)
.map(Some)
.map_err(|e| e.prepend_index(idx)),
None => Ok(None),
}
}
Expand Down Expand Up @@ -229,8 +232,7 @@ impl<'de> de::MapAccess<'de> for MapAccess {
V: de::DeserializeSeed<'de>,
{
let (key, value) = self.elements.pop_front().unwrap();
de::DeserializeSeed::deserialize(seed, value)
.map_err(|e| e.prepend_key(key))
de::DeserializeSeed::deserialize(seed, value).map_err(|e| e.prepend_key(key))
}
}

Expand Down Expand Up @@ -315,21 +317,21 @@ impl<'de> de::VariantAccess<'de> for EnumAccess {
V: de::Visitor<'de>,
{
match self.value.kind {
ValueKind::Table(t) => de::Deserializer::deserialize_seq(t.into_iter().next().unwrap().1, visitor),
ValueKind::Table(t) => {
de::Deserializer::deserialize_seq(t.into_iter().next().unwrap().1, visitor)
}
_ => unreachable!(),
}
}

fn struct_variant<V>(
self,
_fields: &'static [&'static str],
visitor: V,
) -> Result<V::Value>
fn struct_variant<V>(self, _fields: &'static [&'static str], visitor: V) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
match self.value.kind {
ValueKind::Table(t) => de::Deserializer::deserialize_map(t.into_iter().next().unwrap().1, visitor),
ValueKind::Table(t) => {
de::Deserializer::deserialize_map(t.into_iter().next().unwrap().1, visitor)
}
_ => unreachable!(),
}
}
Expand Down Expand Up @@ -448,7 +450,11 @@ impl<'de> de::Deserializer<'de> for Config {
where
V: de::Visitor<'de>,
{
visitor.visit_enum(EnumAccess{ value: self.cache, name, variants })
visitor.visit_enum(EnumAccess {
value: self.cache,
name,
variants,
})
}

forward_to_deserialize_any! {
Expand Down
16 changes: 7 additions & 9 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,12 @@ impl ConfigError {
unexpected,
expected,
key,
} => {
ConfigError::Type {
origin,
unexpected,
expected,
key: Some(concat(key)),
}
}
} => ConfigError::Type {
origin,
unexpected,
expected,
key: Some(concat(key)),
},
ConfigError::NotFound(key) => ConfigError::NotFound(concat(Some(key))),
_ => self,
}
Expand Down Expand Up @@ -210,7 +208,7 @@ impl fmt::Display for ConfigError {
}
}

impl Error for ConfigError { }
impl Error for ConfigError {}

impl de::Error for ConfigError {
fn custom<T: fmt::Display>(msg: T) -> Self {
Expand Down
15 changes: 3 additions & 12 deletions src/file/format/ini.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,13 @@ 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.clone(),
Value::new(uri, ValueKind::String(v.clone())),
);
sec_map.insert(k.clone(), Value::new(uri, ValueKind::String(v.clone())));
}
map.insert(
sec.clone(),
Value::new(uri, ValueKind::Table(sec_map)),
);
map.insert(sec.clone(), Value::new(uri, ValueKind::Table(sec_map)));
}
None => {
for (k, v) in prop.iter() {
map.insert(
k.clone(),
Value::new(uri, ValueKind::String(v.clone())),
);
map.insert(k.clone(), Value::new(uri, ValueKind::String(v.clone())));
}
}
}
Expand Down
16 changes: 9 additions & 7 deletions src/file/format/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ 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)),

Expand Down
7 changes: 2 additions & 5 deletions src/file/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use value::Value;
pub use self::format::FileFormat;
use self::source::FileSource;

pub use self::source::string::FileSourceString;
pub use self::source::file::FileSourceFile;
pub use self::source::string::FileSourceString;

#[derive(Clone, Debug)]
pub struct File<T>
Expand Down Expand Up @@ -119,9 +119,6 @@ where
// Parse the string using the given format
format
.parse(uri.as_ref(), &contents)
.map_err(|cause| ConfigError::FileParse {
uri,
cause,
})
.map_err(|cause| ConfigError::FileParse { uri, cause })
}
}
26 changes: 15 additions & 11 deletions src/file/source/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,23 +58,27 @@ impl FileSourceFile {
}

match format_hint {
Some(format) => for ext in format.extensions() {
filename.set_extension(ext);

if filename.is_file() {
return Ok((filename, format));
}
},

None => for (format, extensions) in ALL_EXTENSIONS.iter() {
Some(format) => {
for ext in format.extensions() {
filename.set_extension(ext);

if filename.is_file() {
return Ok((filename, *format));
return Ok((filename, format));
}
}
},
}

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));
}
}
}
}
}

Err(Box::new(io::Error::new(
Expand Down
43 changes: 18 additions & 25 deletions src/path/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,34 +151,30 @@ impl Expression {
_ => None,
},

Expression::Subscript(ref expr, index) => {
match expr.get_mut_forcibly(root) {
Some(value) => {
match value.kind {
ValueKind::Array(_) => (),
_ => *value = Vec::<Value>::new().into(),
}

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_forcibly(root) {
Some(value) => {
match value.kind {
ValueKind::Array(_) => (),
_ => *value = Vec::<Value>::new().into(),
}

if index >= array.len() {
array.resize(
(index + 1) as usize,
Value::new(None, ValueKind::Nil),
);
}
match value.kind {
ValueKind::Array(ref mut array) => {
let index = sindex_to_uindex(index, array.len());

Some(&mut array[index])
if index >= array.len() {
array
.resize((index + 1) as usize, Value::new(None, ValueKind::Nil));
}

_ => None,
Some(&mut array[index])
}

_ => None,
}
_ => None,
}
}
_ => None,
},
}
}

Expand Down Expand Up @@ -246,10 +242,7 @@ impl Expression {
if let ValueKind::Array(ref mut array) = parent.kind {
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;
Expand Down
62 changes: 31 additions & 31 deletions src/path/parser.rs
Original file line number Diff line number Diff line change
@@ -1,49 +1,49 @@
use super::Expression;
use nom::{
IResult, Err,
error::ErrorKind,
bytes::complete::{is_a, tag},
character::complete::{char, digit1, space0},
sequence::{delimited, pair, preceded},
branch::alt,
combinator::{map, map_res, opt, recognize},
branch::alt,
bytes::complete::{is_a, tag},
character::complete::{char, digit1, space0},
combinator::{map, map_res, opt, recognize},
error::ErrorKind,
sequence::{delimited, pair, preceded},
Err, IResult,
};
use std::str::{from_utf8, FromStr};

fn raw_ident(i: &str) -> IResult<&str, String> {
map(is_a(
"abcdefghijklmnopqrstuvwxyz \
map(
is_a(
"abcdefghijklmnopqrstuvwxyz \
ABCDEFGHIJKLMNOPQRSTUVWXYZ \
0123456789 \
_-"
), |s:&str| s.to_string())(i)
_-",
),
|s: &str| s.to_string(),
)(i)
}

fn integer(i: &str) -> IResult<&str, isize> {
map_res(
delimited(
space0,
recognize(pair(opt(tag("-")), digit1)),
space0
),
FromStr::from_str
)(i)
map_res(
delimited(space0, recognize(pair(opt(tag("-")), digit1)), space0),
FromStr::from_str,
)(i)
}

fn ident(i: &str) -> IResult<&str, Expression> {
map(raw_ident, Expression::Identifier)(i)
map(raw_ident, Expression::Identifier)(i)
}

fn postfix<'a>(expr: Expression) -> impl Fn(&'a str) -> IResult<&'a str, Expression> {
let e2 = expr.clone();
let child = map(preceded(tag("."), raw_ident), move |id| Expression::Child(Box::new(expr.clone()), id));
let e2 = expr.clone();
let child = map(preceded(tag("."), raw_ident), move |id| {
Expression::Child(Box::new(expr.clone()), id)
});

let subscript = map(delimited(char('['), integer, char(']')), move |num| Expression::Subscript(Box::new(e2.clone()), num));
let subscript = map(delimited(char('['), integer, char(']')), move |num| {
Expression::Subscript(Box::new(e2.clone()), num)
});

alt((
child,
subscript
))
alt((child, subscript))
}

pub fn from_str(input: &str) -> Result<Expression, ErrorKind> {
Expand Down Expand Up @@ -72,10 +72,10 @@ pub fn from_str(input: &str) -> Result<Expression, ErrorKind> {
}

pub fn to_error_kind(e: Err<(&str, ErrorKind)>) -> ErrorKind {
match e {
Err::Incomplete(_) => ErrorKind::Complete,
Err::Failure((_, e)) | Err::Error((_, e)) => e,
}
match e {
Err::Incomplete(_) => ErrorKind::Complete,
Err::Failure((_, e)) | Err::Error((_, e)) => e,
}
}

#[cfg(test)]
Expand Down
Loading