[pbs-devel] [PATCH proxmox 13/14] proxmox-time: calendar_events: implement FromStr
Dominik Csapak
d.csapak at proxmox.com
Tue Nov 30 13:12:07 CET 2021
and deprecate 'parse_calendar_event'
Signed-off-by: Dominik Csapak <d.csapak at proxmox.com>
---
proxmox-time/src/calendar_event.rs | 13 +++++++++++--
proxmox-time/src/test.rs | 6 +++---
2 files changed, 14 insertions(+), 5 deletions(-)
diff --git a/proxmox-time/src/calendar_event.rs b/proxmox-time/src/calendar_event.rs
index a839f9d..d1d74a2 100644
--- a/proxmox-time/src/calendar_event.rs
+++ b/proxmox-time/src/calendar_event.rs
@@ -163,9 +163,17 @@ impl CalendarEvent {
}
}
+impl std::str::FromStr for CalendarEvent {
+ type Err = Error;
+
+ fn from_str(i: &str) -> Result<Self, Self::Err> {
+ parse_complete_line("calendar event", i, parse_calendar_event_incomplete)
+ }
+}
+
/// Verify the format of the [CalendarEvent]
pub fn verify_calendar_event(i: &str) -> Result<(), Error> {
- parse_calendar_event(i)?;
+ let _: CalendarEvent = i.parse()?;
Ok(())
}
@@ -180,8 +188,9 @@ pub fn compute_next_event(
}
/// Parse a [CalendarEvent]
+#[deprecated="use std::str::FromStr trait instead"]
pub fn parse_calendar_event(i: &str) -> Result<CalendarEvent, Error> {
- parse_complete_line("calendar event", i, parse_calendar_event_incomplete)
+ i.parse()
}
fn parse_calendar_event_incomplete(mut i: &str) -> IResult<&str, CalendarEvent> {
diff --git a/proxmox-time/src/test.rs b/proxmox-time/src/test.rs
index 8c5e1cf..0476cc1 100644
--- a/proxmox-time/src/test.rs
+++ b/proxmox-time/src/test.rs
@@ -3,7 +3,7 @@ use anyhow::bail;
use super::*;
fn test_event(v: &'static str) -> Result<(), Error> {
- match parse_calendar_event(v) {
+ match v.parse::<CalendarEvent>() {
Ok(event) => println!("CalendarEvent '{}' => {:?}", v, event),
Err(err) => bail!("parsing '{}' failed - {}", v, err),
}
@@ -18,7 +18,7 @@ const fn make_test_time(mday: i32, hour: i32, min: i32) -> i64 {
#[test]
fn test_compute_next_event() -> Result<(), Error> {
let test_value = |v: &'static str, last: i64, expect: i64| -> Result<i64, Error> {
- let event = match parse_calendar_event(v) {
+ let event: CalendarEvent = match v.parse() {
Ok(event) => event,
Err(err) => bail!("parsing '{}' failed - {}", v, err),
};
@@ -44,7 +44,7 @@ fn test_compute_next_event() -> Result<(), Error> {
};
let test_never = |v: &'static str, last: i64| -> Result<(), Error> {
- let event = match parse_calendar_event(v) {
+ let event: CalendarEvent = match v.parse() {
Ok(event) => event,
Err(err) => bail!("parsing '{}' failed - {}", v, err),
};
--
2.30.2
More information about the pbs-devel
mailing list