Add helper trait for extracting from Array/Table

This commit is contained in:
Matthias Beyer 2016-11-14 15:00:11 +01:00
parent bdc9318a41
commit 6f79e6e007

View file

@ -86,7 +86,7 @@ impl TomlValueExt for Value {
let (destination, value) = try!(setup(self, spec, sep)); let (destination, value) = try!(setup(self, spec, sep));
// There is already an value at this place // There is already an value at this place
if extract(value, &destination).is_ok() { if value.extract(&destination).is_ok() {
return Ok(false); return Ok(false);
} }
@ -315,7 +315,7 @@ 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| if let Some(token) = next { v.and_then(move |value| if let Some(token) = next {
walk_iter(extract(value, &token), i) walk_iter(value.extract(&token), i)
} else { } else {
Ok(value) Ok(value)
}) })
@ -324,28 +324,32 @@ fn walk_header(v: &mut Value, tokens: Vec<Token>) -> Result<&mut Value> {
walk_iter(Ok(v), &mut tokens.into_iter()) walk_iter(Ok(v), &mut tokens.into_iter())
} }
fn extract_from_table<'a>(v: &'a mut Value, s: &str) -> Result<&'a mut Value> { trait Extract {
match *v { fn extract<'a>(&'a mut self, &Token) -> Result<&'a mut Self>;
Value::Table(ref mut t) => t.get_mut(&s[..]).ok_or(SEK::HeaderKeyNotFound.into_error()), }
_ => Err(SEK::HeaderPathTypeFailure.into_error()),
} impl Extract for Value {
} fn extract<'a>(&'a mut self, token: &Token) -> Result<&'a mut Value> {
match *token {
fn extract_from_array(v: &mut Value, i: usize) -> Result<&mut Value> { // on Token::Key extract from Value::Table
match *v { Token::Key(ref s) => match *self {
Value::Array(ref mut a) => if a.len() < i { Value::Table(ref mut t) =>
Err(SEK::HeaderKeyNotFound.into_error()) t.get_mut(&s[..]).ok_or(SEK::HeaderKeyNotFound.into_error()),
} else {
Ok(&mut a[i]) _ => Err(SEK::HeaderPathTypeFailure.into_error()),
}, },
_ => Err(SEK::HeaderPathTypeFailure.into_error()),
} // on Token::Index extract from Value::Array
} Token::Index(i) => match *self {
Value::Array(ref mut a) => if a.len() < i {
fn extract<'a>(v: &'a mut Value, token: &Token) -> Result<&'a mut Value> { Err(SEK::HeaderKeyNotFound.into_error())
match *token { } else {
Token::Key(ref s) => extract_from_table(v, s), Ok(&mut a[i])
Token::Index(i) => extract_from_array(v, i), },
_ => Err(SEK::HeaderPathTypeFailure.into_error()),
}
}
} }
} }