From 22fb26dc12996e19978dc63ba4d2b91e8d61ea28 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 22 Apr 2018 14:05:39 +0200 Subject: [PATCH] Replace Store::retrieve_for_module() with own iterator type --- bin/domain/imag-timetrack/src/cont.rs | 2 - bin/domain/imag-timetrack/src/day.rs | 2 - bin/domain/imag-timetrack/src/list.rs | 2 - bin/domain/imag-timetrack/src/month.rs | 2 - bin/domain/imag-timetrack/src/stop.rs | 4 -- bin/domain/imag-timetrack/src/week.rs | 2 - bin/domain/imag-timetrack/src/year.rs | 2 - lib/domain/libimagtimetrack/src/iter/get.rs | 53 +++++++++++++++++++ lib/domain/libimagtimetrack/src/iter/mod.rs | 1 + .../libimagtimetrack/src/timetrackingstore.rs | 11 ++-- 10 files changed, 58 insertions(+), 23 deletions(-) create mode 100644 lib/domain/libimagtimetrack/src/iter/get.rs diff --git a/bin/domain/imag-timetrack/src/cont.rs b/bin/domain/imag-timetrack/src/cont.rs index a23d4e21..1e81e7af 100644 --- a/bin/domain/imag-timetrack/src/cont.rs +++ b/bin/domain/imag-timetrack/src/cont.rs @@ -37,8 +37,6 @@ pub fn cont(rt: &Runtime) -> i32 { .get_timetrackings() .map_err_trace_exit_unwrap(1) .trace_unwrap() - .filter(Option::is_some) - .map(Option::unwrap) .filter(|e| has_end_time.filter(&e)) .group_by(|elem| match elem.get_end_datetime() { // Now group them by the end time Ok(Some(dt)) => dt, diff --git a/bin/domain/imag-timetrack/src/day.rs b/bin/domain/imag-timetrack/src/day.rs index 97451920..1e59a852 100644 --- a/bin/domain/imag-timetrack/src/day.rs +++ b/bin/domain/imag-timetrack/src/day.rs @@ -90,8 +90,6 @@ pub fn day(rt: &Runtime) -> i32 { .get_timetrackings() .map_err_trace_exit_unwrap(1) .trace_unwrap() - .filter(Option::is_some) - .map(Option::unwrap) .filter(|e| filter.filter(e)) .map(|e| -> Result<_, TTE> { debug!("Processing {:?}", e.get_location()); diff --git a/bin/domain/imag-timetrack/src/list.rs b/bin/domain/imag-timetrack/src/list.rs index f79493d4..1c3d7097 100644 --- a/bin/domain/imag-timetrack/src/list.rs +++ b/bin/domain/imag-timetrack/src/list.rs @@ -121,8 +121,6 @@ pub fn list_impl(rt: &Runtime, .get_timetrackings() .map_err_trace_exit_unwrap(1) .trace_unwrap() - .filter(Option::is_some) - .map(Option::unwrap) .filter(|e| filter.filter(e)) .fold(Ok(table), |acc: Result<_>, e| { acc.and_then(|mut tab: Table| { diff --git a/bin/domain/imag-timetrack/src/month.rs b/bin/domain/imag-timetrack/src/month.rs index 13414e7a..39ac1735 100644 --- a/bin/domain/imag-timetrack/src/month.rs +++ b/bin/domain/imag-timetrack/src/month.rs @@ -105,8 +105,6 @@ pub fn month(rt: &Runtime) -> i32 { .get_timetrackings() .map_err_trace_exit_unwrap(1) .trace_unwrap() - .filter(Option::is_some) - .map(Option::unwrap) .filter(|e| filter.filter(e)) .map(|e| -> Result<_, TTE> { debug!("Processing {:?}", e.get_location()); diff --git a/bin/domain/imag-timetrack/src/stop.rs b/bin/domain/imag-timetrack/src/stop.rs index cc91fda9..c4099b52 100644 --- a/bin/domain/imag-timetrack/src/stop.rs +++ b/bin/domain/imag-timetrack/src/stop.rs @@ -60,8 +60,6 @@ pub fn stop(rt: &Runtime) -> i32 { .get_timetrackings() .map_err_trace_exit_unwrap(1) .trace_unwrap() - .filter(Option::is_some) - .map(Option::unwrap) .filter_map(|tracking| { let is_none = tracking .get_end_datetime() @@ -87,8 +85,6 @@ pub fn stop(rt: &Runtime) -> i32 { .map_warn_err_str("Getting timetrackings failed") .map_err_trace_exit_unwrap(1) .trace_unwrap() - .filter(Option::is_some) - .map(Option::unwrap) // Filter all timetrackings for the ones that are not yet ended. .filter(|e| filter.filter(e)) diff --git a/bin/domain/imag-timetrack/src/week.rs b/bin/domain/imag-timetrack/src/week.rs index db1cec90..90da88b6 100644 --- a/bin/domain/imag-timetrack/src/week.rs +++ b/bin/domain/imag-timetrack/src/week.rs @@ -103,8 +103,6 @@ pub fn week(rt: &Runtime) -> i32 { .get_timetrackings() .map_err_trace_exit_unwrap(1) .trace_unwrap() - .filter(Option::is_some) - .map(Option::unwrap) .filter(|e| filter.filter(e)) .map(|e| -> Result<_, TTE> { debug!("Processing {:?}", e.get_location()); diff --git a/bin/domain/imag-timetrack/src/year.rs b/bin/domain/imag-timetrack/src/year.rs index 4db59ec2..7904c6d3 100644 --- a/bin/domain/imag-timetrack/src/year.rs +++ b/bin/domain/imag-timetrack/src/year.rs @@ -103,8 +103,6 @@ pub fn year(rt: &Runtime) -> i32 { .get_timetrackings() .map_err_trace_exit_unwrap(1) .trace_unwrap() - .filter(Option::is_some) - .map(Option::unwrap) .filter(|e| filter.filter(e)) .map(|e| -> Result<_, TTE> { debug!("Processing {:?}", e.get_location()); diff --git a/lib/domain/libimagtimetrack/src/iter/get.rs b/lib/domain/libimagtimetrack/src/iter/get.rs new file mode 100644 index 00000000..e0433958 --- /dev/null +++ b/lib/domain/libimagtimetrack/src/iter/get.rs @@ -0,0 +1,53 @@ +// +// imag - the personal information management suite for the commandline +// Copyright (C) 2015-2018 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 libimagstore::storeid::StoreIdIteratorWithStore; +use libimagstore::store::Store; +use libimagstore::store::Result as StoreResult; +use libimagstore::store::FileLockEntry; + +use constants::*; + +pub struct TimeTrackingsGetIterator<'a>(StoreIdIteratorWithStore<'a>, &'a Store); + +impl<'a> TimeTrackingsGetIterator<'a> { + pub fn new(sit: StoreIdIteratorWithStore<'a>, store: &'a Store) -> Self { + TimeTrackingsGetIterator(sit, store) + } +} + +impl<'a> Iterator for TimeTrackingsGetIterator<'a> { + type Item = StoreResult>; + + fn next(&mut self) -> Option { + while let Some(next) = self.0.next() { + if next.is_in_collection(&[CRATE_NAME]) { + return match self.1.get(next) { + Ok(Some(fle)) => Some(Ok(fle)), + Ok(None) => continue, + Err(e) => Some(Err(e)) + }; + } + } + + None + } + +} + diff --git a/lib/domain/libimagtimetrack/src/iter/mod.rs b/lib/domain/libimagtimetrack/src/iter/mod.rs index 509d2f91..ffe212c7 100644 --- a/lib/domain/libimagtimetrack/src/iter/mod.rs +++ b/lib/domain/libimagtimetrack/src/iter/mod.rs @@ -19,6 +19,7 @@ pub mod create; pub mod filter; +pub mod get; pub mod setendtime; pub mod storeid; pub mod tag; diff --git a/lib/domain/libimagtimetrack/src/timetrackingstore.rs b/lib/domain/libimagtimetrack/src/timetrackingstore.rs index 286c6d08..72e608ae 100644 --- a/lib/domain/libimagtimetrack/src/timetrackingstore.rs +++ b/lib/domain/libimagtimetrack/src/timetrackingstore.rs @@ -28,12 +28,11 @@ use toml_query::insert::TomlValueInsertExt; use libimagstore::store::Store; use libimagstore::store::FileLockEntry; -use libimagstore::iter::get::StoreGetIterator; -use libimagstore::iter::get::StoreIdGetIteratorExtension; use libimagentrydatetime::datepath::compiler::DatePathCompiler; use error::Result; use constants::*; +use iter::get::TimeTrackingsGetIterator; use tag::TimeTrackingTag as TTT; @@ -43,7 +42,7 @@ pub trait TimeTrackStore<'a> { fn create_timetracking_at(&'a self, start: &NDT, ts: &TTT) -> Result>; fn create_timetracking(&'a self, start: &NDT, end: &NDT, ts: &TTT) -> Result>; - fn get_timetrackings(&'a self) -> Result>; + fn get_timetrackings(&'a self) -> Result>; } fn now() -> NDT { @@ -103,10 +102,8 @@ impl<'a> TimeTrackStore<'a> for Store { }) } - fn get_timetrackings(&'a self) -> Result> { - self.retrieve_for_module(CRATE_NAME) - .map_err(From::from) - .map(|iter| iter.into_get_iter(self)) + fn get_timetrackings(&'a self) -> Result> { + Ok(TimeTrackingsGetIterator::new(self.entries()?, self)) } }