From 9cc1415ef503c326381b534badeac15480d20d7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20Gr=C3=BCnbichler?= Date: Mon, 25 Jan 2021 14:42:51 +0100 Subject: [PATCH] systemd/time: extract Time/DateSpec structs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit could be pulled up into CalendarEvent if desired.. Signed-off-by: Fabian Grünbichler --- src/tools/systemd/parse_time.rs | 41 ++++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/src/tools/systemd/parse_time.rs b/src/tools/systemd/parse_time.rs index 88f5c92d..c0f58b04 100644 --- a/src/tools/systemd/parse_time.rs +++ b/src/tools/systemd/parse_time.rs @@ -86,6 +86,19 @@ lazy_static! { map }; } + +struct TimeSpec { + hour: Vec, + minute: Vec, + second: Vec, +} + +struct DateSpec { + year: Vec, + month: Vec, + day: Vec, +} + fn parse_time_comp(max: usize) -> impl Fn(&str) -> IResult<&str, u32> { move |i: &str| { let (i, v) = map_res(recognize(digit1), str::parse)(i)?; @@ -176,7 +189,7 @@ fn parse_date_time_comp_list(start: u32, max: usize) -> impl Fn(&str) -> IResult } } -fn parse_time_spec(i: &str) -> IResult<&str, (Vec, Vec, Vec)> { +fn parse_time_spec(i: &str) -> IResult<&str, TimeSpec> { let (i, (hour, minute, opt_second)) = tuple(( parse_date_time_comp_list(0, 24), @@ -185,13 +198,13 @@ fn parse_time_spec(i: &str) -> IResult<&str, (Vec, Vec IResult<&str, (Vec, Vec, Vec)> { +fn parse_date_spec(i: &str) -> IResult<&str, DateSpec> { // TODO: implement ~ for days (man systemd.time) if let Ok((i, (year, month, day))) = tuple(( @@ -199,12 +212,12 @@ fn parse_date_spec(i: &str) -> IResult<&str, (Vec, Vec IResult<&str, CalendarEvent> for range in range_list { event.days.insert(range); } } - if let (n, Some((year, month, day))) = opt(parse_date_spec)(i)? { - event.year = year; - event.month = month; - event.day = day; + if let (n, Some(date)) = opt(parse_date_spec)(i)? { + event.year = date.year; + event.month = date.month; + event.day = date.day; has_datespec = true; i = space0(n)?.0; } - if let (n, Some((hour, minute, second))) = opt(parse_time_spec)(i)? { - event.hour = hour; - event.minute = minute; - event.second = second; + if let (n, Some(time)) = opt(parse_time_spec)(i)? { + event.hour = time.hour; + event.minute = time.minute; + event.second = time.second; has_timespec = true; i = n; } else {