Remove "get" iterator, store provides this now

This commit is contained in:
Matthias Beyer 2018-02-11 19:58:53 +01:00
parent f33768abbf
commit 77be32d80f
10 changed files with 199 additions and 261 deletions

View file

@ -33,77 +33,69 @@ use libimagtimetrack::iter::filter::*;
use libimagrt::runtime::Runtime; use libimagrt::runtime::Runtime;
pub fn cont(rt: &Runtime) -> i32 { pub fn cont(rt: &Runtime) -> i32 {
rt.store() let groups = rt.store()
.get_timetrackings() .get_timetrackings()
.and_then(|iter| { .map_err_trace_exit_unwrap(1)
let groups = iter .trace_unwrap()
// unwrap everything, trace errors .filter(Option::is_some)
.trace_unwrap() .map(Option::unwrap)
.filter(|e| has_end_time.filter(&e))
// I want all entries with an end time .group_by(|elem| match elem.get_end_datetime() { // Now group them by the end time
.filter(|e| has_end_time.filter(&e)) Ok(Some(dt)) => dt,
Ok(None) => {
// Now group them by the end time // error. We expect all of them having an end-time.
.group_by(|elem| match elem.get_end_datetime() { error!("Has no end time, but should be filtered out: {:?}", elem);
Ok(Some(dt)) => dt, error!("This is a bug. Please report.");
Ok(None) => { error!("Will panic now");
// error. We expect all of them having an end-time. panic!("Unknown bug")
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)
},
} }
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) .into_iter()
.map_err_trace()
.unwrap_or(1) // 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)
} }

View file

@ -81,31 +81,32 @@ pub fn day(rt: &Runtime) -> i32 {
rt.store() rt.store()
.get_timetrackings() .get_timetrackings()
.and_then(|iter| { .map_err_trace_exit_unwrap(1)
iter.trace_unwrap() .trace_unwrap()
.filter(|e| filter.filter(e)) .filter(Option::is_some)
.fold(Ok(()), |acc, e| { .map(Option::unwrap)
acc.and_then(|_| { .filter(|e| filter.filter(e))
debug!("Processing {:?}", e.get_location()); .fold(Ok(()), |acc: Result<(), ::libimagtimetrack::error::TimeTrackError>, e| {
acc.and_then(|_| {
debug!("Processing {:?}", e.get_location());
let tag = e.get_timetrack_tag()?; let tag = e.get_timetrack_tag()?;
debug!(" -> tag = {:?}", tag); debug!(" -> tag = {:?}", tag);
let start = e.get_start_datetime()?; let start = e.get_start_datetime()?;
debug!(" -> start = {:?}", start); debug!(" -> start = {:?}", start);
let end = e.get_end_datetime()?; let end = e.get_end_datetime()?;
debug!(" -> end = {:?}", end); debug!(" -> end = {:?}", end);
match (start, end) { match (start, end) {
(None, _) => println!("{} has no start time.", tag), (None, _) => println!("{} has no start time.", tag),
(Some(s), None) => println!("{} | {} - ...", tag, s), (Some(s), None) => println!("{} | {} - ...", tag, s),
(Some(s), Some(e)) => println!("{} | {} - {}", tag, s, e), (Some(s), Some(e)) => println!("{} | {} - {}", tag, s, e),
} }
Ok(()) Ok(())
}) })
})
}) })
.map(|_| 0) .map(|_| 0)
.map_err_trace() .map_err_trace()

View file

@ -30,6 +30,7 @@ use libimagerror::trace::trace_error;
use libimagerror::trace::MapErrTrace; use libimagerror::trace::MapErrTrace;
use libimagerror::iter::TraceIterator; use libimagerror::iter::TraceIterator;
use libimagstore::store::FileLockEntry; use libimagstore::store::FileLockEntry;
use libimagtimetrack::error::TimeTrackError;
use libimagtimetrack::timetrackingstore::TimeTrackStore; use libimagtimetrack::timetrackingstore::TimeTrackStore;
use libimagtimetrack::timetracking::TimeTracking; use libimagtimetrack::timetracking::TimeTracking;
use libimagtimetrack::error::Result; use libimagtimetrack::error::Result;
@ -120,52 +121,54 @@ pub fn list_impl(rt: &Runtime,
rt.store() rt.store()
.get_timetrackings() .get_timetrackings()
.and_then(|iter| { .map_err_trace_exit_unwrap(1)
iter.trace_unwrap() .trace_unwrap()
.filter(|e| filter.filter(e)) .filter(Option::is_some)
.fold(Ok(table), |acc: Result<_>, e| { .map(Option::unwrap)
acc.and_then(|mut tab: Table| { .filter(|e| filter.filter(e))
debug!("Processing {:?}", e.get_location()); .fold(Ok(table), |acc: Result<_>, e| {
acc.and_then(|mut tab: Table| {
debug!("Processing {:?}", e.get_location());
let tag = e.get_timetrack_tag()?; let tag = e.get_timetrack_tag()?;
debug!(" -> tag = {:?}", tag); debug!(" -> tag = {:?}", tag);
let start = e.get_start_datetime()?; let start = e.get_start_datetime()?;
debug!(" -> start = {:?}", start); debug!(" -> start = {:?}", start);
let end = e.get_end_datetime()?; let end = e.get_end_datetime()?;
debug!(" -> end = {:?}", end); debug!(" -> end = {:?}", end);
let v = match (start, end) { let v = match (start, end) {
(None, _) => vec![String::from(tag.as_str()), String::from(""), String::from("")], (None, _) => vec![String::from(tag.as_str()), String::from(""), String::from("")],
(Some(s), None) => { (Some(s), None) => {
vec![ vec![
String::from(tag.as_str()), String::from(tag.as_str()),
format!("{}", s), format!("{}", s),
String::from(""), String::from(""),
] ]
}, },
(Some(s), Some(e)) => { (Some(s), Some(e)) => {
vec![ vec![
String::from(tag.as_str()), String::from(tag.as_str()),
format!("{}", s), format!("{}", s),
format!("{}", e), format!("{}", e),
] ]
}, },
}; };
let cells : Vec<Cell> = v let cells : Vec<Cell> = v
.into_iter() .into_iter()
.map(|s| Cell::new(&s)) .map(|s| Cell::new(&s))
.collect(); .collect();
tab.add_row(Row::new(cells)); tab.add_row(Row::new(cells));
Ok(tab) Ok(tab)
}) })
})?
.print(&mut stdout)
.map_err(|_| String::from("Failed printing table").into())
}) })
.map_err_trace_exit_unwrap(1)
.print(&mut stdout)
.map_err(|_| TimeTrackError::from(String::from("Failed printing table")))
.map(|_| 0) .map(|_| 0)
.map_err_trace() .map_err_trace()
.unwrap_or(1) .unwrap_or(1)

View file

@ -96,31 +96,32 @@ pub fn month(rt: &Runtime) -> i32 {
rt.store() rt.store()
.get_timetrackings() .get_timetrackings()
.and_then(|iter| { .map_err_trace_exit_unwrap(1)
iter.trace_unwrap() .trace_unwrap()
.filter(|e| filter.filter(e)) .filter(Option::is_some)
.fold(Ok(()), |acc, e| { .map(Option::unwrap)
acc.and_then(|_| { .filter(|e| filter.filter(e))
debug!("Processing {:?}", e.get_location()); .fold(Ok(()), |acc: Result<(), ::libimagtimetrack::error::TimeTrackError>, e| {
acc.and_then(|_| {
debug!("Processing {:?}", e.get_location());
let tag = e.get_timetrack_tag()?; let tag = e.get_timetrack_tag()?;
debug!(" -> tag = {:?}", tag); debug!(" -> tag = {:?}", tag);
let start = e.get_start_datetime()?; let start = e.get_start_datetime()?;
debug!(" -> start = {:?}", start); debug!(" -> start = {:?}", start);
let end = e.get_end_datetime()?; let end = e.get_end_datetime()?;
debug!(" -> end = {:?}", end); debug!(" -> end = {:?}", end);
match (start, end) { match (start, end) {
(None, _) => println!("{} has no start time.", tag), (None, _) => println!("{} has no start time.", tag),
(Some(s), None) => println!("{} | {} - ...", tag, s), (Some(s), None) => println!("{} | {} - ...", tag, s),
(Some(s), Some(e)) => println!("{} | {} - {}", tag, s, e), (Some(s), Some(e)) => println!("{} | {} - {}", tag, s, e),
} }
Ok(()) Ok(())
}) })
})
}) })
.map(|_| 0) .map(|_| 0)
.map_err_trace() .map_err_trace()

View file

@ -57,8 +57,10 @@ pub fn stop(rt: &Runtime) -> i32 {
rt.store() rt.store()
.get_timetrackings() .get_timetrackings()
.map_err_trace_exit_unwrap(1) .map_err_trace_exit_unwrap(1)
.trace_unwrap()
.filter(Option::is_some)
.map(Option::unwrap)
.filter_map(|tracking| { .filter_map(|tracking| {
let tracking = tracking.map_err_trace_exit_unwrap(1);
let is_none = tracking let is_none = tracking
.get_end_datetime() .get_end_datetime()
.map_err_trace_exit_unwrap(1) .map_err_trace_exit_unwrap(1)
@ -83,6 +85,8 @@ pub fn stop(rt: &Runtime) -> i32 {
.map_warn_err_str("Getting timetrackings failed") .map_warn_err_str("Getting timetrackings failed")
.map_err_trace_exit_unwrap(1) .map_err_trace_exit_unwrap(1)
.trace_unwrap() .trace_unwrap()
.filter(Option::is_some)
.map(Option::unwrap)
// Filter all timetrackings for the ones that are not yet ended. // Filter all timetrackings for the ones that are not yet ended.
.filter(|e| filter.filter(e)) .filter(|e| filter.filter(e))

View file

@ -88,31 +88,32 @@ pub fn week(rt: &Runtime) -> i32 {
rt.store() rt.store()
.get_timetrackings() .get_timetrackings()
.and_then(|iter| { .map_err_trace_exit_unwrap(1)
iter.trace_unwrap() .trace_unwrap()
.filter(|e| filter.filter(e)) .filter(Option::is_some)
.fold(Ok(()), |acc, e| { .map(Option::unwrap)
acc.and_then(|_| { .filter(|e| filter.filter(e))
debug!("Processing {:?}", e.get_location()); .fold(Ok(()), |acc: Result<(), ::libimagtimetrack::error::TimeTrackError>, e| {
acc.and_then(|_| {
debug!("Processing {:?}", e.get_location());
let tag = e.get_timetrack_tag()?; let tag = e.get_timetrack_tag()?;
debug!(" -> tag = {:?}", tag); debug!(" -> tag = {:?}", tag);
let start = e.get_start_datetime()?; let start = e.get_start_datetime()?;
debug!(" -> start = {:?}", start); debug!(" -> start = {:?}", start);
let end = e.get_end_datetime()?; let end = e.get_end_datetime()?;
debug!(" -> end = {:?}", end); debug!(" -> end = {:?}", end);
match (start, end) { match (start, end) {
(None, _) => println!("{} has no start time.", tag), (None, _) => println!("{} has no start time.", tag),
(Some(s), None) => println!("{} | {} - ...", tag, s), (Some(s), None) => println!("{} | {} - ...", tag, s),
(Some(s), Some(e)) => println!("{} | {} - {}", tag, s, e), (Some(s), Some(e)) => println!("{} | {} - {}", tag, s, e),
} }
Ok(()) Ok(())
}) })
})
}) })
.map(|_| 0) .map(|_| 0)
.map_err_trace() .map_err_trace()

View file

@ -87,31 +87,32 @@ pub fn year(rt: &Runtime) -> i32 {
rt.store() rt.store()
.get_timetrackings() .get_timetrackings()
.and_then(|iter| { .map_err_trace_exit_unwrap(1)
iter.trace_unwrap() .trace_unwrap()
.filter(|e| filter.filter(e)) .filter(Option::is_some)
.fold(Ok(()), |acc, e| { .map(Option::unwrap)
acc.and_then(|_| { .filter(|e| filter.filter(e))
debug!("Processing {:?}", e.get_location()); .fold(Ok(()), |acc: Result<(), ::libimagtimetrack::error::TimeTrackError>, e| {
acc.and_then(|_| {
debug!("Processing {:?}", e.get_location());
let tag = e.get_timetrack_tag()?; let tag = e.get_timetrack_tag()?;
debug!(" -> tag = {:?}", tag); debug!(" -> tag = {:?}", tag);
let start = e.get_start_datetime()?; let start = e.get_start_datetime()?;
debug!(" -> start = {:?}", start); debug!(" -> start = {:?}", start);
let end = e.get_end_datetime()?; let end = e.get_end_datetime()?;
debug!(" -> end = {:?}", end); debug!(" -> end = {:?}", end);
match (start, end) { match (start, end) {
(None, _) => println!("{} has no start time.", tag), (None, _) => println!("{} has no start time.", tag),
(Some(s), None) => println!("{} | {} - ...", tag, s), (Some(s), None) => println!("{} | {} - ...", tag, s),
(Some(s), Some(e)) => println!("{} | {} - {}", tag, s, e), (Some(s), Some(e)) => println!("{} | {} - {}", tag, s, e),
} }
Ok(()) Ok(())
}) })
})
}) })
.map(|_| 0) .map(|_| 0)
.map_err_trace() .map_err_trace()

View file

@ -1,63 +0,0 @@
//
// imag - the personal information management suite for the commandline
// Copyright (C) 2015-2018 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 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<FileLockEntry<'a>, TTE>;
fn next(&mut self) -> Option<Self::Item> {
self.inner.next().map(|sid| {
self.store
.get(sid)
.chain_err(|| TTEK::StoreReadError)?
.ok_or(TTE::from_kind(TTEK::StoreReadError))
})
}
}
// impl<'a, I> From<I> for GetTimeTrackIter<'a, I>
// where I: Iterator<Item = Result<FileLockEntry<'a>, TTE>>
// {
// fn from(i: I) -> GetTimeTrackIter<'a, I> {
// GetTimeTrackIter(i)
// }
// }
//

View file

@ -19,7 +19,6 @@
pub mod create; pub mod create;
pub mod filter; pub mod filter;
pub mod get;
pub mod setendtime; pub mod setendtime;
pub mod storeid; pub mod storeid;
pub mod tag; pub mod tag;

View file

@ -28,13 +28,12 @@ use toml_query::insert::TomlValueInsertExt;
use libimagstore::store::Store; use libimagstore::store::Store;
use libimagstore::store::FileLockEntry; use libimagstore::store::FileLockEntry;
use libimagstore::iter::get::StoreGetIterator;
use libimagstore::iter::get::StoreIdGetIteratorExtension;
use libimagentrydatetime::datepath::compiler::DatePathCompiler; use libimagentrydatetime::datepath::compiler::DatePathCompiler;
use error::Result; use error::Result;
use constants::*; use constants::*;
use error::TimeTrackErrorKind as TTEK;
use error::ResultExt;
use iter::get::GetTimeTrackIter;
use tag::TimeTrackingTag as TTT; use tag::TimeTrackingTag as TTT;
@ -44,7 +43,7 @@ pub trait TimeTrackStore<'a> {
fn create_timetracking_at(&'a self, start: &NDT, ts: &TTT) -> Result<FileLockEntry<'a>>; fn create_timetracking_at(&'a self, start: &NDT, ts: &TTT) -> Result<FileLockEntry<'a>>;
fn create_timetracking(&'a self, start: &NDT, end: &NDT, ts: &TTT) -> Result<FileLockEntry<'a>>; fn create_timetracking(&'a self, start: &NDT, end: &NDT, ts: &TTT) -> Result<FileLockEntry<'a>>;
fn get_timetrackings(&'a self) -> Result<GetTimeTrackIter<'a>>; fn get_timetrackings(&'a self) -> Result<StoreGetIterator<'a>>;
} }
fn now() -> NDT { fn now() -> NDT {
@ -99,15 +98,15 @@ impl<'a> TimeTrackStore<'a> for Store {
let v = Value::String(end.format(DATE_TIME_FORMAT).to_string()); let v = Value::String(end.format(DATE_TIME_FORMAT).to_string());
fle.get_header_mut() fle.get_header_mut()
.insert(DATE_TIME_END_HEADER_PATH, v) .insert(DATE_TIME_END_HEADER_PATH, v)
.chain_err(|| TTEK::HeaderWriteError) .map_err(From::from)
.map(|_| fle) .map(|_| fle)
}) })
} }
fn get_timetrackings(&'a self) -> Result<GetTimeTrackIter<'a>> { fn get_timetrackings(&'a self) -> Result<StoreGetIterator<'a>> {
self.retrieve_for_module(CRATE_NAME) self.retrieve_for_module(CRATE_NAME)
.chain_err(|| TTEK::StoreReadError) .map_err(From::from)
.map(|iter| GetTimeTrackIter::new(iter, self)) .map(|iter| iter.into_get_iter(self))
} }
} }