diff --git a/libimagentryfilter/src/builtin/header/field_eq.rs b/libimagentryfilter/src/builtin/header/field_eq.rs index fc4e1e77..5617a42b 100644 --- a/libimagentryfilter/src/builtin/header/field_eq.rs +++ b/libimagentryfilter/src/builtin/header/field_eq.rs @@ -25,10 +25,9 @@ impl FieldEq { impl Filter for FieldEq { fn filter(&self, e: &Entry) -> bool { - let header = e.get_header(); - self.header_field_path - .walk(header) - .map(|v| self.expected_value == v.clone()) + e.get_header() + .read(&self.header_field_path[..]) + .map(|val| val.map(|v| v == self.expected_value).unwrap_or(false)) .unwrap_or(false) } diff --git a/libimagentryfilter/src/builtin/header/field_exists.rs b/libimagentryfilter/src/builtin/header/field_exists.rs index 45bc870a..ea964cb8 100644 --- a/libimagentryfilter/src/builtin/header/field_exists.rs +++ b/libimagentryfilter/src/builtin/header/field_exists.rs @@ -22,8 +22,7 @@ impl FieldExists { impl Filter for FieldExists { fn filter(&self, e: &Entry) -> bool { - let header = e.get_header(); - self.header_field_path.walk(header).is_some() + e.get_header().read(&self.header_field_path[..]).is_ok() } } diff --git a/libimagentryfilter/src/builtin/header/field_grep.rs b/libimagentryfilter/src/builtin/header/field_grep.rs index f1535602..2c28c9cf 100644 --- a/libimagentryfilter/src/builtin/header/field_grep.rs +++ b/libimagentryfilter/src/builtin/header/field_grep.rs @@ -26,13 +26,12 @@ impl FieldGrep { impl Filter for FieldGrep { fn filter(&self, e: &Entry) -> bool { - let header = e.get_header(); - self.header_field_path - .walk(header) + e.get_header() + .read(&self.header_field_path[..]) .map(|v| { match v { - Value::String(s) => self.grep.captures(&s[..]).is_some(), - _ => false, + Some(Value::String(s)) => self.grep.captures(&s[..]).is_some(), + _ => false, } }) .unwrap_or(false) diff --git a/libimagentryfilter/src/builtin/header/field_isempty.rs b/libimagentryfilter/src/builtin/header/field_isempty.rs index 83f74984..a6a5b3d3 100644 --- a/libimagentryfilter/src/builtin/header/field_isempty.rs +++ b/libimagentryfilter/src/builtin/header/field_isempty.rs @@ -22,18 +22,17 @@ impl FieldIsEmpty { impl Filter for FieldIsEmpty { fn filter(&self, e: &Entry) -> bool { - let header = e.get_header(); - self.header_field_path - .walk(header) + e.get_header() + .read(&self.header_field_path[..]) .map(|v| { match v { - Value::Array(a) => a.is_empty(), - Value::Boolean(_) => false, - Value::Float(_) => false, - Value::Integer(_) => false, - Value::String(_) => false, - Value::Table(t) => t.is_empty(), - _ => true, + Some(Value::Array(a)) => a.is_empty(), + Some(Value::Boolean(_)) => false, + Some(Value::Float(_)) => false, + Some(Value::Integer(_)) => false, + Some(Value::String(_)) => false, + Some(Value::Table(t)) => t.is_empty(), + _ => true, } }) .unwrap_or(false) diff --git a/libimagentryfilter/src/builtin/header/field_istype.rs b/libimagentryfilter/src/builtin/header/field_istype.rs index 23191611..6ff6d511 100644 --- a/libimagentryfilter/src/builtin/header/field_istype.rs +++ b/libimagentryfilter/src/builtin/header/field_istype.rs @@ -50,10 +50,9 @@ impl FieldIsType { impl Filter for FieldIsType { fn filter(&self, e: &Entry) -> bool { - let header = e.get_header(); - self.header_field_path - .walk(header) - .map(|v| self.expected_type.matches(&v)) + e.get_header() + .read(&self.header_field_path[..]) + .map(|val| val.map(|v| self.expected_type.matches(&v)).unwrap_or(false)) .unwrap_or(false) } diff --git a/libimagentryfilter/src/builtin/header/field_path.rs b/libimagentryfilter/src/builtin/header/field_path.rs new file mode 100644 index 00000000..b61f32b9 --- /dev/null +++ b/libimagentryfilter/src/builtin/header/field_path.rs @@ -0,0 +1 @@ +pub type FieldPath = String; diff --git a/libimagentryfilter/src/builtin/header/field_path/element.rs b/libimagentryfilter/src/builtin/header/field_path/element.rs deleted file mode 100644 index d6ad599a..00000000 --- a/libimagentryfilter/src/builtin/header/field_path/element.rs +++ /dev/null @@ -1,47 +0,0 @@ -use std::fmt::{Display, Formatter}; -use std::fmt::Error as FmtError; - -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> { - try!(write!(fmt, "{}", self.name)); - Ok(()) - } - -} - - diff --git a/libimagentryfilter/src/builtin/header/field_path/error.rs b/libimagentryfilter/src/builtin/header/field_path/error.rs deleted file mode 100644 index 69703cbf..00000000 --- a/libimagentryfilter/src/builtin/header/field_path/error.rs +++ /dev/null @@ -1,39 +0,0 @@ -use std::fmt::{Display, Formatter}; -use std::fmt::Error as FmtError; -use std::error::Error; - -use builtin::header::field_path::element::FieldPathElement; - -#[derive(Debug)] -pub struct FieldPathParsingError { - source: String, - token: FieldPathElement -} - -impl FieldPathParsingError { - - pub fn new(source: String, token: FieldPathElement) -> FieldPathParsingError { - FieldPathParsingError { source: source, token: token } - } -} - -impl Display for FieldPathParsingError { - - fn fmt(&self, fmt: &mut Formatter) -> Result<(), FmtError> { - try!(write!(fmt, "Failed to compile '{}', failed at: '{}'", self.source, self.token)); - Ok(()) - } - -} - -impl Error for FieldPathParsingError { - - fn description(&self) -> &str { - &self.source[..] - } - - fn cause(&self) -> Option<&Error> { - None - } - -} diff --git a/libimagentryfilter/src/builtin/header/field_path/mod.rs b/libimagentryfilter/src/builtin/header/field_path/mod.rs deleted file mode 100644 index 0760c596..00000000 --- a/libimagentryfilter/src/builtin/header/field_path/mod.rs +++ /dev/null @@ -1,44 +0,0 @@ -use std::fmt::{Display, Formatter}; -use std::fmt::Error as FmtError; -use std::error::Error; - -use toml::Value; - -pub mod element; -pub mod error; - -use libimagstore::store::Entry; -use libimagstore::store::EntryHeader; - -use builtin::header::field_path::element::FieldPathElement; -use builtin::header::field_path::error::FieldPathParsingError; - -pub struct FieldPath { - elements: Vec, -} - -impl FieldPath { - - pub fn new(elements: Vec) -> FieldPath { - FieldPath { - elements: elements, - } - } - - pub fn compile(source: String) -> Result { - unimplemented!() - } - - pub fn walk(&self, e: &EntryHeader) -> Option { - 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)) - }) - } - -} -