Add tag filtering

This commit is contained in:
Matthias Beyer 2016-02-29 20:42:11 +01:00
parent fbccce7b7c
commit 6c50d88669
2 changed files with 86 additions and 0 deletions

View file

@ -7,7 +7,16 @@ extern crate toml;
extern crate libimagstore; extern crate libimagstore;
extern crate libimagtag; extern crate libimagtag;
// core functionality modules of the crate,
// these depend only on libimagstore
pub mod cli; pub mod cli;
pub mod builtin; pub mod builtin;
pub mod filter; pub mod filter;
pub mod ops; 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;

View file

@ -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<Tag>,
}
impl HasAllTags {
pub fn new(tags: Vec<Tag>) -> 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<Tag>,
}
impl HasAnyTags {
pub fn new(tags: Vec<Tag>) -> 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))
}
}