diff options
Diffstat (limited to 'libimagentryfilter')
-rw-r--r-- | libimagentryfilter/src/builtin/header/field_path/element.rs | 31 | ||||
-rw-r--r-- | libimagentryfilter/src/builtin/header/field_path/mod.rs | 13 |
2 files changed, 41 insertions, 3 deletions
diff --git a/libimagentryfilter/src/builtin/header/field_path/element.rs b/libimagentryfilter/src/builtin/header/field_path/element.rs index 044289aa..d6ad599a 100644 --- a/libimagentryfilter/src/builtin/header/field_path/element.rs +++ b/libimagentryfilter/src/builtin/header/field_path/element.rs @@ -1,11 +1,40 @@ use std::fmt::{Display, Formatter}; use std::fmt::Error as FmtError; -#[derive(Debug)] +use toml::Value; + +#[derive(Clone, Debug)] pub struct FieldPathElement { name: String } +impl FieldPathElement { + + pub fn new(name: String) -> FieldPathElement { + FieldPathElement { name: name } + } + + pub fn apply(&self, value: Value) -> Option<Value> { + use std::str::FromStr; + use std::ops::Index; + + match value { + Value::Table(t) => { + t.get(&self.name).map(|a| a.clone()) + }, + + Value::Array(a) => { + usize::from_str(&self.name[..]) + .ok() + .and_then(|i| Some(a[i].clone())) + }, + + _ => None, + } + } + +} + impl Display for FieldPathElement { fn fmt(&self, fmt: &mut Formatter) -> Result<(), FmtError> { diff --git a/libimagentryfilter/src/builtin/header/field_path/mod.rs b/libimagentryfilter/src/builtin/header/field_path/mod.rs index 0a8e61df..0760c596 100644 --- a/libimagentryfilter/src/builtin/header/field_path/mod.rs +++ b/libimagentryfilter/src/builtin/header/field_path/mod.rs @@ -20,7 +20,9 @@ pub struct FieldPath { impl FieldPath { pub fn new(elements: Vec<FieldPathElement>) -> FieldPath { - unimplemented!() + FieldPath { + elements: elements, + } } pub fn compile(source: String) -> Result<FieldPath, FieldPathParsingError> { @@ -28,7 +30,14 @@ impl FieldPath { } pub fn walk(&self, e: &EntryHeader) -> Option<Value> { - unimplemented!() + let init_val : Value = Value::Table(e.toml().clone()); + + self.elements + .clone() + .into_iter() + .fold(Some(init_val), |acc: Option<Value>, token: FieldPathElement| { + acc.and_then(|element| token.apply(element)) + }) } } |