From 77be32d80f8ca60e10fe98f0eebf4bdf9de9f754 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 11 Feb 2018 19:58:53 +0100 Subject: [PATCH] Remove "get" iterator, store provides this now --- bin/domain/imag-timetrack/src/cont.rs | 130 ++++++++---------- bin/domain/imag-timetrack/src/day.rs | 41 +++--- bin/domain/imag-timetrack/src/list.rs | 81 +++++------ bin/domain/imag-timetrack/src/month.rs | 41 +++--- bin/domain/imag-timetrack/src/stop.rs | 6 +- bin/domain/imag-timetrack/src/week.rs | 41 +++--- bin/domain/imag-timetrack/src/year.rs | 41 +++--- lib/domain/libimagtimetrack/src/iter/get.rs | 63 --------- lib/domain/libimagtimetrack/src/iter/mod.rs | 1 - .../libimagtimetrack/src/timetrackingstore.rs | 15 +- 10 files changed, 199 insertions(+), 261 deletions(-) delete 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 905dc07c..a23d4e21 100644 --- a/bin/domain/imag-timetrack/src/cont.rs +++ b/bin/domain/imag-timetrack/src/cont.rs @@ -33,77 +33,69 @@ use libimagtimetrack::iter::filter::*; use libimagrt::runtime::Runtime; pub fn cont(rt: &Runtime) -> i32 { - rt.store() + let groups = rt.store() .get_timetrackings() - .and_then(|iter| { - let groups = iter - // unwrap everything, trace errors - .trace_unwrap() - - // I want all entries with an end time - .filter(|e| has_end_time.filter(&e)) - - // Now group them by the end time - .group_by(|elem| match elem.get_end_datetime() { - Ok(Some(dt)) => dt, - Ok(None) => { - // error. We expect all of them having an end-time. - error!("Has no end time, but should be filtered out: {:?}", elem); - error!("This is a bug. Please report."); - error!("Will panic now"); - panic!("Unknown bug") - } - Err(e) => { - trace_error(&e); - NaiveDateTime::from_timestamp(0, 0) // placeholder - } - }); - - // sort the trackings by key, so by end datetime - let elements = { - let mut v = vec![]; - for (key, value) in groups.into_iter() { - v.push((key, value)); - } - - v.into_iter() - .sorted_by(|t1, t2| { - let (k1, _) = *t1; - let (k2, _) = *t2; - Ord::cmp(&k1, &k2) - }) - .into_iter() - - // get the last one, which should be the highest one - .last() // -> Option<_> - }; - - match elements { - Some((_, trackings)) => { - // and then, for all trackings - trackings - .fold(Ok(0), |acc, tracking| { - debug!("Having tracking: {:?}", tracking); - - acc.and_then(|_| { - // create a new tracking with the same tag - tracking - .get_timetrack_tag() - .and_then(|tag| rt.store().create_timetracking_now(&tag)) - .map(|_| 0) - .map_err_trace() - }) - }) - }, - - None => { - info!("No trackings to continue"); - Ok(1) - }, + .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, + Ok(None) => { + // error. We expect all of them having an end-time. + error!("Has no end time, but should be filtered out: {:?}", elem); + error!("This is a bug. Please report."); + error!("Will panic now"); + panic!("Unknown bug") } + Err(e) => { + trace_error(&e); + NaiveDateTime::from_timestamp(0, 0) // placeholder + } + }); + + // sort the trackings by key, so by end datetime + let elements = { + let mut v = vec![]; + for (key, value) in groups.into_iter() { + v.push((key, value)); + } + + v.into_iter() + .sorted_by(|t1, t2| { + let (k1, _) = *t1; + let (k2, _) = *t2; + Ord::cmp(&k1, &k2) }) - .map(|_| 0) - .map_err_trace() - .unwrap_or(1) + .into_iter() + + // get the last one, which should be the highest one + .last() // -> Option<_> + }; + + match elements { + Some((_, trackings)) => { + // and then, for all trackings + trackings + .fold(Ok(0), |acc, tracking| { + debug!("Having tracking: {:?}", tracking); + + acc.and_then(|_| { + // create a new tracking with the same tag + tracking + .get_timetrack_tag() + .and_then(|tag| rt.store().create_timetracking_now(&tag)) + .map(|_| 0) + .map_err_trace() + }) + }) + }, + + None => { + info!("No trackings to continue"); + Ok(1) + }, + }.map_err_trace_exit_unwrap(1) } diff --git a/bin/domain/imag-timetrack/src/day.rs b/bin/domain/imag-timetrack/src/day.rs index 988a4225..d6c86b23 100644 --- a/bin/domain/imag-timetrack/src/day.rs +++ b/bin/domain/imag-timetrack/src/day.rs @@ -81,31 +81,32 @@ pub fn day(rt: &Runtime) -> i32 { rt.store() .get_timetrackings() - .and_then(|iter| { - iter.trace_unwrap() - .filter(|e| filter.filter(e)) - .fold(Ok(()), |acc, e| { - acc.and_then(|_| { - debug!("Processing {:?}", e.get_location()); + .map_err_trace_exit_unwrap(1) + .trace_unwrap() + .filter(Option::is_some) + .map(Option::unwrap) + .filter(|e| filter.filter(e)) + .fold(Ok(()), |acc: Result<(), ::libimagtimetrack::error::TimeTrackError>, e| { + acc.and_then(|_| { + debug!("Processing {:?}", e.get_location()); - let tag = e.get_timetrack_tag()?; - debug!(" -> tag = {:?}", tag); + let tag = e.get_timetrack_tag()?; + debug!(" -> tag = {:?}", tag); - let start = e.get_start_datetime()?; - debug!(" -> start = {:?}", start); + let start = e.get_start_datetime()?; + debug!(" -> start = {:?}", start); - let end = e.get_end_datetime()?; - debug!(" -> end = {:?}", end); + let end = e.get_end_datetime()?; + debug!(" -> end = {:?}", end); - match (start, end) { - (None, _) => println!("{} has no start time.", tag), - (Some(s), None) => println!("{} | {} - ...", tag, s), - (Some(s), Some(e)) => println!("{} | {} - {}", tag, s, e), - } + match (start, end) { + (None, _) => println!("{} has no start time.", tag), + (Some(s), None) => println!("{} | {} - ...", tag, s), + (Some(s), Some(e)) => println!("{} | {} - {}", tag, s, e), + } - Ok(()) - }) - }) + Ok(()) + }) }) .map(|_| 0) .map_err_trace() diff --git a/bin/domain/imag-timetrack/src/list.rs b/bin/domain/imag-timetrack/src/list.rs index cb0fd2a4..2ecf7580 100644 --- a/bin/domain/imag-timetrack/src/list.rs +++ b/bin/domain/imag-timetrack/src/list.rs @@ -30,6 +30,7 @@ use libimagerror::trace::trace_error; use libimagerror::trace::MapErrTrace; use libimagerror::iter::TraceIterator; use libimagstore::store::FileLockEntry; +use libimagtimetrack::error::TimeTrackError; use libimagtimetrack::timetrackingstore::TimeTrackStore; use libimagtimetrack::timetracking::TimeTracking; use libimagtimetrack::error::Result; @@ -120,52 +121,54 @@ pub fn list_impl(rt: &Runtime, rt.store() .get_timetrackings() - .and_then(|iter| { - iter.trace_unwrap() - .filter(|e| filter.filter(e)) - .fold(Ok(table), |acc: Result<_>, e| { - acc.and_then(|mut tab: Table| { - debug!("Processing {:?}", e.get_location()); + .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| { + debug!("Processing {:?}", e.get_location()); - let tag = e.get_timetrack_tag()?; - debug!(" -> tag = {:?}", tag); + let tag = e.get_timetrack_tag()?; + debug!(" -> tag = {:?}", tag); - let start = e.get_start_datetime()?; - debug!(" -> start = {:?}", start); + let start = e.get_start_datetime()?; + debug!(" -> start = {:?}", start); - let end = e.get_end_datetime()?; - debug!(" -> end = {:?}", end); + let end = e.get_end_datetime()?; + debug!(" -> end = {:?}", end); - let v = match (start, end) { - (None, _) => vec![String::from(tag.as_str()), String::from(""), String::from("")], - (Some(s), None) => { - vec![ - String::from(tag.as_str()), - format!("{}", s), - String::from(""), - ] - }, - (Some(s), Some(e)) => { - vec![ - String::from(tag.as_str()), - format!("{}", s), - format!("{}", e), - ] - }, - }; + let v = match (start, end) { + (None, _) => vec![String::from(tag.as_str()), String::from(""), String::from("")], + (Some(s), None) => { + vec![ + String::from(tag.as_str()), + format!("{}", s), + String::from(""), + ] + }, + (Some(s), Some(e)) => { + vec![ + String::from(tag.as_str()), + format!("{}", s), + format!("{}", e), + ] + }, + }; - let cells : Vec = v - .into_iter() - .map(|s| Cell::new(&s)) - .collect(); - tab.add_row(Row::new(cells)); + let cells : Vec = v + .into_iter() + .map(|s| Cell::new(&s)) + .collect(); + tab.add_row(Row::new(cells)); - Ok(tab) - }) - })? - .print(&mut stdout) - .map_err(|_| String::from("Failed printing table").into()) + Ok(tab) + }) }) + .map_err_trace_exit_unwrap(1) + .print(&mut stdout) + .map_err(|_| TimeTrackError::from(String::from("Failed printing table"))) .map(|_| 0) .map_err_trace() .unwrap_or(1) diff --git a/bin/domain/imag-timetrack/src/month.rs b/bin/domain/imag-timetrack/src/month.rs index af93cc97..1015a93b 100644 --- a/bin/domain/imag-timetrack/src/month.rs +++ b/bin/domain/imag-timetrack/src/month.rs @@ -96,31 +96,32 @@ pub fn month(rt: &Runtime) -> i32 { rt.store() .get_timetrackings() - .and_then(|iter| { - iter.trace_unwrap() - .filter(|e| filter.filter(e)) - .fold(Ok(()), |acc, e| { - acc.and_then(|_| { - debug!("Processing {:?}", e.get_location()); + .map_err_trace_exit_unwrap(1) + .trace_unwrap() + .filter(Option::is_some) + .map(Option::unwrap) + .filter(|e| filter.filter(e)) + .fold(Ok(()), |acc: Result<(), ::libimagtimetrack::error::TimeTrackError>, e| { + acc.and_then(|_| { + debug!("Processing {:?}", e.get_location()); - let tag = e.get_timetrack_tag()?; - debug!(" -> tag = {:?}", tag); + let tag = e.get_timetrack_tag()?; + debug!(" -> tag = {:?}", tag); - let start = e.get_start_datetime()?; - debug!(" -> start = {:?}", start); + let start = e.get_start_datetime()?; + debug!(" -> start = {:?}", start); - let end = e.get_end_datetime()?; - debug!(" -> end = {:?}", end); + let end = e.get_end_datetime()?; + debug!(" -> end = {:?}", end); - match (start, end) { - (None, _) => println!("{} has no start time.", tag), - (Some(s), None) => println!("{} | {} - ...", tag, s), - (Some(s), Some(e)) => println!("{} | {} - {}", tag, s, e), - } + match (start, end) { + (None, _) => println!("{} has no start time.", tag), + (Some(s), None) => println!("{} | {} - ...", tag, s), + (Some(s), Some(e)) => println!("{} | {} - {}", tag, s, e), + } - Ok(()) - }) - }) + Ok(()) + }) }) .map(|_| 0) .map_err_trace() diff --git a/bin/domain/imag-timetrack/src/stop.rs b/bin/domain/imag-timetrack/src/stop.rs index ff583a66..038b82d4 100644 --- a/bin/domain/imag-timetrack/src/stop.rs +++ b/bin/domain/imag-timetrack/src/stop.rs @@ -57,8 +57,10 @@ pub fn stop(rt: &Runtime) -> i32 { rt.store() .get_timetrackings() .map_err_trace_exit_unwrap(1) + .trace_unwrap() + .filter(Option::is_some) + .map(Option::unwrap) .filter_map(|tracking| { - let tracking = tracking.map_err_trace_exit_unwrap(1); let is_none = tracking .get_end_datetime() .map_err_trace_exit_unwrap(1) @@ -83,6 +85,8 @@ 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 db085a67..32d83831 100644 --- a/bin/domain/imag-timetrack/src/week.rs +++ b/bin/domain/imag-timetrack/src/week.rs @@ -88,31 +88,32 @@ pub fn week(rt: &Runtime) -> i32 { rt.store() .get_timetrackings() - .and_then(|iter| { - iter.trace_unwrap() - .filter(|e| filter.filter(e)) - .fold(Ok(()), |acc, e| { - acc.and_then(|_| { - debug!("Processing {:?}", e.get_location()); + .map_err_trace_exit_unwrap(1) + .trace_unwrap() + .filter(Option::is_some) + .map(Option::unwrap) + .filter(|e| filter.filter(e)) + .fold(Ok(()), |acc: Result<(), ::libimagtimetrack::error::TimeTrackError>, e| { + acc.and_then(|_| { + debug!("Processing {:?}", e.get_location()); - let tag = e.get_timetrack_tag()?; - debug!(" -> tag = {:?}", tag); + let tag = e.get_timetrack_tag()?; + debug!(" -> tag = {:?}", tag); - let start = e.get_start_datetime()?; - debug!(" -> start = {:?}", start); + let start = e.get_start_datetime()?; + debug!(" -> start = {:?}", start); - let end = e.get_end_datetime()?; - debug!(" -> end = {:?}", end); + let end = e.get_end_datetime()?; + debug!(" -> end = {:?}", end); - match (start, end) { - (None, _) => println!("{} has no start time.", tag), - (Some(s), None) => println!("{} | {} - ...", tag, s), - (Some(s), Some(e)) => println!("{} | {} - {}", tag, s, e), - } + match (start, end) { + (None, _) => println!("{} has no start time.", tag), + (Some(s), None) => println!("{} | {} - ...", tag, s), + (Some(s), Some(e)) => println!("{} | {} - {}", tag, s, e), + } - Ok(()) - }) - }) + Ok(()) + }) }) .map(|_| 0) .map_err_trace() diff --git a/bin/domain/imag-timetrack/src/year.rs b/bin/domain/imag-timetrack/src/year.rs index a3e94631..1af93dad 100644 --- a/bin/domain/imag-timetrack/src/year.rs +++ b/bin/domain/imag-timetrack/src/year.rs @@ -87,31 +87,32 @@ pub fn year(rt: &Runtime) -> i32 { rt.store() .get_timetrackings() - .and_then(|iter| { - iter.trace_unwrap() - .filter(|e| filter.filter(e)) - .fold(Ok(()), |acc, e| { - acc.and_then(|_| { - debug!("Processing {:?}", e.get_location()); + .map_err_trace_exit_unwrap(1) + .trace_unwrap() + .filter(Option::is_some) + .map(Option::unwrap) + .filter(|e| filter.filter(e)) + .fold(Ok(()), |acc: Result<(), ::libimagtimetrack::error::TimeTrackError>, e| { + acc.and_then(|_| { + debug!("Processing {:?}", e.get_location()); - let tag = e.get_timetrack_tag()?; - debug!(" -> tag = {:?}", tag); + let tag = e.get_timetrack_tag()?; + debug!(" -> tag = {:?}", tag); - let start = e.get_start_datetime()?; - debug!(" -> start = {:?}", start); + let start = e.get_start_datetime()?; + debug!(" -> start = {:?}", start); - let end = e.get_end_datetime()?; - debug!(" -> end = {:?}", end); + let end = e.get_end_datetime()?; + debug!(" -> end = {:?}", end); - match (start, end) { - (None, _) => println!("{} has no start time.", tag), - (Some(s), None) => println!("{} | {} - ...", tag, s), - (Some(s), Some(e)) => println!("{} | {} - {}", tag, s, e), - } + match (start, end) { + (None, _) => println!("{} has no start time.", tag), + (Some(s), None) => println!("{} | {} - ...", tag, s), + (Some(s), Some(e)) => println!("{} | {} - {}", tag, s, e), + } - Ok(()) - }) - }) + Ok(()) + }) }) .map(|_| 0) .map_err_trace() diff --git a/lib/domain/libimagtimetrack/src/iter/get.rs b/lib/domain/libimagtimetrack/src/iter/get.rs deleted file mode 100644 index e4f5c028..00000000 --- a/lib/domain/libimagtimetrack/src/iter/get.rs +++ /dev/null @@ -1,63 +0,0 @@ -// -// 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 error::TimeTrackError as TTE; -use error::TimeTrackErrorKind as TTEK; -use error::ResultExt; - -use libimagstore::store::FileLockEntry; -use libimagstore::store::Store; -use libimagstore::storeid::StoreIdIterator; - -pub struct GetTimeTrackIter<'a>{ - inner: StoreIdIterator, - store: &'a Store, -} - -impl<'a> GetTimeTrackIter<'a> { - - pub fn new(sidit: StoreIdIterator, store: &'a Store) -> GetTimeTrackIter<'a> { - GetTimeTrackIter { - inner: sidit, - store: store - } - } -} - -impl<'a> Iterator for GetTimeTrackIter<'a> { - type Item = Result, TTE>; - - fn next(&mut self) -> Option { - self.inner.next().map(|sid| { - self.store - .get(sid) - .chain_err(|| TTEK::StoreReadError)? - .ok_or(TTE::from_kind(TTEK::StoreReadError)) - }) - } -} - -// impl<'a, I> From for GetTimeTrackIter<'a, I> -// where I: Iterator, TTE>> -// { -// fn from(i: I) -> GetTimeTrackIter<'a, I> { -// GetTimeTrackIter(i) -// } -// } -// diff --git a/lib/domain/libimagtimetrack/src/iter/mod.rs b/lib/domain/libimagtimetrack/src/iter/mod.rs index ffe212c7..509d2f91 100644 --- a/lib/domain/libimagtimetrack/src/iter/mod.rs +++ b/lib/domain/libimagtimetrack/src/iter/mod.rs @@ -19,7 +19,6 @@ 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 bdc6611a..50c8412e 100644 --- a/lib/domain/libimagtimetrack/src/timetrackingstore.rs +++ b/lib/domain/libimagtimetrack/src/timetrackingstore.rs @@ -28,13 +28,12 @@ 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 error::TimeTrackErrorKind as TTEK; -use error::ResultExt; -use iter::get::GetTimeTrackIter; use tag::TimeTrackingTag as TTT; @@ -44,7 +43,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 { @@ -99,15 +98,15 @@ impl<'a> TimeTrackStore<'a> for Store { let v = Value::String(end.format(DATE_TIME_FORMAT).to_string()); fle.get_header_mut() .insert(DATE_TIME_END_HEADER_PATH, v) - .chain_err(|| TTEK::HeaderWriteError) + .map_err(From::from) .map(|_| fle) }) } - fn get_timetrackings(&'a self) -> Result> { + fn get_timetrackings(&'a self) -> Result> { self.retrieve_for_module(CRATE_NAME) - .chain_err(|| TTEK::StoreReadError) - .map(|iter| GetTimeTrackIter::new(iter, self)) + .map_err(From::from) + .map(|iter| iter.into_get_iter(self)) } }