From fcbc020083d00b2d7b4a3d318945838c8756ec86 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Tue, 2 Feb 2016 14:06:00 +0100 Subject: [PATCH 01/14] libimagentryfilter: init --- libimagentryfilter/Cargo.toml | 6 ++++++ libimagentryfilter/src/lib.rs | 3 +++ 2 files changed, 9 insertions(+) create mode 100644 libimagentryfilter/Cargo.toml create mode 100644 libimagentryfilter/src/lib.rs diff --git a/libimagentryfilter/Cargo.toml b/libimagentryfilter/Cargo.toml new file mode 100644 index 00000000..5dc1da31 --- /dev/null +++ b/libimagentryfilter/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "libimagentryfilter" +version = "0.1.0" +authors = ["Matthias Beyer "] + +[dependencies] diff --git a/libimagentryfilter/src/lib.rs b/libimagentryfilter/src/lib.rs new file mode 100644 index 00000000..a93251b6 --- /dev/null +++ b/libimagentryfilter/src/lib.rs @@ -0,0 +1,3 @@ +#[test] +fn it_works() { +} From cb17e6a4c508e539543496414c78fb36d4093fe9 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Tue, 2 Feb 2016 14:09:12 +0100 Subject: [PATCH 02/14] Add initial dependencies --- libimagentryfilter/Cargo.toml | 9 +++++++++ libimagentryfilter/src/lib.rs | 11 ++++++++--- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/libimagentryfilter/Cargo.toml b/libimagentryfilter/Cargo.toml index 5dc1da31..00aae942 100644 --- a/libimagentryfilter/Cargo.toml +++ b/libimagentryfilter/Cargo.toml @@ -4,3 +4,12 @@ version = "0.1.0" authors = ["Matthias Beyer "] [dependencies] +clap = "1.5.5" +itertools = "0.4.7" +log = "0.3.4" +regex = "0.1.48" +toml = "0.1.27" + +[dependencies.libimagstore] +path = "../libimagstore" + diff --git a/libimagentryfilter/src/lib.rs b/libimagentryfilter/src/lib.rs index a93251b6..7d7c4139 100644 --- a/libimagentryfilter/src/lib.rs +++ b/libimagentryfilter/src/lib.rs @@ -1,3 +1,8 @@ -#[test] -fn it_works() { -} +#[macro_use] extern crate log; + +extern crate itertools; +extern crate regex; +extern crate toml; + +extern crate libimagstore; + From 10050db42f509c6f99e6fed95bb9aeca9ab77ef1 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Tue, 2 Feb 2016 14:20:15 +0100 Subject: [PATCH 03/14] Add initial file structure --- libimagentryfilter/src/builtin/mod.rs | 0 libimagentryfilter/src/cli.rs | 0 libimagentryfilter/src/filter.rs | 0 libimagentryfilter/src/lib.rs | 4 ++++ libimagentryfilter/src/ops/and.rs | 0 libimagentryfilter/src/ops/mod.rs | 3 +++ libimagentryfilter/src/ops/not.rs | 0 libimagentryfilter/src/ops/or.rs | 0 8 files changed, 7 insertions(+) create mode 100644 libimagentryfilter/src/builtin/mod.rs create mode 100644 libimagentryfilter/src/cli.rs create mode 100644 libimagentryfilter/src/filter.rs create mode 100644 libimagentryfilter/src/ops/and.rs create mode 100644 libimagentryfilter/src/ops/mod.rs create mode 100644 libimagentryfilter/src/ops/not.rs create mode 100644 libimagentryfilter/src/ops/or.rs diff --git a/libimagentryfilter/src/builtin/mod.rs b/libimagentryfilter/src/builtin/mod.rs new file mode 100644 index 00000000..e69de29b diff --git a/libimagentryfilter/src/cli.rs b/libimagentryfilter/src/cli.rs new file mode 100644 index 00000000..e69de29b diff --git a/libimagentryfilter/src/filter.rs b/libimagentryfilter/src/filter.rs new file mode 100644 index 00000000..e69de29b diff --git a/libimagentryfilter/src/lib.rs b/libimagentryfilter/src/lib.rs index 7d7c4139..0a3c4316 100644 --- a/libimagentryfilter/src/lib.rs +++ b/libimagentryfilter/src/lib.rs @@ -6,3 +6,7 @@ extern crate toml; extern crate libimagstore; +pub mod cli; +pub mod builtin; +pub mod filter; +pub mod ops; diff --git a/libimagentryfilter/src/ops/and.rs b/libimagentryfilter/src/ops/and.rs new file mode 100644 index 00000000..e69de29b diff --git a/libimagentryfilter/src/ops/mod.rs b/libimagentryfilter/src/ops/mod.rs new file mode 100644 index 00000000..668eedc5 --- /dev/null +++ b/libimagentryfilter/src/ops/mod.rs @@ -0,0 +1,3 @@ +pub mod and; +pub mod not; +pub mod or; diff --git a/libimagentryfilter/src/ops/not.rs b/libimagentryfilter/src/ops/not.rs new file mode 100644 index 00000000..e69de29b diff --git a/libimagentryfilter/src/ops/or.rs b/libimagentryfilter/src/ops/or.rs new file mode 100644 index 00000000..e69de29b From 0fb331f25a5677c4d8aef3bc86b0c523f6d4b8c0 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Tue, 2 Feb 2016 14:48:05 +0100 Subject: [PATCH 04/14] Add Filter trait and operators --- libimagentryfilter/src/filter.rs | 30 ++++++++++++++++++++++++++++++ libimagentryfilter/src/ops/and.rs | 24 ++++++++++++++++++++++++ libimagentryfilter/src/ops/not.rs | 23 +++++++++++++++++++++++ libimagentryfilter/src/ops/or.rs | 24 ++++++++++++++++++++++++ 4 files changed, 101 insertions(+) diff --git a/libimagentryfilter/src/filter.rs b/libimagentryfilter/src/filter.rs index e69de29b..8fa605b5 100644 --- a/libimagentryfilter/src/filter.rs +++ b/libimagentryfilter/src/filter.rs @@ -0,0 +1,30 @@ +use libimagstore::store::Entry; + +pub use ops::and::And; +pub use ops::not::Not; +pub use ops::or::Or; + +pub trait Filter { + + fn filter(&self, &Entry) -> bool; + + fn not(self) -> Not + where Self: Sized + 'static + { + Not::new(Box::new(self)) + } + + fn or(self, other: Box) -> Or + where Self: Sized + 'static + { + Or::new(Box::new(self), other) + } + + fn and(self, other: Box) -> And + where Self: Sized + 'static + { + And::new(Box::new(self), other) + } + +} + diff --git a/libimagentryfilter/src/ops/and.rs b/libimagentryfilter/src/ops/and.rs index e69de29b..79470b9b 100644 --- a/libimagentryfilter/src/ops/and.rs +++ b/libimagentryfilter/src/ops/and.rs @@ -0,0 +1,24 @@ +use libimagstore::store::Entry; + +use filter::Filter; + +pub struct And { + a: Box, + b: Box +} + +impl And { + + pub fn new(a: Box, b: Box) -> And { + And { a: a, b: b } + } + +} + +impl Filter for And { + + fn filter(&self, e: &Entry) -> bool { + self.a.filter(e) && self.b.filter(e) + } + +} diff --git a/libimagentryfilter/src/ops/not.rs b/libimagentryfilter/src/ops/not.rs index e69de29b..a709d95b 100644 --- a/libimagentryfilter/src/ops/not.rs +++ b/libimagentryfilter/src/ops/not.rs @@ -0,0 +1,23 @@ +use libimagstore::store::Entry; + +use filter::Filter; + +pub struct Not { + a: Box +} + +impl Not { + + pub fn new(a: Box) -> Not { + Not { a: a } + } + +} + +impl Filter for Not { + + fn filter(&self, e: &Entry) -> bool { + !self.a.filter(e) + } + +} diff --git a/libimagentryfilter/src/ops/or.rs b/libimagentryfilter/src/ops/or.rs index e69de29b..e2ca810b 100644 --- a/libimagentryfilter/src/ops/or.rs +++ b/libimagentryfilter/src/ops/or.rs @@ -0,0 +1,24 @@ +use libimagstore::store::Entry; + +use filter::Filter; + +pub struct Or { + a: Box, + b: Box +} + +impl Or { + + pub fn new(a: Box, b: Box) -> Or { + Or { a: a, b: b } + } + +} + +impl Filter for Or { + + fn filter(&self, e: &Entry) -> bool { + self.a.filter(e) || self.b.filter(e) + } + +} From 012ca4a4278764b9f90587bb550e48e7ba184acd Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Tue, 2 Feb 2016 15:27:17 +0100 Subject: [PATCH 05/14] Initialize structure for header builtins --- libimagentryfilter/src/builtin/content/mod.rs | 0 .../src/builtin/header/field_eq.rs | 36 +++++++++++++++++ .../src/builtin/header/field_path/element.rs | 18 +++++++++ .../src/builtin/header/field_path/error.rs | 39 +++++++++++++++++++ .../src/builtin/header/field_path/mod.rs | 35 +++++++++++++++++ libimagentryfilter/src/builtin/header/mod.rs | 3 ++ libimagentryfilter/src/builtin/mod.rs | 2 + 7 files changed, 133 insertions(+) create mode 100644 libimagentryfilter/src/builtin/content/mod.rs create mode 100644 libimagentryfilter/src/builtin/header/field_eq.rs create mode 100644 libimagentryfilter/src/builtin/header/field_path/element.rs create mode 100644 libimagentryfilter/src/builtin/header/field_path/error.rs create mode 100644 libimagentryfilter/src/builtin/header/field_path/mod.rs create mode 100644 libimagentryfilter/src/builtin/header/mod.rs diff --git a/libimagentryfilter/src/builtin/content/mod.rs b/libimagentryfilter/src/builtin/content/mod.rs new file mode 100644 index 00000000..e69de29b diff --git a/libimagentryfilter/src/builtin/header/field_eq.rs b/libimagentryfilter/src/builtin/header/field_eq.rs new file mode 100644 index 00000000..fc4e1e77 --- /dev/null +++ b/libimagentryfilter/src/builtin/header/field_eq.rs @@ -0,0 +1,36 @@ +use libimagstore::store::Entry; + +use builtin::header::field_path::FieldPath; +use filter::Filter; + +use toml::Value; + +/// Check whether certain header field in a entry is equal to a value +pub struct FieldEq { + header_field_path: FieldPath, + expected_value: Value +} + +impl FieldEq { + + pub fn new(path: FieldPath, expected_value: Value) -> FieldEq { + FieldEq { + header_field_path: path, + expected_value: expected_value, + } + } + +} + +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()) + .unwrap_or(false) + } + +} + diff --git a/libimagentryfilter/src/builtin/header/field_path/element.rs b/libimagentryfilter/src/builtin/header/field_path/element.rs new file mode 100644 index 00000000..044289aa --- /dev/null +++ b/libimagentryfilter/src/builtin/header/field_path/element.rs @@ -0,0 +1,18 @@ +use std::fmt::{Display, Formatter}; +use std::fmt::Error as FmtError; + +#[derive(Debug)] +pub struct FieldPathElement { + name: String +} + +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 new file mode 100644 index 00000000..69703cbf --- /dev/null +++ b/libimagentryfilter/src/builtin/header/field_path/error.rs @@ -0,0 +1,39 @@ +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 new file mode 100644 index 00000000..0a8e61df --- /dev/null +++ b/libimagentryfilter/src/builtin/header/field_path/mod.rs @@ -0,0 +1,35 @@ +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 { + unimplemented!() + } + + pub fn compile(source: String) -> Result { + unimplemented!() + } + + pub fn walk(&self, e: &EntryHeader) -> Option { + unimplemented!() + } + +} + diff --git a/libimagentryfilter/src/builtin/header/mod.rs b/libimagentryfilter/src/builtin/header/mod.rs new file mode 100644 index 00000000..14b98d62 --- /dev/null +++ b/libimagentryfilter/src/builtin/header/mod.rs @@ -0,0 +1,3 @@ +pub mod field_eq; +pub mod field_path; + diff --git a/libimagentryfilter/src/builtin/mod.rs b/libimagentryfilter/src/builtin/mod.rs index e69de29b..260f1a1b 100644 --- a/libimagentryfilter/src/builtin/mod.rs +++ b/libimagentryfilter/src/builtin/mod.rs @@ -0,0 +1,2 @@ +pub mod content; +pub mod header; From 0dc88877c6c27babaf2ece32e6a7145a40d17713 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Tue, 2 Feb 2016 15:53:49 +0100 Subject: [PATCH 06/14] Implement FieldPath::walk() --- .../src/builtin/header/field_path/element.rs | 31 ++++++++++++++++++- .../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 { + 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)) + }) } } From 41564a7d8e2d7c1606b0cb1a17f4d5c7f7d5124e Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Tue, 2 Feb 2016 17:21:08 +0100 Subject: [PATCH 07/14] Add filter: header::field_grep::FieldGrep --- .../src/builtin/header/field_grep.rs | 43 +++++++++++++++++++ libimagentryfilter/src/builtin/header/mod.rs | 1 + 2 files changed, 44 insertions(+) create mode 100644 libimagentryfilter/src/builtin/header/field_grep.rs diff --git a/libimagentryfilter/src/builtin/header/field_grep.rs b/libimagentryfilter/src/builtin/header/field_grep.rs new file mode 100644 index 00000000..f1535602 --- /dev/null +++ b/libimagentryfilter/src/builtin/header/field_grep.rs @@ -0,0 +1,43 @@ +use regex::Regex; +use toml::Value; + +use libimagstore::store::Entry; + +use builtin::header::field_path::FieldPath; +use filter::Filter; + +/// Check whether certain header field in a entry is equal to a value +pub struct FieldGrep { + header_field_path: FieldPath, + grep: Regex, +} + +impl FieldGrep { + + pub fn new(path: FieldPath, grep: Regex) -> FieldGrep { + FieldGrep { + header_field_path: path, + grep: grep, + } + } + +} + +impl Filter for FieldGrep { + + fn filter(&self, e: &Entry) -> bool { + let header = e.get_header(); + self.header_field_path + .walk(header) + .map(|v| { + match v { + Value::String(s) => self.grep.captures(&s[..]).is_some(), + _ => false, + } + }) + .unwrap_or(false) + } + +} + + diff --git a/libimagentryfilter/src/builtin/header/mod.rs b/libimagentryfilter/src/builtin/header/mod.rs index 14b98d62..c42547df 100644 --- a/libimagentryfilter/src/builtin/header/mod.rs +++ b/libimagentryfilter/src/builtin/header/mod.rs @@ -1,3 +1,4 @@ pub mod field_eq; +pub mod field_grep; pub mod field_path; From d4cee5459fec9bf5f90bb2f140134fbf91feec79 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Tue, 2 Feb 2016 17:54:02 +0100 Subject: [PATCH 08/14] Implement 3-{and,or} variants --- libimagentryfilter/src/filter.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/libimagentryfilter/src/filter.rs b/libimagentryfilter/src/filter.rs index 8fa605b5..dd8128e4 100644 --- a/libimagentryfilter/src/filter.rs +++ b/libimagentryfilter/src/filter.rs @@ -20,11 +20,23 @@ pub trait Filter { Or::new(Box::new(self), other) } + fn or3(self, other: Box, other2: Box) -> Or + where Self: Sized + 'static + { + Or::new(Box::new(self), Box::new(Or::new(other, other2))) + } + fn and(self, other: Box) -> And where Self: Sized + 'static { And::new(Box::new(self), other) } + fn and3(self, other: Box, other2: Box) -> And + where Self: Sized + 'static + { + And::new(Box::new(self), Box::new(And::new(other, other2))) + } + } From 34e62aaade1a34ca273e8190c6f5fbaff8591346 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Tue, 2 Feb 2016 17:57:27 +0100 Subject: [PATCH 09/14] Implement {and,or}_not() variants --- libimagentryfilter/src/filter.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/libimagentryfilter/src/filter.rs b/libimagentryfilter/src/filter.rs index dd8128e4..2d0015a1 100644 --- a/libimagentryfilter/src/filter.rs +++ b/libimagentryfilter/src/filter.rs @@ -20,6 +20,12 @@ pub trait Filter { Or::new(Box::new(self), other) } + fn or_not(self, other: Box) -> Or + where Self: Sized + 'static + { + self.or(Box::new(Not::new(other))) + } + fn or3(self, other: Box, other2: Box) -> Or where Self: Sized + 'static { @@ -38,5 +44,11 @@ pub trait Filter { And::new(Box::new(self), Box::new(And::new(other, other2))) } + fn and_not(self, other: Box) -> And + where Self: Sized + 'static + { + self.and(Box::new(Not::new(other))) + } + } From 12443f631bf3ccc6ccb09d60ec59e837cee62400 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Wed, 3 Feb 2016 16:18:38 +0100 Subject: [PATCH 10/14] Add builtin header check: Field is type --- .../src/builtin/header/field_istype.rs | 62 +++++++++++++++++++ libimagentryfilter/src/builtin/header/mod.rs | 1 + 2 files changed, 63 insertions(+) create mode 100644 libimagentryfilter/src/builtin/header/field_istype.rs diff --git a/libimagentryfilter/src/builtin/header/field_istype.rs b/libimagentryfilter/src/builtin/header/field_istype.rs new file mode 100644 index 00000000..23191611 --- /dev/null +++ b/libimagentryfilter/src/builtin/header/field_istype.rs @@ -0,0 +1,62 @@ +use libimagstore::store::Entry; + +use builtin::header::field_path::FieldPath; +use filter::Filter; + +use toml::Value; + +pub enum Type { + Array, + Boolean, + Float, + Integer, + None, + String, + Table, +} + +impl Type { + + fn matches(&self, v: &Value) -> bool { + match (self, v) { + (&Type::String, &Value::String(_)) => true, + (&Type::Integer, &Value::Integer(_)) => true, + (&Type::Float, &Value::Float(_)) => true, + (&Type::Boolean, &Value::Boolean(_)) => true, + (&Type::Array, &Value::Array(_)) => true, + (&Type::Table, &Value::Table(_)) => true, + _ => false, + } + } + +} + +pub struct FieldIsType { + header_field_path: FieldPath, + expected_type: Type, +} + +impl FieldIsType { + + pub fn new(path: FieldPath, expected_type: Type) -> FieldIsType { + FieldIsType { + header_field_path: path, + expected_type: expected_type, + } + } + +} + +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)) + .unwrap_or(false) + } + +} + + diff --git a/libimagentryfilter/src/builtin/header/mod.rs b/libimagentryfilter/src/builtin/header/mod.rs index c42547df..1dd2cb69 100644 --- a/libimagentryfilter/src/builtin/header/mod.rs +++ b/libimagentryfilter/src/builtin/header/mod.rs @@ -1,4 +1,5 @@ pub mod field_eq; pub mod field_grep; +pub mod field_istype; pub mod field_path; From 780410f29dd2e1bd91b3c75502b3916013e2252e Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Wed, 3 Feb 2016 16:23:39 +0100 Subject: [PATCH 11/14] Add builtin header check: Field exists --- .../src/builtin/header/field_exists.rs | 31 +++++++++++++++++++ libimagentryfilter/src/builtin/header/mod.rs | 3 +- 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 libimagentryfilter/src/builtin/header/field_exists.rs diff --git a/libimagentryfilter/src/builtin/header/field_exists.rs b/libimagentryfilter/src/builtin/header/field_exists.rs new file mode 100644 index 00000000..45bc870a --- /dev/null +++ b/libimagentryfilter/src/builtin/header/field_exists.rs @@ -0,0 +1,31 @@ +use libimagstore::store::Entry; + +use builtin::header::field_path::FieldPath; +use filter::Filter; + +use toml::Value; + +pub struct FieldExists { + header_field_path: FieldPath, +} + +impl FieldExists { + + pub fn new(path: FieldPath) -> FieldExists { + FieldExists { + header_field_path: path, + } + } + +} + +impl Filter for FieldExists { + + fn filter(&self, e: &Entry) -> bool { + let header = e.get_header(); + self.header_field_path.walk(header).is_some() + } + +} + + diff --git a/libimagentryfilter/src/builtin/header/mod.rs b/libimagentryfilter/src/builtin/header/mod.rs index 1dd2cb69..886e5ce6 100644 --- a/libimagentryfilter/src/builtin/header/mod.rs +++ b/libimagentryfilter/src/builtin/header/mod.rs @@ -1,5 +1,6 @@ pub mod field_eq; +pub mod field_exists; pub mod field_grep; +pub mod field_isempty; pub mod field_istype; pub mod field_path; - From bcf8cf0447084f9ddb94312891cabc6fcce16c0d Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Wed, 3 Feb 2016 16:26:01 +0100 Subject: [PATCH 12/14] Add builtin header check: Field is empty --- .../src/builtin/header/field_isempty.rs | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 libimagentryfilter/src/builtin/header/field_isempty.rs diff --git a/libimagentryfilter/src/builtin/header/field_isempty.rs b/libimagentryfilter/src/builtin/header/field_isempty.rs new file mode 100644 index 00000000..83f74984 --- /dev/null +++ b/libimagentryfilter/src/builtin/header/field_isempty.rs @@ -0,0 +1,45 @@ +use libimagstore::store::Entry; + +use builtin::header::field_path::FieldPath; +use filter::Filter; + +use toml::Value; + +pub struct FieldIsEmpty { + header_field_path: FieldPath, +} + +impl FieldIsEmpty { + + pub fn new(path: FieldPath) -> FieldIsEmpty { + FieldIsEmpty { + header_field_path: path, + } + } + +} + +impl Filter for FieldIsEmpty { + + fn filter(&self, e: &Entry) -> bool { + let header = e.get_header(); + self.header_field_path + .walk(header) + .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, + } + }) + .unwrap_or(false) + } + +} + + + From f81190fb8ae92ca27f25aca2fc97abf5d2f25d29 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Wed, 3 Feb 2016 16:33:01 +0100 Subject: [PATCH 13/14] Add builtin content filter: grep --- .../src/builtin/content/grep.rs | 57 +++++++++++++++++++ libimagentryfilter/src/builtin/content/mod.rs | 1 + 2 files changed, 58 insertions(+) create mode 100644 libimagentryfilter/src/builtin/content/grep.rs diff --git a/libimagentryfilter/src/builtin/content/grep.rs b/libimagentryfilter/src/builtin/content/grep.rs new file mode 100644 index 00000000..a3d89af4 --- /dev/null +++ b/libimagentryfilter/src/builtin/content/grep.rs @@ -0,0 +1,57 @@ +use std::convert::Into; + +use regex::Regex; +use regex::Error as RError; + +use libimagstore::store::Entry; + +use builtin::header::field_path::FieldPath; +use filter::Filter; + +pub trait IntoRegex { + + fn into_regex(self) -> Result; + +} + +impl<'a> IntoRegex for &'a str { + + fn into_regex(self) -> Result { + Regex::new(self) + } +} + +impl<'a> IntoRegex for Regex { + + fn into_regex(self) -> Result { + Ok(self) + } +} + +pub struct ContentGrep { + regex: Regex, +} + +impl ContentGrep { + + pub fn new(regex: IR) -> Result + where IR: IntoRegex + { + regex.into_regex() + .map(|reg| { + ContentGrep { + regex: reg, + } + }) + } + +} + +impl Filter for ContentGrep { + + fn filter(&self, e: &Entry) -> bool { + self.regex.captures(&e.get_content()[..]).is_some() + } + +} + diff --git a/libimagentryfilter/src/builtin/content/mod.rs b/libimagentryfilter/src/builtin/content/mod.rs index e69de29b..21e9e600 100644 --- a/libimagentryfilter/src/builtin/content/mod.rs +++ b/libimagentryfilter/src/builtin/content/mod.rs @@ -0,0 +1 @@ +pub mod grep; From a00092c8be3c816c18ef48891bd86bb511c53862 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Wed, 3 Feb 2016 16:37:48 +0100 Subject: [PATCH 14/14] Add builtin content filter: length filtering --- .../src/builtin/content/length/is_over.rs | 28 +++++++++++++++++++ .../src/builtin/content/length/is_under.rs | 28 +++++++++++++++++++ .../src/builtin/content/length/mod.rs | 2 ++ libimagentryfilter/src/builtin/content/mod.rs | 1 + 4 files changed, 59 insertions(+) create mode 100644 libimagentryfilter/src/builtin/content/length/is_over.rs create mode 100644 libimagentryfilter/src/builtin/content/length/is_under.rs create mode 100644 libimagentryfilter/src/builtin/content/length/mod.rs diff --git a/libimagentryfilter/src/builtin/content/length/is_over.rs b/libimagentryfilter/src/builtin/content/length/is_over.rs new file mode 100644 index 00000000..f8016805 --- /dev/null +++ b/libimagentryfilter/src/builtin/content/length/is_over.rs @@ -0,0 +1,28 @@ +use libimagstore::store::Entry; + +use builtin::header::field_path::FieldPath; +use filter::Filter; + +pub struct ContentLengthIsOver { + val: usize +} + +impl ContentLengthIsOver { + + pub fn new(value: usize) -> ContentLengthIsOver { + ContentLengthIsOver { + val: value, + } + } + +} + +impl Filter for ContentLengthIsOver { + + fn filter(&self, e: &Entry) -> bool { + e.get_content().len() > self.val + } + +} + + diff --git a/libimagentryfilter/src/builtin/content/length/is_under.rs b/libimagentryfilter/src/builtin/content/length/is_under.rs new file mode 100644 index 00000000..07ade8dc --- /dev/null +++ b/libimagentryfilter/src/builtin/content/length/is_under.rs @@ -0,0 +1,28 @@ +use libimagstore::store::Entry; + +use builtin::header::field_path::FieldPath; +use filter::Filter; + +pub struct ContentLengthIsUnder { + val: usize +} + +impl ContentLengthIsUnder { + + pub fn new(value: usize) -> ContentLengthIsUnder { + ContentLengthIsUnder { + val: value, + } + } + +} + +impl Filter for ContentLengthIsUnder { + + fn filter(&self, e: &Entry) -> bool { + e.get_content().len() < self.val + } + +} + + diff --git a/libimagentryfilter/src/builtin/content/length/mod.rs b/libimagentryfilter/src/builtin/content/length/mod.rs new file mode 100644 index 00000000..94121f96 --- /dev/null +++ b/libimagentryfilter/src/builtin/content/length/mod.rs @@ -0,0 +1,2 @@ +pub mod is_over; +pub mod is_under; diff --git a/libimagentryfilter/src/builtin/content/mod.rs b/libimagentryfilter/src/builtin/content/mod.rs index 21e9e600..b0ec6127 100644 --- a/libimagentryfilter/src/builtin/content/mod.rs +++ b/libimagentryfilter/src/builtin/content/mod.rs @@ -1 +1,2 @@ pub mod grep; +pub mod length;