Merge pull request #1467 from matthiasbeyer/libimagentryfilter/failable
libimagentryfilter: failable
This commit is contained in:
commit
76966bcd6c
11 changed files with 91 additions and 46 deletions
|
@ -27,6 +27,7 @@ regex = "0.2"
|
||||||
semver = "0.8"
|
semver = "0.8"
|
||||||
toml = "0.4"
|
toml = "0.4"
|
||||||
toml-query = "0.6"
|
toml-query = "0.6"
|
||||||
|
error-chain = "0.11"
|
||||||
|
|
||||||
libimagstore = { version = "0.8.0", path = "../../../lib/core/libimagstore" }
|
libimagstore = { version = "0.8.0", path = "../../../lib/core/libimagstore" }
|
||||||
libimagentrytag = { version = "0.8.0", path = "../../../lib/entry/libimagentrytag" }
|
libimagentrytag = { version = "0.8.0", path = "../../../lib/entry/libimagentrytag" }
|
||||||
|
|
|
@ -22,7 +22,9 @@ use libimagstore::store::Entry;
|
||||||
use builtin::header::field_path::FieldPath;
|
use builtin::header::field_path::FieldPath;
|
||||||
use builtin::header::field_predicate::FieldPredicate;
|
use builtin::header::field_predicate::FieldPredicate;
|
||||||
use builtin::header::field_predicate::Predicate;
|
use builtin::header::field_predicate::Predicate;
|
||||||
use filters::filter::Filter;
|
use filters::failable::filter::FailableFilter;
|
||||||
|
use error::Result;
|
||||||
|
use error::FilterError as FE;
|
||||||
|
|
||||||
use toml::Value;
|
use toml::Value;
|
||||||
|
|
||||||
|
@ -53,9 +55,10 @@ impl FieldEq {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Filter<Entry> for FieldEq {
|
impl FailableFilter<Entry> for FieldEq {
|
||||||
|
type Error = FE;
|
||||||
|
|
||||||
fn filter(&self, e: &Entry) -> bool {
|
fn filter(&self, e: &Entry) -> Result<bool> {
|
||||||
self.filter.filter(e)
|
self.filter.filter(e)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -20,7 +20,9 @@
|
||||||
use libimagstore::store::Entry;
|
use libimagstore::store::Entry;
|
||||||
|
|
||||||
use toml_query::read::TomlValueReadExt;
|
use toml_query::read::TomlValueReadExt;
|
||||||
use filters::filter::Filter;
|
use filters::failable::filter::FailableFilter;
|
||||||
|
use error::Result;
|
||||||
|
use error::FilterError as FE;
|
||||||
|
|
||||||
use builtin::header::field_path::FieldPath;
|
use builtin::header::field_path::FieldPath;
|
||||||
|
|
||||||
|
@ -38,10 +40,11 @@ impl FieldExists {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Filter<Entry> for FieldExists {
|
impl FailableFilter<Entry> for FieldExists {
|
||||||
|
type Error = FE;
|
||||||
|
|
||||||
fn filter(&self, e: &Entry) -> bool {
|
fn filter(&self, e: &Entry) -> Result<bool> {
|
||||||
e.get_header().read(&self.header_field_path[..]).is_ok()
|
e.get_header().read(&self.header_field_path[..]).map_err(FE::from).map(|o| o.is_some())
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,7 +25,9 @@ use libimagstore::store::Entry;
|
||||||
use builtin::header::field_path::FieldPath;
|
use builtin::header::field_path::FieldPath;
|
||||||
use builtin::header::field_predicate::FieldPredicate;
|
use builtin::header::field_predicate::FieldPredicate;
|
||||||
use builtin::header::field_predicate::Predicate;
|
use builtin::header::field_predicate::Predicate;
|
||||||
use filters::filter::Filter;
|
use filters::failable::filter::FailableFilter;
|
||||||
|
use error::Result;
|
||||||
|
use error::FilterError as FE;
|
||||||
|
|
||||||
struct EqGrep{
|
struct EqGrep{
|
||||||
regex: Regex
|
regex: Regex
|
||||||
|
@ -57,9 +59,10 @@ impl FieldGrep {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Filter<Entry> for FieldGrep {
|
impl FailableFilter<Entry> for FieldGrep {
|
||||||
|
type Error = FE;
|
||||||
|
|
||||||
fn filter(&self, e: &Entry) -> bool {
|
fn filter(&self, e: &Entry) -> Result<bool> {
|
||||||
self.filter.filter(e)
|
self.filter.filter(e)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -22,7 +22,9 @@ use libimagstore::store::Entry;
|
||||||
use builtin::header::field_path::FieldPath;
|
use builtin::header::field_path::FieldPath;
|
||||||
use builtin::header::field_predicate::FieldPredicate;
|
use builtin::header::field_predicate::FieldPredicate;
|
||||||
use builtin::header::field_predicate::Predicate;
|
use builtin::header::field_predicate::Predicate;
|
||||||
use filters::filter::Filter;
|
use filters::failable::filter::FailableFilter;
|
||||||
|
use error::Result;
|
||||||
|
use error::FilterError as FE;
|
||||||
|
|
||||||
use toml::Value;
|
use toml::Value;
|
||||||
|
|
||||||
|
@ -69,9 +71,10 @@ impl FieldGt {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Filter<Entry> for FieldGt {
|
impl FailableFilter<Entry> for FieldGt {
|
||||||
|
type Error = FE;
|
||||||
|
|
||||||
fn filter(&self, e: &Entry) -> bool {
|
fn filter(&self, e: &Entry) -> Result<bool> {
|
||||||
self.filter.filter(e)
|
self.filter.filter(e)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -21,7 +21,9 @@ use libimagstore::store::Entry;
|
||||||
use toml_query::read::TomlValueReadExt;
|
use toml_query::read::TomlValueReadExt;
|
||||||
|
|
||||||
use builtin::header::field_path::FieldPath;
|
use builtin::header::field_path::FieldPath;
|
||||||
use filters::filter::Filter;
|
use filters::failable::filter::FailableFilter;
|
||||||
|
use error::Result;
|
||||||
|
use error::FilterError as FE;
|
||||||
|
|
||||||
use toml::Value;
|
use toml::Value;
|
||||||
|
|
||||||
|
@ -39,23 +41,25 @@ impl FieldIsEmpty {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Filter<Entry> for FieldIsEmpty {
|
impl FailableFilter<Entry> for FieldIsEmpty {
|
||||||
|
type Error = FE;
|
||||||
|
|
||||||
fn filter(&self, e: &Entry) -> bool {
|
fn filter(&self, e: &Entry) -> Result<bool> {
|
||||||
e.get_header()
|
Ok(e
|
||||||
.read(&self.header_field_path[..])
|
.get_header()
|
||||||
|
.read(&self.header_field_path[..])?
|
||||||
.map(|v| {
|
.map(|v| {
|
||||||
match v {
|
match v {
|
||||||
Some(&Value::Array(ref a)) => a.is_empty(),
|
&Value::Array(ref a) => a.is_empty(),
|
||||||
Some(&Value::String(ref s)) => s.is_empty(),
|
&Value::String(ref s) => s.is_empty(),
|
||||||
Some(&Value::Table(ref t)) => t.is_empty(),
|
&Value::Table(ref t) => t.is_empty(),
|
||||||
Some(&Value::Boolean(_)) |
|
&Value::Boolean(_) |
|
||||||
Some(&Value::Float(_)) |
|
&Value::Float(_) |
|
||||||
Some(&Value::Integer(_)) => false,
|
&Value::Datetime(_) |
|
||||||
_ => true,
|
&Value::Integer(_) => false,
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.unwrap_or(false)
|
.unwrap_or(true))
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,7 +22,9 @@ use libimagstore::store::Entry;
|
||||||
use builtin::header::field_path::FieldPath;
|
use builtin::header::field_path::FieldPath;
|
||||||
use builtin::header::field_predicate::FieldPredicate;
|
use builtin::header::field_predicate::FieldPredicate;
|
||||||
use builtin::header::field_predicate::Predicate;
|
use builtin::header::field_predicate::Predicate;
|
||||||
use filters::filter::Filter;
|
use filters::failable::filter::FailableFilter;
|
||||||
|
use error::Result;
|
||||||
|
use error::FilterError as FE;
|
||||||
|
|
||||||
use toml::Value;
|
use toml::Value;
|
||||||
|
|
||||||
|
@ -78,9 +80,10 @@ impl FieldIsType {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Filter<Entry> for FieldIsType {
|
impl FailableFilter<Entry> for FieldIsType {
|
||||||
|
type Error = FE;
|
||||||
|
|
||||||
fn filter(&self, e: &Entry) -> bool {
|
fn filter(&self, e: &Entry) -> Result<bool> {
|
||||||
self.filter.filter(e)
|
self.filter.filter(e)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -22,7 +22,9 @@ use libimagstore::store::Entry;
|
||||||
use builtin::header::field_path::FieldPath;
|
use builtin::header::field_path::FieldPath;
|
||||||
use builtin::header::field_predicate::FieldPredicate;
|
use builtin::header::field_predicate::FieldPredicate;
|
||||||
use builtin::header::field_predicate::Predicate;
|
use builtin::header::field_predicate::Predicate;
|
||||||
use filters::filter::Filter;
|
use filters::failable::filter::FailableFilter;
|
||||||
|
use error::Result;
|
||||||
|
use error::FilterError as FE;
|
||||||
|
|
||||||
use toml::Value;
|
use toml::Value;
|
||||||
|
|
||||||
|
@ -69,9 +71,10 @@ impl FieldLt {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Filter<Entry> for FieldLt {
|
impl FailableFilter<Entry> for FieldLt {
|
||||||
|
type Error = FE;
|
||||||
|
|
||||||
fn filter(&self, e: &Entry) -> bool {
|
fn filter(&self, e: &Entry) -> Result<bool> {
|
||||||
self.filter.filter(e)
|
self.filter.filter(e)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -20,17 +20,22 @@
|
||||||
use libimagstore::store::Entry;
|
use libimagstore::store::Entry;
|
||||||
|
|
||||||
use toml_query::read::TomlValueReadExt;
|
use toml_query::read::TomlValueReadExt;
|
||||||
|
use toml::Value;
|
||||||
|
|
||||||
use builtin::header::field_path::FieldPath;
|
use builtin::header::field_path::FieldPath;
|
||||||
use filters::filter::Filter;
|
use filters::failable::filter::FailableFilter;
|
||||||
|
use error::Result;
|
||||||
use toml::Value;
|
use error::FilterError as FE;
|
||||||
|
|
||||||
pub trait Predicate {
|
pub trait Predicate {
|
||||||
fn evaluate(&self, &Value) -> bool;
|
fn evaluate(&self, &Value) -> bool;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Check whether certain header field in a entry is equal to a value
|
/// Check whether certain header field in a entry is equal to a value
|
||||||
|
///
|
||||||
|
/// # Notice
|
||||||
|
///
|
||||||
|
/// Returns false if the value is not present.
|
||||||
pub struct FieldPredicate<P: Predicate> {
|
pub struct FieldPredicate<P: Predicate> {
|
||||||
header_field_path: FieldPath,
|
header_field_path: FieldPath,
|
||||||
predicate: Box<P>,
|
predicate: Box<P>,
|
||||||
|
@ -47,15 +52,16 @@ impl<P: Predicate> FieldPredicate<P> {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<P: Predicate> Filter<Entry> for FieldPredicate<P> {
|
impl<P: Predicate> FailableFilter<Entry> for FieldPredicate<P> {
|
||||||
|
type Error = FE;
|
||||||
|
|
||||||
fn filter(&self, e: &Entry) -> bool {
|
fn filter(&self, e: &Entry) -> Result<bool> {
|
||||||
e.get_header()
|
Ok(e.get_header()
|
||||||
.read(&self.header_field_path[..])
|
.read(&self.header_field_path[..])?
|
||||||
.map(|val| val.map(|v| (*self.predicate).evaluate(v)).unwrap_or(false))
|
.map(|v| (*self.predicate).evaluate(v))
|
||||||
.unwrap_or(false)
|
.unwrap_or(false)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -17,3 +17,18 @@
|
||||||
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
//
|
//
|
||||||
|
|
||||||
|
error_chain! {
|
||||||
|
|
||||||
|
types {
|
||||||
|
FilterError, FilterErrorKind, ResultExt, Result;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreign_links {
|
||||||
|
TomlQueryError(::toml_query::error::Error);
|
||||||
|
}
|
||||||
|
|
||||||
|
errors {
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
|
@ -38,6 +38,7 @@ extern crate regex;
|
||||||
extern crate semver;
|
extern crate semver;
|
||||||
extern crate toml;
|
extern crate toml;
|
||||||
extern crate toml_query;
|
extern crate toml_query;
|
||||||
|
#[macro_use] extern crate error_chain;
|
||||||
|
|
||||||
extern crate libimagstore;
|
extern crate libimagstore;
|
||||||
extern crate libimagentrytag;
|
extern crate libimagentrytag;
|
||||||
|
@ -45,8 +46,8 @@ extern crate libimagentrytag;
|
||||||
// core functionality modules of the crate,
|
// core functionality modules of the crate,
|
||||||
// these depend only on libimagstore
|
// these depend only on libimagstore
|
||||||
|
|
||||||
pub mod cli;
|
|
||||||
pub mod builtin;
|
pub mod builtin;
|
||||||
|
pub mod error;
|
||||||
|
|
||||||
// extended functionality of the crate
|
// extended functionality of the crate
|
||||||
// these depend on other internal libraries than libimagstore and use the upper core modules for
|
// these depend on other internal libraries than libimagstore and use the upper core modules for
|
||||||
|
|
Loading…
Reference in a new issue