Rewrite helper to be less complicated

Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
This commit is contained in:
Matthias Beyer 2019-10-03 15:51:45 +02:00
parent 3da1d9e1d9
commit 81c6ee8314
2 changed files with 44 additions and 44 deletions

View file

@ -20,52 +20,44 @@
use chrono::NaiveDateTime; use chrono::NaiveDateTime;
use failure::Fallible as Result; use failure::Fallible as Result;
use vobject::icalendar::Event; use vobject::icalendar::Event;
use libimagerror::trace::MapErrTrace;
/// Generate a filter function to filter shown events pub fn event_is_before<'a>(event: &Event<'a>, before_spec: &NaiveDateTime) -> bool {
/// let uid = || event.uid()
/// If `do_filter` is false, this filter returns true all the time. .map(|uid| uid.into_raw())
/// .unwrap_or_else(|| String::from("<No UID>"));
/// 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<bool> {
move |pe| if do_filter {
let uid = || pe.uid()
.map(|uid| uid.into_raw())
.unwrap_or_else(|| String::from("<No UID>"));
let dtend_is_pre_today : Result<bool> = pe.dtend() let dtend_is_before_spec : Result<bool> = event.dtend()
.map(|dtend| Ok(try_to_parse_datetime(dtend.raw())? < today)) .map(|dtend| {
.unwrap_or_else(|| Err({ let datetime = try_to_parse_datetime(dtend.raw())?;
format_err!("Entry with UID {} has no end time, cannot determine whether to list it", let result = datetime < *before_spec;
uid()) 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<bool> = pe.dtstamp() let dtstamp_is_before_spec : Result<bool> = event.dtstamp()
.map(|dtstamp| Ok(try_to_parse_datetime(dtstamp.raw())? < today)) .map(|dtstamp| {
.unwrap_or_else(|| Err({ let datetime = try_to_parse_datetime(dtstamp.raw())?;
format_err!("Entry with UID {} has no timestamp, cannot determine whether to list it", let result = datetime < *before_spec;
uid()) 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!("dtend_is_before_spec = {:?}", dtend_is_before_spec);
trace!("dtstamp_is_pre_today = {:?}", dtstamp_is_pre_today); trace!("dtstamp_is_before_spec = {:?}", dtstamp_is_before_spec);
match (dtend_is_pre_today, dtstamp_is_pre_today) { match (dtend_is_before_spec, dtstamp_is_before_spec) {
(Ok(b), _) => return Ok(!b), (Ok(b), _) => return b,
(_, Ok(b)) => return Ok(!b), (_, Ok(b)) => return b,
(Err(e), _) => return Err(e) (Err(e), _) => return Err(e).map_err_trace_exit_unwrap()
}
} else {
Ok(true)
} }
} }

View file

@ -171,7 +171,7 @@ fn list(rt: &Runtime) {
let list_format = get_event_print_format("calendar.list_format", rt, &scmd) let list_format = get_event_print_format("calendar.list_format", rt, &scmd)
.map_err_trace_exit_unwrap(); .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() let ref_config = rt.config()
.ok_or_else(|| format_err!("No configuration, cannot continue!")) .ok_or_else(|| format_err!("No configuration, cannot continue!"))
@ -185,11 +185,19 @@ fn list(rt: &Runtime) {
debug!("List format: {:?}", list_format); debug!("List format: {:?}", list_format);
debug!("Ref config : {:?}", ref_config); 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 let event_filter = |e: &'_ Event| { // what a crazy hack to make the compiler happy
debug!("Filtering event: {:?}", e); 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; let mut listed_events = 0;