From 6c50d88669b1ddcd2fb8918d9e3ef004fb7995fa Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 29 Feb 2016 20:42:11 +0100 Subject: [PATCH] Add tag filtering --- libimagentryfilter/src/lib.rs | 9 ++++ libimagentryfilter/src/tags/mod.rs | 77 ++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 libimagentryfilter/src/tags/mod.rs diff --git a/libimagentryfilter/src/lib.rs b/libimagentryfilter/src/lib.rs index 80c966dd..6234c1ac 100644 --- a/libimagentryfilter/src/lib.rs +++ b/libimagentryfilter/src/lib.rs @@ -7,7 +7,16 @@ extern crate toml; extern crate libimagstore; extern crate libimagtag; +// core functionality modules of the crate, +// these depend only on libimagstore + 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 +// their functionality + +pub mod tags; diff --git a/libimagentryfilter/src/tags/mod.rs b/libimagentryfilter/src/tags/mod.rs new file mode 100644 index 00000000..6c7aaa3d --- /dev/null +++ b/libimagentryfilter/src/tags/mod.rs @@ -0,0 +1,77 @@ +use libimagstore::store::Entry; +use libimagtag::tagable::Tagable; +use libimagtag::tag::Tag; + +use filter::Filter; + +/// Check whether an Entry has a certain tag +pub struct HasTag { + tag: Tag, +} + +impl HasTag { + + pub fn new(tag: Tag) -> HasTag { + HasTag { + tag: tag, + } + } + +} + +impl Filter for HasTag { + + fn filter(&self, e: &Entry) -> bool { + e.has_tag(&self.tag).ok().unwrap_or(false) + } + +} + + +/// Check whether an Entry has all of these tags +pub struct HasAllTags { + tags: Vec, +} + +impl HasAllTags { + + pub fn new(tags: Vec) -> HasAllTags { + HasAllTags { + tags: tags, + } + } + +} + +impl Filter for HasAllTags { + + fn filter(&self, e: &Entry) -> bool { + e.has_tags(&self.tags).ok().unwrap_or(false) + } + +} + + +/// Check whether an Entry has any of these tags +pub struct HasAnyTags { + tags: Vec, +} + +impl HasAnyTags { + + pub fn new(tags: Vec) -> HasAnyTags { + HasAnyTags { + tags: tags, + } + } + +} + +impl Filter for HasAnyTags { + + fn filter(&self, e: &Entry) -> bool { + self.tags.iter().any(|tag| e.has_tag(tag).ok().unwrap_or(false)) + } + +} +