diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2018-02-01 20:22:12 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-02-01 20:22:12 +0100 |
commit | de613c9ebd6ec0e35b3b90159232cd8229aa4a3b (patch) | |
tree | 93abf677348d453be748db1c79915a431202fc7b /lib/entry | |
parent | 4c9add9deb1f32f07a031e83863aaac0580e73a9 (diff) | |
parent | 3aa2e6edec55209451b323c5806169d997c4d31a (diff) |
Merge pull request #1227 from matthiasbeyer/libimagentryutil/filtered-iterators
libimagentryutil: filtered iterators
Diffstat (limited to 'lib/entry')
-rw-r--r-- | lib/entry/libimagentryutil/Cargo.toml | 1 | ||||
-rw-r--r-- | lib/entry/libimagentryutil/src/isincollection.rs | 39 | ||||
-rw-r--r-- | lib/entry/libimagentryutil/src/iter.rs | 45 | ||||
-rw-r--r-- | lib/entry/libimagentryutil/src/lib.rs | 3 |
4 files changed, 88 insertions, 0 deletions
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 <mail@beyermatthias.de> 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<A: AsRef<str>>(Vec<A>); + +impl<A: AsRef<str>> IsInCollection<A> { + pub fn new(v: Vec<A>) -> Self { + IsInCollection(v) + } +} + +impl<A: AsRef<str>> Filter<StoreId> for IsInCollection<A> { + + 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 <mail@beyermatthias.de> 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<T> { + type Item; + + fn next_where<F>(&mut self, f: &F) -> Option<Self::Item> + where F: Filter<T>; +} + +impl<T, I> NextWhere<T> for I + where I: Iterator<Item = T> +{ + type Item = T; + + fn next_where<F>(&mut self, f: &F) -> Option<Self::Item> + where F: Filter<T> + { + 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; |