Prettify codebase
This commit is contained in:
parent
35cfb5d651
commit
79f68a1b79
1 changed files with 109 additions and 129 deletions
|
@ -107,24 +107,21 @@ 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
|
||||||
|
@ -143,7 +140,6 @@ impl TomlValueExt for Value {
|
||||||
* 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()),
|
||||||
}
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -195,8 +191,8 @@ impl TomlValueExt for Value {
|
||||||
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
|
||||||
*/
|
*/
|
||||||
|
@ -212,11 +208,10 @@ impl TomlValueExt for Value {
|
||||||
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
|
||||||
|
@ -243,7 +238,6 @@ impl TomlValueExt for Value {
|
||||||
debug!("Matched Index->NON-Array");
|
debug!("Matched Index->NON-Array");
|
||||||
return Err(SEK::HeaderPathTypeFailure.into_error());
|
return Err(SEK::HeaderPathTypeFailure.into_error());
|
||||||
},
|
},
|
||||||
}
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -313,8 +307,8 @@ impl TomlValueExt for Value {
|
||||||
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));
|
||||||
|
@ -323,26 +317,23 @@ impl TomlValueExt for Value {
|
||||||
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()),
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue