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 C6B767D5A8 for ; Tue, 9 Nov 2021 07:53:10 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id DE6382E15A for ; Tue, 9 Nov 2021 07:53:09 +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 A1DF02DF6F for ; Tue, 9 Nov 2021 07:53:01 +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 C12A442EF4; Tue, 9 Nov 2021 07:52:56 +0100 (CET) From: Dietmar Maurer To: pbs-devel@lists.proxmox.com Date: Tue, 9 Nov 2021 07:52:44 +0100 Message-Id: <20211109065253.980304-8-dietmar@proxmox.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20211109065253.980304-1-dietmar@proxmox.com> References: <20211109065253.980304-1-dietmar@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.505 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. [lib.rs, time.rs] Subject: [pbs-devel] [PATCH proxmox-backup 4/9] New DailyDuration type with nom parser 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, 09 Nov 2021 06:53:10 -0000 We will use this to specify timesframes for network rate limits (only apply limite when inside the time frame). Note: This is not systemd related, but we can reuse some of the parser method. Signed-off-by: Dietmar Maurer --- proxmox-systemd/src/daily_duration.rs | 92 +++++++++++++++++++++++++++ proxmox-systemd/src/lib.rs | 1 + proxmox-systemd/src/parse_time.rs | 56 ++++++++++++++++ proxmox-systemd/src/time.rs | 2 +- 4 files changed, 150 insertions(+), 1 deletion(-) create mode 100644 proxmox-systemd/src/daily_duration.rs diff --git a/proxmox-systemd/src/daily_duration.rs b/proxmox-systemd/src/daily_duration.rs new file mode 100644 index 00000000..bed4eb47 --- /dev/null +++ b/proxmox-systemd/src/daily_duration.rs @@ -0,0 +1,92 @@ +use std::cmp::{Ordering, PartialOrd}; + +use super::time::{WeekDays}; + +pub use super::parse_time::parse_daily_duration; + +/// Time of Day (hour with minute) +#[derive(Default, PartialEq, Clone, Debug)] +pub struct HmTime { + pub hour: u32, + pub minute: u32, +} + +impl PartialOrd for HmTime { + fn partial_cmp(&self, other: &Self) -> Option { + let mut order = self.hour.cmp(&other.hour); + if order == Ordering::Equal { + order = self.minute.cmp(&other.minute); + } + Some(order) + } +} + +#[derive(Default, Clone, Debug)] +pub struct DailyDuration { + /// the days in a week this duration should trigger + pub days: WeekDays, + pub start: HmTime, + pub end: HmTime, +} + +#[cfg(test)] +mod test { + + use anyhow::{bail, Error}; + + use super::*; + + fn test_parse( + duration_str: &str, + start_h: u32, start_m: u32, + end_h: u32, end_m: u32, + days: &[usize], + ) -> Result<(), Error> { + let mut day_bits = 0; + for day in days { day_bits |= 1< Result<(), Error> { + + assert!(parse_daily_duration("").is_err()); + assert!(parse_daily_duration(" 8-12").is_err()); + assert!(parse_daily_duration("8:60-12").is_err()); + assert!(parse_daily_duration("8-25").is_err()); + assert!(parse_daily_duration("12-8").is_err()); + + test_parse("8-12", 8, 0, 12, 0, &[])?; + test_parse("8:0-12:0", 8, 0, 12, 0, &[])?; + test_parse("8:00-12:00", 8, 0, 12, 0, &[])?; + test_parse("8:05-12:20", 8, 5, 12, 20, &[])?; + test_parse("8:05 - 12:20", 8, 5, 12, 20, &[])?; + + test_parse("mon 8-12", 8, 0, 12, 0, &[0])?; + test_parse("tue..fri 8-12", 8, 0, 12, 0, &[1,2,3,4])?; + test_parse("sat,tue..thu,fri 8-12", 8, 0, 12, 0, &[1,2,3,4,5])?; + + Ok(()) + } +} diff --git a/proxmox-systemd/src/lib.rs b/proxmox-systemd/src/lib.rs index b4ab4b72..7c2b1f90 100644 --- a/proxmox-systemd/src/lib.rs +++ b/proxmox-systemd/src/lib.rs @@ -1,4 +1,5 @@ pub mod time; +pub mod daily_duration; mod parse_time; mod unit; diff --git a/proxmox-systemd/src/parse_time.rs b/proxmox-systemd/src/parse_time.rs index ba9449b1..d212e264 100644 --- a/proxmox-systemd/src/parse_time.rs +++ b/proxmox-systemd/src/parse_time.rs @@ -4,6 +4,7 @@ use anyhow::{bail, Error}; use lazy_static::lazy_static; use super::time::*; +use super::daily_duration::*; use nom::{ error::{context, ParseError, VerboseError}, @@ -452,3 +453,58 @@ fn parse_time_span_incomplete(mut i: &str) -> IResult<&str, TimeSpan> { Ok((i, ts)) } + +pub fn parse_daily_duration(i: &str) -> Result { + parse_complete_line("daily duration", i, parse_daily_duration_incomplete) +} + +fn parse_daily_duration_incomplete(mut i: &str) -> IResult<&str, DailyDuration> { + + let mut duration = DailyDuration::default(); + + if i.starts_with(|c: char| char::is_ascii_alphabetic(&c)) { + + let (n, range_list) = context( + "weekday range list", + separated_nonempty_list(tag(","), parse_weekdays_range) + )(i)?; + + i = space0(n)?.0; + + for range in range_list { duration.days.insert(range); } + } + + let (i, start) = parse_hm_time(i)?; + + let i = space0(i)?.0; + + let (i, _) = tag("-")(i)?; + + let i = space0(i)?.0; + + let end_time_start = i; + + let (i, end) = parse_hm_time(i)?; + + if start > end { + return Err(parse_error(end_time_start, "end time before start time")); + } + + duration.start = start; + duration.end = end; + + Ok((i, duration)) +} + +fn parse_hm_time(i: &str) -> IResult<&str, HmTime> { + + let (i, (hour, opt_minute)) = tuple(( + parse_time_comp(24), + opt(preceded(tag(":"), parse_time_comp(60))), + ))(i)?; + + match opt_minute { + Some(minute) => Ok((i, HmTime { hour, minute })), + None => Ok((i, HmTime { hour, minute: 0})), + } +} diff --git a/proxmox-systemd/src/time.rs b/proxmox-systemd/src/time.rs index b81e970e..e5fe7965 100644 --- a/proxmox-systemd/src/time.rs +++ b/proxmox-systemd/src/time.rs @@ -5,7 +5,7 @@ use bitflags::bitflags; use proxmox_time::TmEditor; -pub use super::parse_time::*; +pub use super::parse_time::{parse_calendar_event, parse_time_span}; bitflags!{ #[derive(Default)] -- 2.30.2