public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Dominik Csapak <d.csapak@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [pbs-devel] [PATCH proxmox 05/14] proxmox-time: split DateTimeValue into own file
Date: Tue, 30 Nov 2021 13:11:59 +0100	[thread overview]
Message-ID: <20211130121209.216846-6-d.csapak@proxmox.com> (raw)
In-Reply-To: <20211130121209.216846-1-d.csapak@proxmox.com>

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
---
 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<u32>),
+}
+
+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<u32> {
+        let mut next: Option<u32> = 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<u32>),
-}
-
-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<u32> {
-        let mut next: Option<u32> = 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





  parent reply	other threads:[~2021-11-30 12:13 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-30 12:11 [pbs-devel] [PATCH proxmox/proxmox-backup 00/14] improve & cleanup proxmox-time Dominik Csapak
2021-11-30 12:11 ` [pbs-devel] [PATCH proxmox 01/14] proxmox-time: calendar-events: implement repeated ranges Dominik Csapak
2021-11-30 12:11 ` [pbs-devel] [PATCH proxmox 02/14] proxmox-time: calendar-events: make hour optional Dominik Csapak
2021-11-30 12:11 ` [pbs-devel] [PATCH proxmox 03/14] proxmox-time: move common parse functions to parse_helpers Dominik Csapak
2021-11-30 12:11 ` [pbs-devel] [PATCH proxmox 04/14] proxmox-time: move WeekDays into own file Dominik Csapak
2021-11-30 12:11 ` Dominik Csapak [this message]
2021-11-30 12:12 ` [pbs-devel] [PATCH proxmox 06/14] proxmox-time: move parse_daily_duration to daily_duration.rs Dominik Csapak
2021-11-30 12:12 ` [pbs-devel] [PATCH proxmox 07/14] proxmox-time: daily_duration.rs: rustfmt Dominik Csapak
2021-11-30 12:12 ` [pbs-devel] [PATCH proxmox 08/14] proxmox-time: move CalendarEvent into calendar_events.rs Dominik Csapak
2021-11-30 12:12 ` [pbs-devel] [PATCH proxmox 09/14] proxmox-time: move TimeSpan into time_span.rs Dominik Csapak
2021-11-30 12:12 ` [pbs-devel] [PATCH proxmox 10/14] proxmox-time: move tests from time.rs to test.rs Dominik Csapak
2021-11-30 12:12 ` [pbs-devel] [PATCH proxmox 11/14] proxmox-time: lib.rs: rustfmt Dominik Csapak
2021-11-30 12:12 ` [pbs-devel] [PATCH proxmox 12/14] proxmox-time: calendar-events: make compute_next_event a method Dominik Csapak
2021-11-30 12:12 ` [pbs-devel] [PATCH proxmox 13/14] proxmox-time: calendar_events: implement FromStr Dominik Csapak
2021-11-30 12:12 ` [pbs-devel] [PATCH proxmox 14/14] proxmox-time: time-span: " Dominik Csapak
2021-11-30 12:12 ` [pbs-devel] [PATCH proxmox-backup 1/1] remove use of deprecated functions from proxmox-time Dominik Csapak
2021-12-01  6:26   ` [pbs-devel] applied: " Dietmar Maurer
2021-12-01  6:20 ` [pbs-devel] applied: [PATCH proxmox/proxmox-backup 00/14] improve & cleanup proxmox-time Dietmar Maurer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20211130121209.216846-6-d.csapak@proxmox.com \
    --to=d.csapak@proxmox.com \
    --cc=pbs-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal