From 81c6ee83140e866bf4b72ecc1769ae8f2d5773f2 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Thu, 3 Oct 2019 15:51:45 +0200 Subject: [PATCH] Rewrite helper to be less complicated Signed-off-by: Matthias Beyer --- bin/domain/imag-calendar/src/filters.rs | 74 +++++++++++-------------- bin/domain/imag-calendar/src/main.rs | 14 ++++- 2 files changed, 44 insertions(+), 44 deletions(-) diff --git a/bin/domain/imag-calendar/src/filters.rs b/bin/domain/imag-calendar/src/filters.rs index ad382f37..063936b9 100644 --- a/bin/domain/imag-calendar/src/filters.rs +++ b/bin/domain/imag-calendar/src/filters.rs @@ -20,52 +20,44 @@ use chrono::NaiveDateTime; use failure::Fallible as Result; use vobject::icalendar::Event; +use libimagerror::trace::MapErrTrace; -/// Generate a filter function to filter shown events -/// -/// If `do_filter` is false, this filter returns true all the time. -/// -/// If an event is in the past, relative to the `today` parameter, the function returns false, -/// else it returns true. -/// -/// # Details -/// -/// The date of the event is determined by using the "dtend" or "dtstamp" members of the event -/// object. These fields are parsed to NaiveDateTime objects and then compared to the `today` object. -/// -/// If an parsing error happens in the "dtend" parsing step, "dtstamp" is used. If this results also -/// in a parsing error, the first error is returned. -/// -pub fn filter_past(do_filter: bool, today: NaiveDateTime) -> impl FnOnce(&Event) -> Result { - move |pe| if do_filter { - let uid = || pe.uid() - .map(|uid| uid.into_raw()) - .unwrap_or_else(|| String::from("")); +pub fn event_is_before<'a>(event: &Event<'a>, before_spec: &NaiveDateTime) -> bool { + let uid = || event.uid() + .map(|uid| uid.into_raw()) + .unwrap_or_else(|| String::from("")); - let dtend_is_pre_today : Result = pe.dtend() - .map(|dtend| Ok(try_to_parse_datetime(dtend.raw())? < today)) - .unwrap_or_else(|| Err({ - format_err!("Entry with UID {} has no end time, cannot determine whether to list it", - uid()) - })); + let dtend_is_before_spec : Result = event.dtend() + .map(|dtend| { + let datetime = try_to_parse_datetime(dtend.raw())?; + let result = datetime < *before_spec; + trace!("{} < {} => {}", datetime, before_spec, result); + Ok(result) + }) + .unwrap_or_else(|| Err({ + format_err!("Entry with UID {} has no end time, cannot determine whether to list it", + uid()) + })); - let dtstamp_is_pre_today : Result = pe.dtstamp() - .map(|dtstamp| Ok(try_to_parse_datetime(dtstamp.raw())? < today)) - .unwrap_or_else(|| Err({ - format_err!("Entry with UID {} has no timestamp, cannot determine whether to list it", - uid()) - })); + let dtstamp_is_before_spec : Result = event.dtstamp() + .map(|dtstamp| { + let datetime = try_to_parse_datetime(dtstamp.raw())?; + let result = datetime < *before_spec; + trace!("{} < {} => {}", datetime, before_spec, result); + Ok(result) + }) + .unwrap_or_else(|| Err({ + format_err!("Entry with UID {} has no timestamp, cannot determine whether to list it", + uid()) + })); - trace!("dtend_is_pre_today = {:?}", dtend_is_pre_today); - trace!("dtstamp_is_pre_today = {:?}", dtstamp_is_pre_today); + trace!("dtend_is_before_spec = {:?}", dtend_is_before_spec); + trace!("dtstamp_is_before_spec = {:?}", dtstamp_is_before_spec); - match (dtend_is_pre_today, dtstamp_is_pre_today) { - (Ok(b), _) => return Ok(!b), - (_, Ok(b)) => return Ok(!b), - (Err(e), _) => return Err(e) - } - } else { - Ok(true) + match (dtend_is_before_spec, dtstamp_is_before_spec) { + (Ok(b), _) => return b, + (_, Ok(b)) => return b, + (Err(e), _) => return Err(e).map_err_trace_exit_unwrap() } } diff --git a/bin/domain/imag-calendar/src/main.rs b/bin/domain/imag-calendar/src/main.rs index 379374df..4039cde3 100644 --- a/bin/domain/imag-calendar/src/main.rs +++ b/bin/domain/imag-calendar/src/main.rs @@ -171,7 +171,7 @@ fn list(rt: &Runtime) { let list_format = get_event_print_format("calendar.list_format", rt, &scmd) .map_err_trace_exit_unwrap(); - let do_filter_past = !scmd.is_present("list-past"); + let do_filter_past = !scmd.is_present("list-past"); let ref_config = rt.config() .ok_or_else(|| format_err!("No configuration, cannot continue!")) @@ -185,11 +185,19 @@ fn list(rt: &Runtime) { debug!("List format: {:?}", list_format); debug!("Ref config : {:?}", ref_config); + let today = ::chrono::Local::now().naive_local(); let event_filter = |e: &'_ Event| { // what a crazy hack to make the compiler happy debug!("Filtering event: {:?}", e); - let f = filters::filter_past(do_filter_past, chrono::Local::now().naive_local()); - f(e) + + // generate a function `filter_past` which filters out the past or not + let allow_all_past_events = |event| if do_filter_past { + filters::event_is_before(event, &today) + } else { + true + }; + + allow_all_past_events(e) }; let mut listed_events = 0;