Reimplement filters with EntryHeader::read()

Therefor:

    - Remove field_path submodule
    - pub type FieldPath = String
This commit is contained in:
Matthias Beyer 2016-02-13 14:08:25 +01:00
parent 37ce23fcaa
commit 75b6a700e5
9 changed files with 21 additions and 155 deletions

View file

@ -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)
}

View file

@ -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()
}
}

View file

@ -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)

View file

@ -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)

View file

@ -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)
}

View file

@ -0,0 +1 @@
pub type FieldPath = String;

View file

@ -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<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> {
try!(write!(fmt, "{}", self.name));
Ok(())
}
}

View file

@ -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
}
}

View file

@ -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<FieldPathElement>,
}
impl FieldPath {
pub fn new(elements: Vec<FieldPathElement>) -> FieldPath {
FieldPath {
elements: elements,
}
}
pub fn compile(source: String) -> Result<FieldPath, FieldPathParsingError> {
unimplemented!()
}
pub fn walk(&self, e: &EntryHeader) -> Option<Value> {
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))
})
}
}