From 9801a3c29550519caa6633eaefff6707ae34ea46 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Thu, 3 Oct 2019 15:52:16 +0200 Subject: [PATCH] Add helper function to parse string with kairos Signed-off-by: Matthias Beyer --- bin/domain/imag-calendar/Cargo.toml | 1 + bin/domain/imag-calendar/src/main.rs | 1 + bin/domain/imag-calendar/src/util.rs | 25 +++++++++++++++++++++++++ 3 files changed, 27 insertions(+) diff --git a/bin/domain/imag-calendar/Cargo.toml b/bin/domain/imag-calendar/Cargo.toml index 187468f3..192a7402 100644 --- a/bin/domain/imag-calendar/Cargo.toml +++ b/bin/domain/imag-calendar/Cargo.toml @@ -27,6 +27,7 @@ walkdir = "2.2.8" vobject = "0.7" handlebars = "2" chrono = "0.4" +kairos = "0.3" libimagrt = { version = "0.10.0", path = "../../../lib/core/libimagrt" } libimagstore = { version = "0.10.0", path = "../../../lib/core/libimagstore" } diff --git a/bin/domain/imag-calendar/src/main.rs b/bin/domain/imag-calendar/src/main.rs index 4039cde3..82d091cf 100644 --- a/bin/domain/imag-calendar/src/main.rs +++ b/bin/domain/imag-calendar/src/main.rs @@ -41,6 +41,7 @@ extern crate toml_query; extern crate walkdir; extern crate handlebars; extern crate chrono; +extern crate kairos; #[macro_use] extern crate libimagrt; extern crate libimagcalendar; diff --git a/bin/domain/imag-calendar/src/util.rs b/bin/domain/imag-calendar/src/util.rs index ab9f8eba..3908b552 100644 --- a/bin/domain/imag-calendar/src/util.rs +++ b/bin/domain/imag-calendar/src/util.rs @@ -27,6 +27,7 @@ use failure::Fallible as Result; use failure::Error; use failure::err_msg; use toml_query::read::TomlValueReadTypeExt; +use chrono::NaiveDateTime; use libimagrt::runtime::Runtime; use libimagstore::store::FileLockEntry; @@ -34,6 +35,7 @@ use libimagentryref::reference::fassade::RefFassade; use libimagentryref::reference::Ref; use libimagentryref::reference::Config; use libimagentryref::hasher::default::DefaultHasher; +use libimagerror::trace::MapErrTrace; pub struct ParsedEventFLE<'a> { inner: FileLockEntry<'a>, @@ -117,3 +119,26 @@ pub fn build_data_object_for_handlebars<'a>(i: usize, event: &Event<'a>) data } + +pub fn kairos_parse(spec: &str) -> Result { + match ::kairos::parser::parse(spec).map_err_trace_exit_unwrap() { + ::kairos::parser::Parsed::Iterator(_) => { + trace!("before-filter spec resulted in iterator"); + Err(format_err!("Not a moment in time: {}", spec)) + } + + ::kairos::parser::Parsed::TimeType(tt) => { + trace!("before-filter spec resulted in timetype"); + let tt = tt.calculate() + .map_err_trace_exit_unwrap() + .get_moment().unwrap_or_else(|| { + error!("Not a moment in time: {}", spec); + ::std::process::exit(1); + }) + .clone(); + + trace!("Before filter spec {} => {}", spec, tt); + Ok(tt) + } + } +}