From eb6ab8f029a09562f5e2899f2ed3d0cf90a079ae Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Thu, 1 Feb 2018 15:07:22 +0100 Subject: Add IsInCollection helper type --- lib/entry/libimagentryutil/Cargo.toml | 1 + lib/entry/libimagentryutil/src/isincollection.rs | 39 ++++++++++++++++++++++++ lib/entry/libimagentryutil/src/lib.rs | 2 ++ 3 files changed, 42 insertions(+) create mode 100644 lib/entry/libimagentryutil/src/isincollection.rs (limited to 'lib/entry') 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/lib.rs b/lib/entry/libimagentryutil/src/lib.rs index 1298f52f..b66d1d62 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,5 @@ extern crate libimagerror; pub mod error; pub mod isa; +pub mod isincollection; -- cgit v1.2.3 From 3aa2e6edec55209451b323c5806169d997c4d31a Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Thu, 1 Feb 2018 15:17:08 +0100 Subject: Add iterator helper for filtering --- lib/entry/libimagentryutil/src/iter.rs | 45 ++++++++++++++++++++++++++++++++++ lib/entry/libimagentryutil/src/lib.rs | 1 + 2 files changed, 46 insertions(+) create mode 100644 lib/entry/libimagentryutil/src/iter.rs (limited to 'lib/entry') 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 b66d1d62..780ffe76 100644 --- a/lib/entry/libimagentryutil/src/lib.rs +++ b/lib/entry/libimagentryutil/src/lib.rs @@ -46,4 +46,5 @@ extern crate libimagerror; pub mod error; pub mod isa; pub mod isincollection; +pub mod iter; -- cgit v1.2.3