Rewrite helper to be less complicated
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
This commit is contained in:
parent
3da1d9e1d9
commit
81c6ee8314
2 changed files with 44 additions and 44 deletions
|
@ -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<bool> {
|
||||
move |pe| if do_filter {
|
||||
let uid = || pe.uid()
|
||||
.map(|uid| uid.into_raw())
|
||||
.unwrap_or_else(|| String::from("<No UID>"));
|
||||
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("<No UID>"));
|
||||
|
||||
let dtend_is_pre_today : Result<bool> = 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<bool> = 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<bool> = 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<bool> = 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()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in a new issue