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 { + 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) -> FieldPath { - unimplemented!() + FieldPath { + elements: elements, + } } pub fn compile(source: String) -> Result { @@ -28,7 +30,14 @@ impl FieldPath { } pub fn walk(&self, e: &EntryHeader) -> Option { - unimplemented!() + let init_val : Value = Value::Table(e.toml().clone()); + + self.elements + .clone() + .into_iter() + .fold(Some(init_val), |acc: Option, token: FieldPathElement| { + acc.and_then(|element| token.apply(element)) + }) } }