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 5C42982878 for ; Tue, 30 Nov 2021 13:13:13 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id B358E26A41 for ; Tue, 30 Nov 2021 13:12:18 +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 0685826912 for ; Tue, 30 Nov 2021 13:12:12 +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 D3E2144689 for ; Tue, 30 Nov 2021 13:12:11 +0100 (CET) From: Dominik Csapak To: pbs-devel@lists.proxmox.com Date: Tue, 30 Nov 2021 13:11:59 +0100 Message-Id: <20211130121209.216846-6-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.183 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 05/14] proxmox-time: split DateTimeValue 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:13:13 -0000 Signed-off-by: Dominik Csapak --- proxmox-time/src/date_time_value.rs | 82 ++++++++++++++++++++++++++++ proxmox-time/src/lib.rs | 2 + proxmox-time/src/parse_time.rs | 1 + proxmox-time/src/time.rs | 84 +---------------------------- 4 files changed, 86 insertions(+), 83 deletions(-) create mode 100644 proxmox-time/src/date_time_value.rs diff --git a/proxmox-time/src/date_time_value.rs b/proxmox-time/src/date_time_value.rs new file mode 100644 index 0000000..c3c2035 --- /dev/null +++ b/proxmox-time/src/date_time_value.rs @@ -0,0 +1,82 @@ +#[derive(Debug, Clone)] +pub(crate) enum DateTimeValue { + Single(u32), + Range(u32, u32), + Repeated(u32, u32, Option), +} + +impl DateTimeValue { + // Test if the entry contains the value + pub fn contains(&self, value: u32) -> bool { + match self { + DateTimeValue::Single(v) => *v == value, + DateTimeValue::Range(start, end) => value >= *start && value <= *end, + DateTimeValue::Repeated(start, repetition, opt_end) => { + if value >= *start { + if *repetition > 0 { + let offset = value - start; + let res = offset % repetition == 0; + if let Some(end) = opt_end { + res && value <= *end + } else { + res + } + } else { + *start == value + } + } else { + false + } + } + } + } + + pub fn list_contains(list: &[DateTimeValue], value: u32) -> bool { + list.iter().any(|spec| spec.contains(value)) + } + + // Find an return an entry greater than value + pub fn find_next(list: &[DateTimeValue], value: u32) -> Option { + let mut next: Option = None; + let mut set_next = |v: u32| { + if let Some(n) = next { + if v < n { next = Some(v); } + } else { + next = Some(v); + } + }; + for spec in list { + match spec { + DateTimeValue::Single(v) => { + if *v > value { set_next(*v); } + } + DateTimeValue::Range(start, end) => { + if value < *start { + set_next(*start); + } else { + let n = value + 1; + if n >= *start && n <= *end { + set_next(n); + } + } + } + DateTimeValue::Repeated(start, repetition, opt_end) => { + if value < *start { + set_next(*start); + } else if *repetition > 0 { + let n = start + ((value - start + repetition) / repetition) * repetition; + if let Some(end) = opt_end { + if n <= *end { + set_next(n); + } + } else { + set_next(n); + } + } + } + } + } + + next + } +} diff --git a/proxmox-time/src/lib.rs b/proxmox-time/src/lib.rs index bce4b56..3d888a2 100644 --- a/proxmox-time/src/lib.rs +++ b/proxmox-time/src/lib.rs @@ -15,6 +15,8 @@ pub use time::*; pub(crate) mod parse_helpers; +pub(crate) mod date_time_value; + mod week_days; pub use week_days::*; diff --git a/proxmox-time/src/parse_time.rs b/proxmox-time/src/parse_time.rs index 6dde4ae..00a9241 100644 --- a/proxmox-time/src/parse_time.rs +++ b/proxmox-time/src/parse_time.rs @@ -17,6 +17,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}; +use crate::date_time_value::DateTimeValue; lazy_static! { static ref TIME_SPAN_UNITS: HashMap<&'static str, f64> = { diff --git a/proxmox-time/src/time.rs b/proxmox-time/src/time.rs index 2b2a936..7b9625f 100644 --- a/proxmox-time/src/time.rs +++ b/proxmox-time/src/time.rs @@ -2,94 +2,12 @@ use std::convert::TryInto; use anyhow::Error; +use crate::date_time_value::DateTimeValue; use crate::TmEditor; use crate::WeekDays; use crate::{parse_calendar_event, parse_time_span}; -#[derive(Debug, Clone)] -pub(crate) enum DateTimeValue { - Single(u32), - Range(u32, u32), - Repeated(u32, u32, Option), -} - -impl DateTimeValue { - // Test if the entry contains the value - pub fn contains(&self, value: u32) -> bool { - match self { - DateTimeValue::Single(v) => *v == value, - DateTimeValue::Range(start, end) => value >= *start && value <= *end, - DateTimeValue::Repeated(start, repetition, opt_end) => { - if value >= *start { - if *repetition > 0 { - let offset = value - start; - let res = offset % repetition == 0; - if let Some(end) = opt_end { - res && value <= *end - } else { - res - } - } else { - *start == value - } - } else { - false - } - } - } - } - - pub fn list_contains(list: &[DateTimeValue], value: u32) -> bool { - list.iter().any(|spec| spec.contains(value)) - } - - // Find an return an entry greater than value - pub fn find_next(list: &[DateTimeValue], value: u32) -> Option { - let mut next: Option = None; - let mut set_next = |v: u32| { - if let Some(n) = next { - if v < n { next = Some(v); } - } else { - next = Some(v); - } - }; - for spec in list { - match spec { - DateTimeValue::Single(v) => { - if *v > value { set_next(*v); } - } - DateTimeValue::Range(start, end) => { - if value < *start { - set_next(*start); - } else { - let n = value + 1; - if n >= *start && n <= *end { - set_next(n); - } - } - } - DateTimeValue::Repeated(start, repetition, opt_end) => { - if value < *start { - set_next(*start); - } else if *repetition > 0 { - let n = start + ((value - start + repetition) / repetition) * repetition; - if let Some(end) = opt_end { - if n <= *end { - set_next(n); - } - } else { - set_next(n); - } - } - } - } - } - - next - } -} - /// Calendar events may be used to refer to one or more points in time in a /// single expression. They are designed after the systemd.time Calendar Events /// specification, but are not guaranteed to be 100% compatible. -- 2.30.2