From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id 1FEAC82858 for ; Tue, 30 Nov 2021 13:12:44 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 0494B2696D for ; Tue, 30 Nov 2021 13:12:14 +0100 (CET) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [94.136.29.106]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS id 92EB2268F4 for ; Tue, 30 Nov 2021 13:12:10 +0100 (CET) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 679DF45BBA for ; Tue, 30 Nov 2021 13:12:10 +0100 (CET) From: Dominik Csapak To: pbs-devel@lists.proxmox.com Date: Tue, 30 Nov 2021 13:11:58 +0100 Message-Id: <20211130121209.216846-5-d.csapak@proxmox.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20211130121209.216846-1-d.csapak@proxmox.com> References: <20211130121209.216846-1-d.csapak@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.185 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [time.rs, lib.rs] Subject: [pbs-devel] [PATCH proxmox 04/14] proxmox-time: move WeekDays into own file X-BeenThere: pbs-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Backup Server development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 30 Nov 2021 12:12:44 -0000 Signed-off-by: Dominik Csapak --- proxmox-time/src/lib.rs | 3 ++ proxmox-time/src/parse_time.rs | 45 +--------------------- proxmox-time/src/time.rs | 16 +------- proxmox-time/src/week_days.rs | 69 ++++++++++++++++++++++++++++++++++ 4 files changed, 74 insertions(+), 59 deletions(-) create mode 100644 proxmox-time/src/week_days.rs diff --git a/proxmox-time/src/lib.rs b/proxmox-time/src/lib.rs index d2a4cb7..bce4b56 100644 --- a/proxmox-time/src/lib.rs +++ b/proxmox-time/src/lib.rs @@ -15,6 +15,9 @@ pub use time::*; pub(crate) mod parse_helpers; +mod week_days; +pub use week_days::*; + mod daily_duration; pub use daily_duration::*; diff --git a/proxmox-time/src/parse_time.rs b/proxmox-time/src/parse_time.rs index 7b0e168..6dde4ae 100644 --- a/proxmox-time/src/parse_time.rs +++ b/proxmox-time/src/parse_time.rs @@ -16,6 +16,7 @@ use nom::{ }; use crate::parse_helpers::{parse_complete_line, parse_error, parse_hm_time, parse_time_comp, parse_u64, IResult}; +use crate::{parse_weekdays_range, WeekDays}; lazy_static! { static ref TIME_SPAN_UNITS: HashMap<&'static str, f64> = { @@ -98,50 +99,6 @@ struct DateSpec { day: Vec, } -fn parse_weekday(i: &str) -> IResult<&str, WeekDays> { - let (i, text) = alpha1(i)?; - - match text.to_ascii_lowercase().as_str() { - "monday" | "mon" => Ok((i, WeekDays::MONDAY)), - "tuesday" | "tue" => Ok((i, WeekDays::TUESDAY)), - "wednesday" | "wed" => Ok((i, WeekDays::WEDNESDAY)), - "thursday" | "thu" => Ok((i, WeekDays::THURSDAY)), - "friday" | "fri" => Ok((i, WeekDays::FRIDAY)), - "saturday" | "sat" => Ok((i, WeekDays::SATURDAY)), - "sunday" | "sun" => Ok((i, WeekDays::SUNDAY)), - _ => return Err(parse_error(text, "weekday")), - } -} - -fn parse_weekdays_range(i: &str) -> IResult<&str, WeekDays> { - let (i, startday) = parse_weekday(i)?; - - let generate_range = |start, end| { - let mut res = 0; - let mut pos = start; - loop { - res |= pos; - if pos >= end { break; } - pos <<= 1; - } - WeekDays::from_bits(res).unwrap() - }; - - if let (i, Some((_, endday))) = opt(pair(tag(".."),parse_weekday))(i)? { - let start = startday.bits(); - let end = endday.bits(); - if start > end { - let set1 = generate_range(start, WeekDays::SUNDAY.bits()); - let set2 = generate_range(WeekDays::MONDAY.bits(), end); - Ok((i, set1 | set2)) - } else { - Ok((i, generate_range(start, end))) - } - } else { - Ok((i, startday)) - } -} - fn parse_date_time_comp(max: usize) -> impl Fn(&str) -> IResult<&str, DateTimeValue> { move |i: &str| { let (i, value) = parse_time_comp(max)(i)?; diff --git a/proxmox-time/src/time.rs b/proxmox-time/src/time.rs index fb18949..2b2a936 100644 --- a/proxmox-time/src/time.rs +++ b/proxmox-time/src/time.rs @@ -1,26 +1,12 @@ use std::convert::TryInto; use anyhow::Error; -use bitflags::bitflags; use crate::TmEditor; +use crate::WeekDays; use crate::{parse_calendar_event, parse_time_span}; -bitflags!{ - /// Defines one or more days of a week. - #[derive(Default)] - pub struct WeekDays: u8 { - const MONDAY = 1; - const TUESDAY = 2; - const WEDNESDAY = 4; - const THURSDAY = 8; - const FRIDAY = 16; - const SATURDAY = 32; - const SUNDAY = 64; - } -} - #[derive(Debug, Clone)] pub(crate) enum DateTimeValue { Single(u32), diff --git a/proxmox-time/src/week_days.rs b/proxmox-time/src/week_days.rs new file mode 100644 index 0000000..64d4137 --- /dev/null +++ b/proxmox-time/src/week_days.rs @@ -0,0 +1,69 @@ +use bitflags::bitflags; +use nom::{ + bytes::complete::tag, + character::complete::alpha1, + combinator::opt, + sequence::pair, +}; + +use crate::parse_helpers::{parse_error, IResult}; + +bitflags! { + /// Defines one or more days of a week. + #[derive(Default)] + pub struct WeekDays: u8 { + const MONDAY = 1; + const TUESDAY = 2; + const WEDNESDAY = 4; + const THURSDAY = 8; + const FRIDAY = 16; + const SATURDAY = 32; + const SUNDAY = 64; + } +} + +fn parse_weekday(i: &str) -> IResult<&str, WeekDays> { + let (i, text) = alpha1(i)?; + + match text.to_ascii_lowercase().as_str() { + "monday" | "mon" => Ok((i, WeekDays::MONDAY)), + "tuesday" | "tue" => Ok((i, WeekDays::TUESDAY)), + "wednesday" | "wed" => Ok((i, WeekDays::WEDNESDAY)), + "thursday" | "thu" => Ok((i, WeekDays::THURSDAY)), + "friday" | "fri" => Ok((i, WeekDays::FRIDAY)), + "saturday" | "sat" => Ok((i, WeekDays::SATURDAY)), + "sunday" | "sun" => Ok((i, WeekDays::SUNDAY)), + _ => return Err(parse_error(text, "weekday")), + } +} + +pub(crate) fn parse_weekdays_range(i: &str) -> IResult<&str, WeekDays> { + let (i, startday) = parse_weekday(i)?; + + let generate_range = |start, end| { + let mut res = 0; + let mut pos = start; + loop { + res |= pos; + if pos >= end { + break; + } + pos <<= 1; + } + WeekDays::from_bits(res).unwrap() + }; + + if let (i, Some((_, endday))) = opt(pair(tag(".."), parse_weekday))(i)? { + let start = startday.bits(); + let end = endday.bits(); + if start > end { + let set1 = generate_range(start, WeekDays::SUNDAY.bits()); + let set2 = generate_range(WeekDays::MONDAY.bits(), end); + Ok((i, set1 | set2)) + } else { + Ok((i, generate_range(start, end))) + } + } else { + Ok((i, startday)) + } +} -- 2.30.2