Merge pull request #1227 from matthiasbeyer/libimagentryutil/filtered-iterators
libimagentryutil: filtered iterators
This commit is contained in:
commit
de613c9ebd
5 changed files with 94 additions and 3 deletions
|
@ -140,14 +140,17 @@ impl StoreId {
|
||||||
/// The collection specification _has_ to start with the module name. Otherwise this function
|
/// The collection specification _has_ to start with the module name. Otherwise this function
|
||||||
/// may return false negatives.
|
/// 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;
|
use std::path::Component;
|
||||||
|
|
||||||
self.id
|
self.id
|
||||||
.components()
|
.components()
|
||||||
.zip(colls)
|
.zip(colls.as_ref().iter())
|
||||||
.map(|(component, pred_coll)| match component {
|
.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
|
_ => false
|
||||||
})
|
})
|
||||||
.all(|x| x)
|
.all(|x| x)
|
||||||
|
|
|
@ -23,6 +23,7 @@ maintenance = { status = "actively-developed" }
|
||||||
toml = "0.4"
|
toml = "0.4"
|
||||||
toml-query = "0.6"
|
toml-query = "0.6"
|
||||||
error-chain = "0.11"
|
error-chain = "0.11"
|
||||||
|
filters = "0.2"
|
||||||
|
|
||||||
libimagstore = { version = "0.6.0", path = "../../../lib/core/libimagstore" }
|
libimagstore = { version = "0.6.0", path = "../../../lib/core/libimagstore" }
|
||||||
libimagerror = { version = "0.6.0", path = "../../../lib/core/libimagerror" }
|
libimagerror = { version = "0.6.0", path = "../../../lib/core/libimagerror" }
|
||||||
|
|
39
lib/entry/libimagentryutil/src/isincollection.rs
Normal file
39
lib/entry/libimagentryutil/src/isincollection.rs
Normal 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)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
45
lib/entry/libimagentryutil/src/iter.rs
Normal file
45
lib/entry/libimagentryutil/src/iter.rs
Normal 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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -35,6 +35,7 @@
|
||||||
while_true,
|
while_true,
|
||||||
)]
|
)]
|
||||||
|
|
||||||
|
extern crate filters;
|
||||||
extern crate toml;
|
extern crate toml;
|
||||||
extern crate toml_query;
|
extern crate toml_query;
|
||||||
#[macro_use] extern crate error_chain;
|
#[macro_use] extern crate error_chain;
|
||||||
|
@ -44,4 +45,6 @@ extern crate libimagerror;
|
||||||
|
|
||||||
pub mod error;
|
pub mod error;
|
||||||
pub mod isa;
|
pub mod isa;
|
||||||
|
pub mod isincollection;
|
||||||
|
pub mod iter;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue