diff --git a/lib/core/libimagstore/src/storeid.rs b/lib/core/libimagstore/src/storeid.rs index b97c4c7f..79fe4e81 100644 --- a/lib/core/libimagstore/src/storeid.rs +++ b/lib/core/libimagstore/src/storeid.rs @@ -140,14 +140,17 @@ impl StoreId { /// The collection specification _has_ to start with the module name. Otherwise this function /// may return false negatives. /// - pub fn is_in_collection(&self, colls: &[&str]) -> bool { + pub fn is_in_collection, V: AsRef<[S]>>(&self, colls: &V) -> bool { use std::path::Component; self.id .components() - .zip(colls) + .zip(colls.as_ref().iter()) .map(|(component, pred_coll)| match component { - Component::Normal(ref s) => s.to_str().map(|ref s| s == pred_coll).unwrap_or(false), + Component::Normal(ref s) => s + .to_str() + .map(|ref s| s == &pred_coll.as_ref()) + .unwrap_or(false), _ => false }) .all(|x| x) diff --git a/lib/entry/libimagentryutil/Cargo.toml b/lib/entry/libimagentryutil/Cargo.toml index 6d5b9a96..79ad7140 100644 --- a/lib/entry/libimagentryutil/Cargo.toml +++ b/lib/entry/libimagentryutil/Cargo.toml @@ -23,6 +23,7 @@ maintenance = { status = "actively-developed" } toml = "0.4" toml-query = "0.6" error-chain = "0.11" +filters = "0.2" libimagstore = { version = "0.6.0", path = "../../../lib/core/libimagstore" } libimagerror = { version = "0.6.0", path = "../../../lib/core/libimagerror" } diff --git a/lib/entry/libimagentryutil/src/isincollection.rs b/lib/entry/libimagentryutil/src/isincollection.rs new file mode 100644 index 00000000..b92d4148 --- /dev/null +++ b/lib/entry/libimagentryutil/src/isincollection.rs @@ -0,0 +1,39 @@ +// +// imag - the personal information management suite for the commandline +// Copyright (C) 2015, 2016 Matthias Beyer and contributors +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; version +// 2.1 of the License. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +// + +use filters::filter::Filter; + +use libimagstore::storeid::StoreId; + +pub struct IsInCollection>(Vec); + +impl> IsInCollection { + pub fn new(v: Vec) -> Self { + IsInCollection(v) + } +} + +impl> Filter for IsInCollection { + + fn filter(&self, sid: &StoreId) -> bool { + sid.is_in_collection(&self.0) + } + +} + diff --git a/lib/entry/libimagentryutil/src/iter.rs b/lib/entry/libimagentryutil/src/iter.rs new file mode 100644 index 00000000..2289572c --- /dev/null +++ b/lib/entry/libimagentryutil/src/iter.rs @@ -0,0 +1,45 @@ +// +// imag - the personal information management suite for the commandline +// Copyright (C) 2015, 2016 Matthias Beyer and contributors +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; version +// 2.1 of the License. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +// + +use filters::filter::Filter; + +pub trait NextWhere { + type Item; + + fn next_where(&mut self, f: &F) -> Option + where F: Filter; +} + +impl NextWhere for I + where I: Iterator +{ + type Item = T; + + fn next_where(&mut self, f: &F) -> Option + where F: Filter + { + while let Some(next) = self.next() { + if f.filter(&next) { + return Some(next); + } + } + None + } +} + diff --git a/lib/entry/libimagentryutil/src/lib.rs b/lib/entry/libimagentryutil/src/lib.rs index 1298f52f..780ffe76 100644 --- a/lib/entry/libimagentryutil/src/lib.rs +++ b/lib/entry/libimagentryutil/src/lib.rs @@ -35,6 +35,7 @@ while_true, )] +extern crate filters; extern crate toml; extern crate toml_query; #[macro_use] extern crate error_chain; @@ -44,4 +45,6 @@ extern crate libimagerror; pub mod error; pub mod isa; +pub mod isincollection; +pub mod iter;