[pbs-devel] [PATCH proxmox 04/14] proxmox-time: move WeekDays into own file

Dominik Csapak d.csapak at proxmox.com
Tue Nov 30 13:11:58 CET 2021


Signed-off-by: Dominik Csapak <d.csapak at proxmox.com>
---
 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<DateTimeValue>,
 }
 
-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





More information about the pbs-devel mailing list