Move filter functionality in common module, use filters crate

This commit is contained in:
Matthias Beyer 2017-07-19 13:08:58 +02:00
parent 2c4dbc0a4a
commit ea1de45a89
4 changed files with 71 additions and 18 deletions

View file

@ -20,6 +20,7 @@ version = "2.0.1"
semver = "0.2"
toml = "^0.4"
chrono = "^0.4"
filters = "0.1.1"
[dependencies.libimagstore]
path = "../libimagstore"

View file

@ -0,0 +1,61 @@
//
// 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 libimagentrytimetrack::timetracking::TimeTracking;
use libimagentrytimetrack::tag::TimeTrackingTag;
use libimagstore::store::FileLockEntry;
use libimagerror::trace::trace_error;
/// Check whether an timetracking has an end time.
///
/// Trace, if TimeTracking::get_end_datetime() returns Err(_)
pub fn has_end_time(timetracking: &FileLockEntry) -> bool {
match timetracking.get_end_datetime() {
Ok(x) => x.is_some(),
Err(e) => {
warn!("Error checking {} whether End-time is set", timetracking.get_location());
trace_error(&e);
false
}
}
}
/// Check whether an timetracking has one of the passed tags
///
/// Trace, if TimeTracking::get_end_datetime() returns Err(_)
pub struct HasTagFromList<'a> {
list: &'a Vec<TimeTrackingTag>,
}
impl<'a> HasTagFromList<'a> {
pub fn new(v: &'a Vec<TimeTrackingTag>) -> HasTagFromList<'a> {
HasTagFromList {
list: v
}
}
}
impl<'a, 'f> Filter<FileLockEntry<'f>> for HasTagFromList<'a> {
fn filter(&self, tracking: &FileLockEntry) -> bool {
tracking.get_timetrack_tag().map(|t| self.list.contains(&t)).unwrap_or(false)
}
}

View file

@ -27,6 +27,7 @@ extern crate clap;
extern crate semver;
extern crate toml;
extern crate chrono;
extern crate filters;
extern crate libimagerror;
extern crate libimagstore;
@ -35,6 +36,7 @@ extern crate libimagentrytimetrack;
extern crate libimagutil;
mod cont;
mod common;
mod day;
mod month;
mod start;

View file

@ -19,6 +19,10 @@
use std::str::FromStr;
use filters::filter::Filter;
use common::*;
use libimagerror::trace::trace_error;
use libimagerror::iter::TraceIterator;
use libimagrt::runtime::Runtime;
@ -61,27 +65,12 @@ pub fn stop(rt: &Runtime) -> i32 {
};
let filter = has_end_time.not().and(HasTagFromList::new(&tags));
// Filter all timetrackings for the ones that are not yet ended.
iter.trace_unwrap()
.filter_map(|elem| {
// check whether end-time is set
let has_end_time = match elem.get_end_datetime() {
Ok(x) => x.is_some(),
Err(e) => {
warn!("Error checking {} whether End-time is set", elem.get_location());
trace_error(&e);
false
}
};
// Filter the not-yet-ended timetrackings for the ones that should be ended via
// the tag specification
let stopping_tag_is_present : bool = elem
.get_timetrack_tag()
.map(|t| tags.contains(&t))
.unwrap_or(false);
if (!has_end_time) && stopping_tag_is_present {
if filter.filter(&elem) {
Some(elem)
} else {
None