Implement walk_header()

This commit is contained in:
Matthias Beyer 2016-02-04 13:26:01 +01:00
parent a2e3598c01
commit 4759076249

View file

@ -459,6 +459,47 @@ impl EntryHeader {
.collect()
}
fn walk_header(v: &mut Value, tokens: Vec<Token>) -> Result<&mut Value> {
use std::vec::IntoIter;
fn extract_from_table<'a>(v: &'a mut Value, s: &String) -> Result<&'a mut Value> {
match v {
&mut Value::Table(ref mut t) => {
t.get_mut(&s[..])
.ok_or(StoreError::new(StoreErrorKind::HeaderKeyNotFound, None))
},
_ => Err(StoreError::new(StoreErrorKind::HeaderPathTypeFailure, None)),
}
}
fn extract_from_array(v: &mut Value, i: usize) -> Result<&mut Value> {
match v {
&mut Value::Array(ref mut a) => Ok(&mut a[i]),
_ => Err(StoreError::new(StoreErrorKind::HeaderPathTypeFailure, None)),
}
}
fn extract<'a>(v: &'a mut Value, token: &Token) -> Result<&'a mut Value> {
match token {
&Token::Key(ref s) => extract_from_table(v, s),
&Token::Index(i) => extract_from_array(v, i),
}
}
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 {
walk_iter(extract(value, &token), i)
} else {
Ok(value)
}
})
}
walk_iter(Ok(v), &mut tokens.into_iter())
}
}
fn build_default_header() -> BTreeMap<String, Value> {