Merge pull request #648 from impo/libimagentryfilter/filters-rewrite

libimagentryfilter: rewrite with filters crate
This commit is contained in:
Matthias Beyer 2016-08-15 10:51:39 +02:00 committed by GitHub
commit 844ec175ed
24 changed files with 46 additions and 177 deletions

View file

@ -5,11 +5,12 @@ authors = ["Matthias Beyer <mail@beyermatthias.de>"]
[dependencies]
clap = "2.*"
filters = "0.1.*"
itertools = "0.4"
log = "0.3"
regex = "0.1"
toml = "0.2.*"
semver = "0.2.1"
toml = "0.2.*"
[dependencies.libimagstore]
path = "../libimagstore"

View file

@ -1,6 +1,6 @@
use libimagstore::store::Entry;
use filter::Filter;
use filters::filter::Filter;
pub struct BoolFilter(bool);
@ -12,7 +12,7 @@ impl BoolFilter {
}
impl Filter for BoolFilter {
impl Filter<Entry> for BoolFilter {
fn filter(&self, _: &Entry) -> bool {
self.0

View file

@ -1,10 +1,9 @@
use filters::filter::Filter;
use regex::Regex;
use regex::Error as RError;
use libimagstore::store::Entry;
use filter::Filter;
pub trait IntoRegex {
fn into_regex(self) -> Result<Regex, RError>;
@ -44,7 +43,7 @@ impl ContentGrep {
}
impl Filter for ContentGrep {
impl Filter<Entry> for ContentGrep {
fn filter(&self, e: &Entry) -> bool {
self.regex.captures(&e.get_content()[..]).is_some()

View file

@ -1,7 +1,6 @@
use filters::filter::Filter;
use libimagstore::store::Entry;
use filter::Filter;
pub struct ContentLengthIsOver {
val: usize
}
@ -16,7 +15,7 @@ impl ContentLengthIsOver {
}
impl Filter for ContentLengthIsOver {
impl Filter<Entry> for ContentLengthIsOver {
fn filter(&self, e: &Entry) -> bool {
e.get_content().len() > self.val

View file

@ -1,7 +1,6 @@
use filters::filter::Filter;
use libimagstore::store::Entry;
use filter::Filter;
pub struct ContentLengthIsUnder {
val: usize
}
@ -16,7 +15,7 @@ impl ContentLengthIsUnder {
}
impl Filter for ContentLengthIsUnder {
impl Filter<Entry> for ContentLengthIsUnder {
fn filter(&self, e: &Entry) -> bool {
e.get_content().len() < self.val

View file

@ -3,7 +3,7 @@ use libimagstore::store::Entry;
use builtin::header::field_path::FieldPath;
use builtin::header::field_predicate::FieldPredicate;
use builtin::header::field_predicate::Predicate;
use filter::Filter;
use filters::filter::Filter;
use toml::Value;
@ -34,7 +34,7 @@ impl FieldEq {
}
impl Filter for FieldEq {
impl Filter<Entry> for FieldEq {
fn filter(&self, e: &Entry) -> bool {
self.filter.filter(e)

View file

@ -1,7 +1,7 @@
use libimagstore::store::Entry;
use builtin::header::field_path::FieldPath;
use filter::Filter;
use filters::filter::Filter;
pub struct FieldExists {
header_field_path: FieldPath,
@ -17,7 +17,7 @@ impl FieldExists {
}
impl Filter for FieldExists {
impl Filter<Entry> for FieldExists {
fn filter(&self, e: &Entry) -> bool {
e.get_header().read(&self.header_field_path[..]).is_ok()

View file

@ -6,7 +6,7 @@ use libimagstore::store::Entry;
use builtin::header::field_path::FieldPath;
use builtin::header::field_predicate::FieldPredicate;
use builtin::header::field_predicate::Predicate;
use filter::Filter;
use filters::filter::Filter;
struct EqGrep{
regex: Regex
@ -38,7 +38,7 @@ impl FieldGrep {
}
impl Filter for FieldGrep {
impl Filter<Entry> for FieldGrep {
fn filter(&self, e: &Entry) -> bool {
self.filter.filter(e)

View file

@ -3,7 +3,7 @@ use libimagstore::store::Entry;
use builtin::header::field_path::FieldPath;
use builtin::header::field_predicate::FieldPredicate;
use builtin::header::field_predicate::Predicate;
use filter::Filter;
use filters::filter::Filter;
use toml::Value;
@ -50,7 +50,7 @@ impl FieldGt {
}
impl Filter for FieldGt {
impl Filter<Entry> for FieldGt {
fn filter(&self, e: &Entry) -> bool {
self.filter.filter(e)

View file

@ -1,7 +1,7 @@
use libimagstore::store::Entry;
use builtin::header::field_path::FieldPath;
use filter::Filter;
use filters::filter::Filter;
use toml::Value;
@ -19,7 +19,7 @@ impl FieldIsEmpty {
}
impl Filter for FieldIsEmpty {
impl Filter<Entry> for FieldIsEmpty {
fn filter(&self, e: &Entry) -> bool {
e.get_header()

View file

@ -3,7 +3,7 @@ use libimagstore::store::Entry;
use builtin::header::field_path::FieldPath;
use builtin::header::field_predicate::FieldPredicate;
use builtin::header::field_predicate::Predicate;
use filter::Filter;
use filters::filter::Filter;
use toml::Value;
@ -59,7 +59,7 @@ impl FieldIsType {
}
impl Filter for FieldIsType {
impl Filter<Entry> for FieldIsType {
fn filter(&self, e: &Entry) -> bool {
self.filter.filter(e)

View file

@ -3,7 +3,7 @@ use libimagstore::store::Entry;
use builtin::header::field_path::FieldPath;
use builtin::header::field_predicate::FieldPredicate;
use builtin::header::field_predicate::Predicate;
use filter::Filter;
use filters::filter::Filter;
use toml::Value;
@ -50,7 +50,7 @@ impl FieldLt {
}
impl Filter for FieldLt {
impl Filter<Entry> for FieldLt {
fn filter(&self, e: &Entry) -> bool {
self.filter.filter(e)

View file

@ -1,7 +1,7 @@
use libimagstore::store::Entry;
use builtin::header::field_path::FieldPath;
use filter::Filter;
use filters::filter::Filter;
use toml::Value;
@ -26,7 +26,7 @@ impl<P: Predicate> FieldPredicate<P> {
}
impl<P: Predicate> Filter for FieldPredicate<P> {
impl<P: Predicate> Filter<Entry> for FieldPredicate<P> {
fn filter(&self, e: &Entry) -> bool {
e.get_header()

View file

@ -3,7 +3,7 @@ use toml::Value;
use libimagstore::store::Entry;
use filter::Filter;
use filters::filter::Filter;
pub struct VersionEq {
version: Version,
@ -17,7 +17,7 @@ impl VersionEq {
}
impl Filter for VersionEq {
impl Filter<Entry> for VersionEq {
fn filter(&self, e: &Entry) -> bool {
e.get_header()

View file

@ -3,7 +3,7 @@ use toml::Value;
use libimagstore::store::Entry;
use filter::Filter;
use filters::filter::Filter;
pub struct VersionGt {
version: Version,
@ -17,7 +17,7 @@ impl VersionGt {
}
impl Filter for VersionGt {
impl Filter<Entry> for VersionGt {
fn filter(&self, e: &Entry) -> bool {
e.get_header()

View file

@ -3,7 +3,7 @@ use toml::Value;
use libimagstore::store::Entry;
use filter::Filter;
use filters::filter::Filter;
pub struct VersionLt {
version: Version,
@ -17,7 +17,7 @@ impl VersionLt {
}
impl Filter for VersionLt {
impl Filter<Entry> for VersionLt {
fn filter(&self, e: &Entry) -> bool {
e.get_header()

View file

@ -4,23 +4,23 @@ use libimagstore::store::Entry;
use builtin::header::version::gt::VersionGt;
use builtin::header::version::lt::VersionLt;
use filter::Filter;
use ops::and::And;
use ops::not::Not;
use filters::filter::Filter;
use filters::ops::and::And;
use filters::ops::not::Not;
pub struct VersionInRange {
and: And,
and: And<VersionGt, VersionLt>,
}
impl VersionInRange {
pub fn new(lowerbound: Version, upperbound: Version) -> VersionInRange {
VersionInRange { and: VersionGt::new(lowerbound).and(Box::new(VersionLt::new(upperbound))) }
VersionInRange { and: VersionGt::new(lowerbound).and(VersionLt::new(upperbound)) }
}
}
impl Filter for VersionInRange {
impl Filter<Entry> for VersionInRange {
fn filter(&self, e: &Entry) -> bool {
self.and.filter(e)
@ -29,7 +29,7 @@ impl Filter for VersionInRange {
}
pub struct VersionOutOfRange {
not: Not
not: Not<VersionInRange>
}
impl VersionOutOfRange {
@ -40,7 +40,7 @@ impl VersionOutOfRange {
}
impl Filter for VersionOutOfRange {
impl Filter<Entry> for VersionOutOfRange {
fn filter(&self, e: &Entry) -> bool {
self.not.filter(e)

View file

@ -1,54 +0,0 @@
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<Filter>) -> Or
where Self: Sized + 'static
{
Or::new(Box::new(self), other)
}
fn or_not(self, other: Box<Filter>) -> Or
where Self: Sized + 'static
{
self.or(Box::new(Not::new(other)))
}
fn or3(self, other: Box<Filter>, other2: Box<Filter>) -> Or
where Self: Sized + 'static
{
Or::new(Box::new(self), Box::new(Or::new(other, other2)))
}
fn and(self, other: Box<Filter>) -> And
where Self: Sized + 'static
{
And::new(Box::new(self), other)
}
fn and3(self, other: Box<Filter>, other2: Box<Filter>) -> And
where Self: Sized + 'static
{
And::new(Box::new(self), Box::new(And::new(other, other2)))
}
fn and_not(self, other: Box<Filter>) -> And
where Self: Sized + 'static
{
self.and(Box::new(Not::new(other)))
}
}

View file

@ -15,10 +15,11 @@
#[macro_use] extern crate log;
extern crate filters;
extern crate itertools;
extern crate regex;
extern crate toml;
extern crate semver;
extern crate toml;
extern crate libimagstore;
extern crate libimagentrytag;
@ -28,8 +29,6 @@ extern crate libimagentrytag;
pub mod cli;
pub mod builtin;
pub mod filter;
pub mod ops;
// extended functionality of the crate
// these depend on other internal libraries than libimagstore and use the upper core modules for

View file

@ -1,24 +0,0 @@
use libimagstore::store::Entry;
use filter::Filter;
pub struct And {
a: Box<Filter>,
b: Box<Filter>
}
impl And {
pub fn new(a: Box<Filter>, b: Box<Filter>) -> And {
And { a: a, b: b }
}
}
impl Filter for And {
fn filter(&self, e: &Entry) -> bool {
self.a.filter(e) && self.b.filter(e)
}
}

View file

@ -1,3 +0,0 @@
pub mod and;
pub mod not;
pub mod or;

View file

@ -1,23 +0,0 @@
use libimagstore::store::Entry;
use filter::Filter;
pub struct Not {
a: Box<Filter>
}
impl Not {
pub fn new(a: Box<Filter>) -> Not {
Not { a: a }
}
}
impl Filter for Not {
fn filter(&self, e: &Entry) -> bool {
!self.a.filter(e)
}
}

View file

@ -1,24 +0,0 @@
use libimagstore::store::Entry;
use filter::Filter;
pub struct Or {
a: Box<Filter>,
b: Box<Filter>
}
impl Or {
pub fn new(a: Box<Filter>, b: Box<Filter>) -> Or {
Or { a: a, b: b }
}
}
impl Filter for Or {
fn filter(&self, e: &Entry) -> bool {
self.a.filter(e) || self.b.filter(e)
}
}

View file

@ -2,7 +2,7 @@ use libimagstore::store::Entry;
use libimagentrytag::tagable::Tagable;
use libimagentrytag::tag::Tag;
use filter::Filter;
use filters::filter::Filter;
/// Check whether an Entry has a certain tag
pub struct HasTag {
@ -19,7 +19,7 @@ impl HasTag {
}
impl Filter for HasTag {
impl Filter<Entry> for HasTag {
fn filter(&self, e: &Entry) -> bool {
e.has_tag(&self.tag).ok().unwrap_or(false)
@ -43,7 +43,7 @@ impl HasAllTags {
}
impl Filter for HasAllTags {
impl Filter<Entry> for HasAllTags {
fn filter(&self, e: &Entry) -> bool {
e.has_tags(&self.tags).ok().unwrap_or(false)
@ -67,7 +67,7 @@ impl HasAnyTags {
}
impl Filter for HasAnyTags {
impl Filter<Entry> for HasAnyTags {
fn filter(&self, e: &Entry) -> bool {
self.tags.iter().any(|tag| e.has_tag(tag).ok().unwrap_or(false))