Prettify codebase

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

View file

@ -85,11 +85,11 @@ impl TomlValueExt for Value {
fn insert_with_sep(&mut self, spec: &str, sep: char, v: Value) -> Result<bool> { fn insert_with_sep(&mut self, spec: &str, sep: char, v: Value) -> Result<bool> {
let tokens = match tokenize(spec, sep) { let tokens = match tokenize(spec, sep) {
Err(e) => return Err(e), Err(e) => return Err(e),
Ok(t) => t Ok(t) => t
}; };
let destination = match tokens.iter().last() { let destination = match tokens.iter().last() {
None => return Err(SE::new(SEK::HeaderPathSyntaxError, None)), None => return Err(SE::new(SEK::HeaderPathSyntaxError, None)),
Some(d) => d, Some(d) => d,
}; };
@ -98,7 +98,7 @@ impl TomlValueExt for Value {
// walk N-1 tokens // walk N-1 tokens
let value = match walk_header(self, path_to_dest) { let value = match walk_header(self, path_to_dest) {
Err(e) => return Err(e), Err(e) => return Err(e),
Ok(v) => v Ok(v) => v
}; };
// There is already an value at this place // There is already an value at this place
@ -107,43 +107,39 @@ impl TomlValueExt for Value {
} }
match *destination { match *destination {
Token::Key(ref s) => { // if the destination shall be an map key // if the destination shall be an map key
match *value { Token::Key(ref s) => match *value {
/* /*
* Put it in there if we have a map * Put it in there if we have a map
*/ */
Value::Table(ref mut t) => { Value::Table(ref mut t) => { t.insert(s.clone(), v); },
t.insert(s.clone(), v);
}
/* /*
* Fail if there is no map here * Fail if there is no map here
*/ */
_ => return Err(SEK::HeaderPathTypeFailure.into_error()), _ => return Err(SEK::HeaderPathTypeFailure.into_error()),
}
}, },
Token::Index(i) => { // if the destination shall be an array // if the destination shall be an array
match *value { Token::Index(i) => match *value {
/* /*
* Put it in there if we have an array * Put it in there if we have an array
*/ */
Value::Array(ref mut a) => { Value::Array(ref mut a) => {
a.push(v); // push to the end of the array a.push(v); // push to the end of the array
// if the index is inside the array, we swap-remove the element at this // if the index is inside the array, we swap-remove the element at this
// index // index
if a.len() < i { if a.len() < i {
a.swap_remove(i); a.swap_remove(i);
} }
}, },
/* /*
* Fail if there is no array here * Fail if there is no array here
*/ */
_ => return Err(SEK::HeaderPathTypeFailure.into_error()), _ => return Err(SEK::HeaderPathTypeFailure.into_error()),
}
}, },
} }
@ -176,12 +172,12 @@ impl TomlValueExt for Value {
fn set_with_sep(&mut self, spec: &str, sep: char, v: Value) -> Result<Option<Value>> { fn set_with_sep(&mut self, spec: &str, sep: char, v: Value) -> Result<Option<Value>> {
let tokens = match tokenize(spec, sep) { let tokens = match tokenize(spec, sep) {
Err(e) => return Err(e), Err(e) => return Err(e),
Ok(t) => t, Ok(t) => t,
}; };
debug!("tokens = {:?}", tokens); debug!("tokens = {:?}", tokens);
let destination = match tokens.iter().last() { let destination = match tokens.iter().last() {
None => return Err(SEK::HeaderPathSyntaxError.into_error()), None => return Err(SEK::HeaderPathSyntaxError.into_error()),
Some(d) => d Some(d) => d
}; };
debug!("destination = {:?}", destination); debug!("destination = {:?}", destination);
@ -190,60 +186,58 @@ impl TomlValueExt for Value {
// walk N-1 tokens // walk N-1 tokens
let value = match walk_header(self, path_to_dest) { let value = match walk_header(self, path_to_dest) {
Err(e) => return Err(e), Err(e) => return Err(e),
Ok(v) => v Ok(v) => v
}; };
debug!("walked value = {:?}", value); debug!("walked value = {:?}", value);
match *destination { match *destination {
Token::Key(ref s) => { // if the destination shall be an map key->value // if the destination shall be an map key->value
match *value { Token::Key(ref s) => match *value {
/* /*
* Put it in there if we have a map * Put it in there if we have a map
*/ */
Value::Table(ref mut t) => { Value::Table(ref mut t) => {
debug!("Matched Key->Table"); debug!("Matched Key->Table");
return Ok(t.insert(s.clone(), v)); return Ok(t.insert(s.clone(), v));
} }
/* /*
* Fail if there is no map here * Fail if there is no map here
*/ */
_ => { _ => {
debug!("Matched Key->NON-Table"); debug!("Matched Key->NON-Table");
return Err(SEK::HeaderPathTypeFailure.into_error()); return Err(SEK::HeaderPathTypeFailure.into_error());
}
} }
}, },
Token::Index(i) => { // if the destination shall be an array // if the destination shall be an array
match *value { Token::Index(i) => match *value {
/* /*
* Put it in there if we have an array * Put it in there if we have an array
*/ */
Value::Array(ref mut a) => { Value::Array(ref mut a) => {
debug!("Matched Index->Array"); debug!("Matched Index->Array");
a.push(v); // push to the end of the array a.push(v); // push to the end of the array
// if the index is inside the array, we swap-remove the element at this // if the index is inside the array, we swap-remove the element at this
// index // index
if a.len() > i { if a.len() > i {
debug!("Swap-Removing in Array {:?}[{:?}] <- {:?}", a, i, a[a.len()-1]); debug!("Swap-Removing in Array {:?}[{:?}] <- {:?}", a, i, a[a.len()-1]);
return Ok(Some(a.swap_remove(i))); return Ok(Some(a.swap_remove(i)));
} }
debug!("Appended"); debug!("Appended");
return Ok(None); return Ok(None);
}, },
/* /*
* Fail if there is no array here * Fail if there is no array here
*/ */
_ => { _ => {
debug!("Matched Index->NON-Array"); debug!("Matched Index->NON-Array");
return Err(SEK::HeaderPathTypeFailure.into_error()); return Err(SEK::HeaderPathTypeFailure.into_error());
}, },
}
}, },
} }
@ -295,11 +289,11 @@ impl TomlValueExt for Value {
fn delete_with_sep(&mut self, spec: &str, splitchr: char) -> Result<Option<Value>> { fn delete_with_sep(&mut self, spec: &str, splitchr: char) -> Result<Option<Value>> {
let tokens = match tokenize(spec, splitchr) { let tokens = match tokenize(spec, splitchr) {
Err(e) => return Err(e), Err(e) => return Err(e),
Ok(t) => t Ok(t) => t
}; };
let destination = match tokens.iter().last() { let destination = match tokens.iter().last() {
None => return Err(SEK::HeaderPathSyntaxError.into_error()), None => return Err(SEK::HeaderPathSyntaxError.into_error()),
Some(d) => d Some(d) => d
}; };
debug!("destination = {:?}", destination); debug!("destination = {:?}", destination);
@ -308,41 +302,38 @@ impl TomlValueExt for Value {
// walk N-1 tokens // walk N-1 tokens
let mut value = match walk_header(self, path_to_dest) { let mut value = match walk_header(self, path_to_dest) {
Err(e) => return Err(e), Err(e) => return Err(e),
Ok(v) => v Ok(v) => v
}; };
debug!("walked value = {:?}", value); debug!("walked value = {:?}", value);
match *destination { match *destination {
Token::Key(ref s) => { // if the destination shall be an map key->value // if the destination shall be an map key->value
match *value { Token::Key(ref s) => match *value {
Value::Table(ref mut t) => { Value::Table(ref mut t) => {
debug!("Matched Key->Table, removing {:?}", s); debug!("Matched Key->Table, removing {:?}", s);
return Ok(t.remove(s)); return Ok(t.remove(s));
}, },
_ => { _ => {
debug!("Matched Key->NON-Table"); debug!("Matched Key->NON-Table");
return Err(SEK::HeaderPathTypeFailure.into_error()); return Err(SEK::HeaderPathTypeFailure.into_error());
}
} }
}, },
Token::Index(i) => { // if the destination shall be an array // if the destination shall be an array
match *value { Token::Index(i) => match *value {
Value::Array(ref mut a) => {
// if the index is inside the array, we swap-remove the element at this // if the index is inside the array, we swap-remove the element at this
// index // index
if a.len() > i { Value::Array(ref mut a) => if a.len() > i {
debug!("Removing in Array {:?}[{:?}]", a, i); debug!("Removing in Array {:?}[{:?}]", a, i);
return Ok(Some(a.remove(i))); return Ok(Some(a.remove(i)));
} else { } else {
return Ok(None); return Ok(None);
} },
}, _ => {
_ => { debug!("Matched Index->NON-Array");
debug!("Matched Index->NON-Array"); return Err(SEK::HeaderPathTypeFailure.into_error());
return Err(SEK::HeaderPathTypeFailure.into_error()); },
},
}
}, },
} }
@ -355,11 +346,7 @@ fn tokenize(spec: &str, splitchr: char) -> Result<Vec<Token>> {
use std::str::FromStr; use std::str::FromStr;
spec.split(splitchr) spec.split(splitchr)
.map(|s| { .map(|s| usize::from_str(s).map(Token::Index).or_else(|_| Ok(Token::Key(String::from(s)))))
usize::from_str(s)
.map(Token::Index)
.or_else(|_| Ok(Token::Key(String::from(s))))
})
.collect() .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> { fn walk_iter<'a>(v: Result<&'a mut Value>, i: &mut IntoIter<Token>) -> Result<&'a mut Value> {
let next = i.next(); let next = i.next();
v.and_then(move |value| { v.and_then(move |value| if let Some(token) = next {
if let Some(token) = next { walk_iter(extract(value, &token), i)
walk_iter(extract(value, &token), i) } else {
} else { Ok(value)
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> { fn extract_from_table<'a>(v: &'a mut Value, s: &str) -> Result<&'a mut Value> {
match *v { match *v {
Value::Table(ref mut t) => { Value::Table(ref mut t) => t.get_mut(&s[..]).ok_or(SEK::HeaderKeyNotFound.into_error()),
t.get_mut(&s[..])
.ok_or(SEK::HeaderKeyNotFound.into_error())
},
_ => Err(SEK::HeaderPathTypeFailure.into_error()), _ => Err(SEK::HeaderPathTypeFailure.into_error()),
} }
} }
fn extract_from_array(v: &mut Value, i: usize) -> Result<&mut Value> { fn extract_from_array(v: &mut Value, i: usize) -> Result<&mut Value> {
match *v { match *v {
Value::Array(ref mut a) => { Value::Array(ref mut a) => if a.len() < i {
if a.len() < i { Err(SEK::HeaderKeyNotFound.into_error())
Err(SEK::HeaderKeyNotFound.into_error()) } else {
} else { Ok(&mut a[i])
Ok(&mut a[i])
}
}, },
_ => Err(SEK::HeaderPathTypeFailure.into_error()), _ => Err(SEK::HeaderPathTypeFailure.into_error()),
} }