proxmox-time: calendar_events: implement FromStr

and deprecate 'parse_calendar_event'

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
This commit is contained in:
Dominik Csapak 2021-11-30 13:12:07 +01:00 committed by Dietmar Maurer
parent a96e9fb724
commit 42420d3a5e
2 changed files with 14 additions and 5 deletions

View File

@ -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> {

View File

@ -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),
};