diff --git a/imag-timetrack/Cargo.toml b/imag-timetrack/Cargo.toml index 5f8956d0..290e5070 100644 --- a/imag-timetrack/Cargo.toml +++ b/imag-timetrack/Cargo.toml @@ -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" diff --git a/imag-timetrack/src/common.rs b/imag-timetrack/src/common.rs new file mode 100644 index 00000000..e2c3f364 --- /dev/null +++ b/imag-timetrack/src/common.rs @@ -0,0 +1,61 @@ +// +// 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 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, +} + +impl<'a> HasTagFromList<'a> { + pub fn new(v: &'a Vec) -> HasTagFromList<'a> { + HasTagFromList { + list: v + } + } +} + +impl<'a, 'f> Filter> for HasTagFromList<'a> { + fn filter(&self, tracking: &FileLockEntry) -> bool { + tracking.get_timetrack_tag().map(|t| self.list.contains(&t)).unwrap_or(false) + } +} + diff --git a/imag-timetrack/src/main.rs b/imag-timetrack/src/main.rs index 364ff629..800f6a9d 100644 --- a/imag-timetrack/src/main.rs +++ b/imag-timetrack/src/main.rs @@ -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; diff --git a/imag-timetrack/src/stop.rs b/imag-timetrack/src/stop.rs index b9e9cd49..f3dfaade 100644 --- a/imag-timetrack/src/stop.rs +++ b/imag-timetrack/src/stop.rs @@ -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