[pbs-devel] [PATCH proxmox 14/14] proxmox-time: time-span: implement FromStr

Dominik Csapak d.csapak at proxmox.com
Tue Nov 30 13:12:08 CET 2021


and deprecate 'parse_time_span'

Signed-off-by: Dominik Csapak <d.csapak at proxmox.com>
---
 proxmox-time/src/test.rs      |  2 +-
 proxmox-time/src/time_span.rs | 13 +++++++++++--
 2 files changed, 12 insertions(+), 3 deletions(-)

diff --git a/proxmox-time/src/test.rs b/proxmox-time/src/test.rs
index 0476cc1..ec63a37 100644
--- a/proxmox-time/src/test.rs
+++ b/proxmox-time/src/test.rs
@@ -225,7 +225,7 @@ fn test_calendar_event_weekday() -> Result<(), Error> {
 #[test]
 fn test_time_span_parser() -> Result<(), Error> {
     let test_value = |ts_str: &str, expect: f64| -> Result<(), Error> {
-        let ts = parse_time_span(ts_str)?;
+        let ts: TimeSpan = ts_str.parse()?;
         assert_eq!(f64::from(ts), expect, "{}", ts_str);
         Ok(())
     };
diff --git a/proxmox-time/src/time_span.rs b/proxmox-time/src/time_span.rs
index 2f8cf59..0a6c9ca 100644
--- a/proxmox-time/src/time_span.rs
+++ b/proxmox-time/src/time_span.rs
@@ -198,9 +198,18 @@ fn parse_time_unit(i: &str) -> IResult<&str, &str> {
     }
 }
 
+impl std::str::FromStr for TimeSpan {
+    type Err = Error;
+
+    fn from_str(i: &str) -> Result<Self, Self::Err> {
+        parse_complete_line("calendar event", i, parse_time_span_incomplete)
+    }
+}
+
 /// Parse a [TimeSpan]
+#[deprecated="Use std::str::FromStr trait instead."]
 pub fn parse_time_span(i: &str) -> Result<TimeSpan, Error> {
-    parse_complete_line("time span", i, parse_time_span_incomplete)
+    i.parse()
 }
 
 fn parse_time_span_incomplete(mut i: &str) -> IResult<&str, TimeSpan> {
@@ -259,7 +268,7 @@ fn parse_time_span_incomplete(mut i: &str) -> IResult<&str, TimeSpan> {
 
 /// Verify the format of the [TimeSpan]
 pub fn verify_time_span(i: &str) -> Result<(), Error> {
-    parse_time_span(i)?;
+    let _: TimeSpan = i.parse()?;
     Ok(())
 }
 
-- 
2.30.2





More information about the pbs-devel mailing list