Prettify codebase

This commit is contained in:
Matthias Beyer 2016-11-14 14:28:55 +01:00
parent 35cfb5d651
commit 79f68a1b79

View file

@ -107,24 +107,21 @@ impl TomlValueExt for Value {
}
match *destination {
Token::Key(ref s) => { // if the destination shall be an map key
match *value {
// if the destination shall be an map key
Token::Key(ref s) => match *value {
/*
* Put it in there if we have a map
*/
Value::Table(ref mut t) => {
t.insert(s.clone(), v);
}
Value::Table(ref mut t) => { t.insert(s.clone(), v); },
/*
* Fail if there is no map here
*/
_ => return Err(SEK::HeaderPathTypeFailure.into_error()),
}
},
Token::Index(i) => { // if the destination shall be an array
match *value {
// if the destination shall be an array
Token::Index(i) => match *value {
/*
* Put it in there if we have an array
@ -143,7 +140,6 @@ impl TomlValueExt for Value {
* Fail if there is no array here
*/
_ => return Err(SEK::HeaderPathTypeFailure.into_error()),
}
},
}
@ -195,8 +191,8 @@ impl TomlValueExt for Value {
debug!("walked value = {:?}", value);
match *destination {
Token::Key(ref s) => { // if the destination shall be an map key->value
match *value {
// if the destination shall be an map key->value
Token::Key(ref s) => match *value {
/*
* Put it in there if we have a map
*/
@ -212,11 +208,10 @@ impl TomlValueExt for Value {
debug!("Matched Key->NON-Table");
return Err(SEK::HeaderPathTypeFailure.into_error());
}
}
},
Token::Index(i) => { // if the destination shall be an array
match *value {
// if the destination shall be an array
Token::Index(i) => match *value {
/*
* Put it in there if we have an array
@ -243,7 +238,6 @@ impl TomlValueExt for Value {
debug!("Matched Index->NON-Array");
return Err(SEK::HeaderPathTypeFailure.into_error());
},
}
},
}
@ -313,8 +307,8 @@ impl TomlValueExt for Value {
debug!("walked value = {:?}", value);
match *destination {
Token::Key(ref s) => { // if the destination shall be an map key->value
match *value {
// if the destination shall be an map key->value
Token::Key(ref s) => match *value {
Value::Table(ref mut t) => {
debug!("Matched Key->Table, removing {:?}", s);
return Ok(t.remove(s));
@ -323,26 +317,23 @@ impl TomlValueExt for Value {
debug!("Matched Key->NON-Table");
return Err(SEK::HeaderPathTypeFailure.into_error());
}
}
},
Token::Index(i) => { // if the destination shall be an array
match *value {
Value::Array(ref mut a) => {
// if the destination shall be an array
Token::Index(i) => match *value {
// if the index is inside the array, we swap-remove the element at this
// index
if a.len() > i {
Value::Array(ref mut a) => if a.len() > i {
debug!("Removing in Array {:?}[{:?}]", a, i);
return Ok(Some(a.remove(i)));
} else {
return Ok(None);
}
},
_ => {
debug!("Matched Index->NON-Array");
return Err(SEK::HeaderPathTypeFailure.into_error());
},
}
},
}
@ -355,11 +346,7 @@ fn tokenize(spec: &str, splitchr: char) -> Result<Vec<Token>> {
use std::str::FromStr;
spec.split(splitchr)
.map(|s| {
usize::from_str(s)
.map(Token::Index)
.or_else(|_| Ok(Token::Key(String::from(s))))
})
.map(|s| usize::from_str(s).map(Token::Index).or_else(|_| Ok(Token::Key(String::from(s)))))
.collect()
}
@ -368,12 +355,10 @@ fn walk_header(v: &mut Value, tokens: Vec<Token>) -> Result<&mut Value> {
fn walk_iter<'a>(v: Result<&'a mut Value>, i: &mut IntoIter<Token>) -> Result<&'a mut Value> {
let next = i.next();
v.and_then(move |value| {
if let Some(token) = next {
v.and_then(move |value| if let Some(token) = next {
walk_iter(extract(value, &token), i)
} else {
Ok(value)
}
})
}
@ -382,22 +367,17 @@ fn walk_header(v: &mut Value, tokens: Vec<Token>) -> Result<&mut Value> {
fn extract_from_table<'a>(v: &'a mut Value, s: &str) -> Result<&'a mut Value> {
match *v {
Value::Table(ref mut t) => {
t.get_mut(&s[..])
.ok_or(SEK::HeaderKeyNotFound.into_error())
},
Value::Table(ref mut t) => t.get_mut(&s[..]).ok_or(SEK::HeaderKeyNotFound.into_error()),
_ => Err(SEK::HeaderPathTypeFailure.into_error()),
}
}
fn extract_from_array(v: &mut Value, i: usize) -> Result<&mut Value> {
match *v {
Value::Array(ref mut a) => {
if a.len() < i {
Value::Array(ref mut a) => if a.len() < i {
Err(SEK::HeaderKeyNotFound.into_error())
} else {
Ok(&mut a[i])
}
},
_ => Err(SEK::HeaderPathTypeFailure.into_error()),
}