Merge pull request #1227 from matthiasbeyer/libimagentryutil/filtered-iterators

libimagentryutil: filtered iterators
This commit is contained in:
Matthias Beyer 2018-02-01 20:22:12 +01:00 committed by GitHub
commit de613c9ebd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 94 additions and 3 deletions

View file

@ -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<S: AsRef<str>, 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)

View file

@ -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" }

View file

@ -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)
}
}

View file

@ -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
}
}

View file

@ -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;