From 89cf979b130609c65c7847a7214b7530fe6d77a0 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Thu, 12 Apr 2018 22:37:45 +0200 Subject: [PATCH] Add functionality to filter for a collection --- bin/core/imag-ids/Cargo.toml | 2 ++ bin/core/imag-ids/src/main.rs | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/bin/core/imag-ids/Cargo.toml b/bin/core/imag-ids/Cargo.toml index 00177775..891933fc 100644 --- a/bin/core/imag-ids/Cargo.toml +++ b/bin/core/imag-ids/Cargo.toml @@ -22,6 +22,8 @@ is-it-maintained-open-issues = { repository = "matthiasbeyer/imag" } maintenance = { status = "actively-developed" } [dependencies] +filters = "0.2" + libimagstore = { version = "0.7.0", path = "../../../lib/core/libimagstore" } libimagrt = { version = "0.7.0", path = "../../../lib/core/libimagrt" } libimagerror = { version = "0.7.0", path = "../../../lib/core/libimagerror" } diff --git a/bin/core/imag-ids/src/main.rs b/bin/core/imag-ids/src/main.rs index 30e0c49f..5dada73e 100644 --- a/bin/core/imag-ids/src/main.rs +++ b/bin/core/imag-ids/src/main.rs @@ -33,6 +33,7 @@ )] extern crate clap; +extern crate filters; extern crate libimagerror; extern crate libimagstore; @@ -40,12 +41,14 @@ extern crate libimagstore; use std::io::Write; +use filters::filter::Filter; use clap::{Arg, App}; use libimagrt::setup::generate_runtime_setup; use libimagerror::trace::MapErrTrace; use libimagerror::exit::ExitUnwrap; use libimagerror::io::ToExitCode; +use libimagstore::storeid::StoreId; /// No special CLI @@ -57,6 +60,29 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> { .required(false) .multiple(false) .help("Print the storepath for each id")) + + .arg(Arg::with_name("in-collection-filter") + .long("in-collection") + .short("c") + .required(false) + .takes_value(true) + .multiple(true) + .value_names(&["COLLECTION"]) + .help("Filter for ids which are only in these collections")) +} + +pub struct IsInCollectionsFilter<'a, A>(Option, ::std::marker::PhantomData<&'a str>) + where A: AsRef<[&'a str]>; + +impl<'a, A> Filter for IsInCollectionsFilter<'a, A> + where A: AsRef<[&'a str]> + 'a +{ + fn filter(&self, sid: &StoreId) -> bool { + match self.0 { + Some(ref colls) => sid.is_in_collection(colls), + None => true, + } + } } fn main() { @@ -68,9 +94,17 @@ fn main() { let print_storepath = rt.cli().is_present("print-storepath"); + let values = rt + .cli() + .values_of("in-collection-filter") + .map(|v| v.collect::>()); + + let collection_filter = IsInCollectionsFilter(values, ::std::marker::PhantomData); + rt.store() .entries() .map_err_trace_exit_unwrap(1) + .filter(|id| collection_filter.filter(id)) .map(|id| if print_storepath { id } else {