From ba1c6c06fac878844ac03b9bd4032067c815364b Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 11 Feb 2018 19:35:20 +0100 Subject: [PATCH] Add error chaining for compatibility with new libimagerror interface --- bin/domain/imag-timetrack/src/day.rs | 29 +++++++++++++++----------- bin/domain/imag-timetrack/src/month.rs | 23 ++++++++++++-------- bin/domain/imag-timetrack/src/start.rs | 5 ++++- bin/domain/imag-timetrack/src/stop.rs | 4 +++- bin/domain/imag-timetrack/src/track.rs | 6 ++++-- bin/domain/imag-timetrack/src/week.rs | 15 +++++++++++-- bin/domain/imag-timetrack/src/year.rs | 15 +++++++++++-- 7 files changed, 68 insertions(+), 29 deletions(-) diff --git a/bin/domain/imag-timetrack/src/day.rs b/bin/domain/imag-timetrack/src/day.rs index d6c86b23..09f4882f 100644 --- a/bin/domain/imag-timetrack/src/day.rs +++ b/bin/domain/imag-timetrack/src/day.rs @@ -26,6 +26,7 @@ use libimagerror::trace::trace_error; use libimagerror::trace::MapErrTrace; use libimagerror::iter::TraceIterator; use libimagstore::store::FileLockEntry; +use libimagtimetrack::error::TimeTrackError as TTE; use libimagtimetrack::timetrackingstore::TimeTrackStore; use libimagtimetrack::timetracking::TimeTracking; use libimagtimetrack::tag::TimeTrackingTag; @@ -39,21 +40,25 @@ pub fn day(rt: &Runtime) -> i32 { let cmd = cmd.unwrap(); // checked in main() let filter = { - let start = match cmd.value_of("start").map(::chrono::naive::NaiveDateTime::from_str) { - None => ::chrono::offset::Local::today().and_hms(0, 0, 0).naive_local(), - Some(Ok(dt)) => dt, - Some(Err(e)) => { - trace_error(&e); - return 1 + let start = match cmd.value_of("start").map(NaiveDateTime::from_str) { + None => ::chrono::offset::Local::today().and_hms(0, 0, 0).naive_local(), + Some(s) => match s.map_err(TTE::from) { + Ok(dt) => dt, + Err(e) => { + trace_error(&e); + return 1 + } } }; - let end = match cmd.value_of("end").map(::chrono::naive::NaiveDateTime::from_str) { - None => ::chrono::offset::Local::today().and_hms(23, 59, 59).naive_local(), - Some(Ok(dt)) => dt, - Some(Err(e)) => { - trace_error(&e); - return 1 + let end = match cmd.value_of("end").map(NaiveDateTime::from_str) { + None => ::chrono::offset::Local::today().and_hms(23, 59, 59).naive_local(), + Some(s) => match s.map_err(TTE::from) { + Ok(dt) => dt, + Err(e) => { + trace_error(&e); + return 1 + } } }; diff --git a/bin/domain/imag-timetrack/src/month.rs b/bin/domain/imag-timetrack/src/month.rs index 1015a93b..ac715500 100644 --- a/bin/domain/imag-timetrack/src/month.rs +++ b/bin/domain/imag-timetrack/src/month.rs @@ -26,6 +26,7 @@ use libimagerror::trace::trace_error; use libimagerror::trace::MapErrTrace; use libimagerror::iter::TraceIterator; use libimagstore::store::FileLockEntry; +use libimagtimetrack::error::TimeTrackError as TTE; use libimagtimetrack::timetrackingstore::TimeTrackStore; use libimagtimetrack::timetracking::TimeTracking; use libimagtimetrack::tag::TimeTrackingTag; @@ -44,11 +45,13 @@ pub fn month(rt: &Runtime) -> i32 { let now = Local::now(); let start = match cmd.value_of("start").map(::chrono::naive::NaiveDateTime::from_str) { - None => NaiveDate::from_ymd(now.year(), now.month(), 1).and_hms(0, 0, 0), - Some(Ok(dt)) => dt, - Some(Err(e)) => { - trace_error(&e); - return 1 + None => NaiveDate::from_ymd(now.year(), now.month(), 1).and_hms(0, 0, 0), + Some(s) => match s.map_err(TTE::from) { + Ok(dt) => dt, + Err(e) => { + trace_error(&e); + return 1 + } } }; @@ -65,10 +68,12 @@ pub fn month(rt: &Runtime) -> i32 { NaiveDate::from_ymd(year, month, 1).and_hms(0, 0, 0) }, - Some(Ok(dt)) => dt, - Some(Err(e)) => { - trace_error(&e); - return 1 + Some(s) => match s.map_err(TTE::from) { + Ok(dt) => dt, + Err(e) => { + trace_error(&e); + return 1 + } } }; diff --git a/bin/domain/imag-timetrack/src/start.rs b/bin/domain/imag-timetrack/src/start.rs index e5efa3a1..6827bb30 100644 --- a/bin/domain/imag-timetrack/src/start.rs +++ b/bin/domain/imag-timetrack/src/start.rs @@ -19,8 +19,11 @@ use std::str::FromStr; +use chrono::naive::NaiveDateTime; + use libimagrt::runtime::Runtime; use libimagerror::trace::trace_error; +use libimagtimetrack::error::TimeTrackError as TTE; use libimagtimetrack::tag::TimeTrackingTag; use libimagtimetrack::timetrackingstore::TimeTrackStore; use libimagerror::trace::MapErrTrace; @@ -31,7 +34,7 @@ pub fn start(rt: &Runtime) -> i32 { let start = match cmd.value_of("start-time") { None | Some("now") => ::chrono::offset::Local::now().naive_local(), - Some(ndt) => match ::chrono::naive::NaiveDateTime::from_str(ndt) { + Some(ndt) => match NaiveDateTime::from_str(ndt).map_err(TTE::from) { Ok(ndt) => ndt, Err(e) => { trace_error(&e); diff --git a/bin/domain/imag-timetrack/src/stop.rs b/bin/domain/imag-timetrack/src/stop.rs index 038b82d4..cc91fda9 100644 --- a/bin/domain/imag-timetrack/src/stop.rs +++ b/bin/domain/imag-timetrack/src/stop.rs @@ -20,12 +20,14 @@ use std::str::FromStr; use filters::filter::Filter; +use chrono::NaiveDateTime; use libimagerror::trace::trace_error; use libimagerror::iter::TraceIterator; use libimagerror::trace::MapErrTrace; use libimagrt::runtime::Runtime; +use libimagtimetrack::error::TimeTrackError as TTE; use libimagtimetrack::timetracking::TimeTracking; use libimagtimetrack::tag::TimeTrackingTag; use libimagtimetrack::timetrackingstore::*; @@ -40,7 +42,7 @@ pub fn stop(rt: &Runtime) -> i32 { let stop_time = match cmd.value_of("stop-time") { None | Some("now") => ::chrono::offset::Local::now().naive_local(), - Some(ndt) => match ::chrono::naive::NaiveDateTime::from_str(ndt) { + Some(ndt) => match NaiveDateTime::from_str(ndt).map_err(TTE::from) { Ok(ndt) => ndt, Err(e) => { trace_error(&e); diff --git a/bin/domain/imag-timetrack/src/track.rs b/bin/domain/imag-timetrack/src/track.rs index b758bb9e..ffc9020f 100644 --- a/bin/domain/imag-timetrack/src/track.rs +++ b/bin/domain/imag-timetrack/src/track.rs @@ -20,10 +20,12 @@ use std::process::exit; use clap::ArgMatches; +use chrono::naive::NaiveDate; use chrono::naive::NaiveDateTime; use libimagrt::runtime::Runtime; use libimagerror::trace::trace_error; +use libimagtimetrack::error::TimeTrackError as TTE; use libimagtimetrack::tag::TimeTrackingTag; use libimagtimetrack::timetrackingstore::TimeTrackStore; use libimagerror::trace::MapErrTrace; @@ -41,10 +43,10 @@ pub fn track(rt: &Runtime) -> i32 { match cmd.value_of(clap_name) { Some("now") => Some(::chrono::offset::Local::now().naive_local()), Some(els) => { - match ::chrono::naive::NaiveDateTime::parse_from_str(els, DATE_TIME_PARSE_FMT) { + match NaiveDateTime::parse_from_str(els, DATE_TIME_PARSE_FMT).map_err(TTE::from) { Ok(ndt) => Some(ndt), Err(e_ndt) => { - match ::chrono::naive::NaiveDate::parse_from_str(els, DATE_PARSE_FMT) { + match NaiveDate::parse_from_str(els, DATE_PARSE_FMT).map_err(TTE::from) { Ok(ndt) => Some(ndt.and_hms(0, 0, 0)), Err(e_nd) => { error!("Cannot parse date {}:", errname); diff --git a/bin/domain/imag-timetrack/src/week.rs b/bin/domain/imag-timetrack/src/week.rs index 32d83831..ba6953b8 100644 --- a/bin/domain/imag-timetrack/src/week.rs +++ b/bin/domain/imag-timetrack/src/week.rs @@ -26,6 +26,7 @@ use libimagerror::trace::trace_error; use libimagerror::trace::MapErrTrace; use libimagerror::iter::TraceIterator; use libimagstore::store::FileLockEntry; +use libimagtimetrack::error::TimeTrackError as TTE; use libimagtimetrack::timetrackingstore::TimeTrackStore; use libimagtimetrack::timetracking::TimeTracking; use libimagtimetrack::tag::TimeTrackingTag; @@ -44,7 +45,12 @@ pub fn week(rt: &Runtime) -> i32 { let this_week = Local::now().iso_week(); - let start = match cmd.value_of("start").map(::chrono::naive::NaiveDateTime::from_str) { + let start = match cmd + .value_of("start") + .map(|s| { + ::chrono::naive::NaiveDateTime::from_str(s).map_err(TTE::from) + }) + { None => NaiveDate::from_isoywd(this_week.year(), this_week.week(), Weekday::Mon) .and_hms(0, 0, 0), Some(Ok(dt)) => dt, @@ -54,7 +60,12 @@ pub fn week(rt: &Runtime) -> i32 { } }; - let end = match cmd.value_of("end").map(::chrono::naive::NaiveDateTime::from_str) { + let end = match cmd + .value_of("end") + .map(|s| { + ::chrono::naive::NaiveDateTime::from_str(s).map_err(TTE::from) + }) + { None => NaiveDate::from_isoywd(this_week.year(), this_week.week(), Weekday::Sun) .and_hms(23, 59, 59), Some(Ok(dt)) => dt, diff --git a/bin/domain/imag-timetrack/src/year.rs b/bin/domain/imag-timetrack/src/year.rs index 1af93dad..9e7d1aed 100644 --- a/bin/domain/imag-timetrack/src/year.rs +++ b/bin/domain/imag-timetrack/src/year.rs @@ -26,6 +26,7 @@ use libimagerror::trace::trace_error; use libimagerror::trace::MapErrTrace; use libimagerror::iter::TraceIterator; use libimagstore::store::FileLockEntry; +use libimagtimetrack::error::TimeTrackError as TTE; use libimagtimetrack::timetrackingstore::TimeTrackStore; use libimagtimetrack::timetracking::TimeTracking; use libimagtimetrack::tag::TimeTrackingTag; @@ -43,7 +44,12 @@ pub fn year(rt: &Runtime) -> i32 { let now = Local::now(); - let start = match cmd.value_of("start").map(::chrono::naive::NaiveDateTime::from_str) { + let start = match cmd + .value_of("start") + .map(|s| { + ::chrono::naive::NaiveDateTime::from_str(s).map_err(TTE::from) + }) + { None => NaiveDate::from_ymd(now.year(), 1, 1).and_hms(0, 0, 0), Some(Ok(dt)) => dt, Some(Err(e)) => { @@ -52,7 +58,12 @@ pub fn year(rt: &Runtime) -> i32 { } }; - let end = match cmd.value_of("end").map(::chrono::naive::NaiveDateTime::from_str) { + let end = match cmd + .value_of("end") + .map(|s| { + ::chrono::naive::NaiveDateTime::from_str(s).map_err(TTE::from) + }) + { None => { NaiveDate::from_ymd(now.year() + 1, 1, 1).and_hms(0, 0, 0) },