forked from proxmox-mirrors/proxmox
proxmox-time: move parse_daily_duration to daily_duration.rs
Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
This commit is contained in:
parent
f61ee1372f
commit
78e1e8ce09
@ -2,10 +2,15 @@ use std::cmp::{Ordering, PartialOrd};
|
|||||||
use std::convert::{TryFrom, TryInto};
|
use std::convert::{TryFrom, TryInto};
|
||||||
|
|
||||||
use anyhow::Error;
|
use anyhow::Error;
|
||||||
|
use nom::{
|
||||||
|
bytes::complete::tag,
|
||||||
|
character::complete::space0,
|
||||||
|
error::context,
|
||||||
|
multi::separated_nonempty_list,
|
||||||
|
};
|
||||||
|
|
||||||
use crate::{TmEditor, WeekDays};
|
use crate::parse_helpers::{parse_complete_line, parse_error, parse_hm_time, IResult};
|
||||||
|
use crate::{parse_weekdays_range, TmEditor, WeekDays};
|
||||||
pub use super::parse_time::parse_daily_duration;
|
|
||||||
|
|
||||||
/// Time of Day (hour with minute)
|
/// Time of Day (hour with minute)
|
||||||
#[derive(Default, PartialEq, Clone, Debug)]
|
#[derive(Default, PartialEq, Clone, Debug)]
|
||||||
@ -79,6 +84,49 @@ impl DailyDuration {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Parse a [DailyDuration]
|
||||||
|
pub fn parse_daily_duration(i: &str) -> Result<DailyDuration, Error> {
|
||||||
|
parse_complete_line("daily duration", i, parse_daily_duration_incomplete)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_daily_duration_incomplete(mut i: &str) -> IResult<&str, DailyDuration> {
|
||||||
|
let mut duration = DailyDuration::default();
|
||||||
|
|
||||||
|
if i.starts_with(|c: char| char::is_ascii_alphabetic(&c)) {
|
||||||
|
let (n, range_list) = context(
|
||||||
|
"weekday range list",
|
||||||
|
separated_nonempty_list(tag(","), parse_weekdays_range),
|
||||||
|
)(i)?;
|
||||||
|
|
||||||
|
i = space0(n)?.0;
|
||||||
|
|
||||||
|
for range in range_list {
|
||||||
|
duration.days.insert(range);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let (i, start) = parse_hm_time(i)?;
|
||||||
|
|
||||||
|
let i = space0(i)?.0;
|
||||||
|
|
||||||
|
let (i, _) = tag("-")(i)?;
|
||||||
|
|
||||||
|
let i = space0(i)?.0;
|
||||||
|
|
||||||
|
let end_time_start = i;
|
||||||
|
|
||||||
|
let (i, end) = parse_hm_time(i)?;
|
||||||
|
|
||||||
|
if start > end {
|
||||||
|
return Err(parse_error(end_time_start, "end time before start time"));
|
||||||
|
}
|
||||||
|
|
||||||
|
duration.start = start;
|
||||||
|
duration.end = end;
|
||||||
|
|
||||||
|
Ok((i, duration))
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
|
|
||||||
|
@ -375,46 +375,3 @@ fn parse_time_span_incomplete(mut i: &str) -> IResult<&str, TimeSpan> {
|
|||||||
|
|
||||||
Ok((i, ts))
|
Ok((i, ts))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Parse a [DailyDuration]
|
|
||||||
pub fn parse_daily_duration(i: &str) -> Result<DailyDuration, Error> {
|
|
||||||
parse_complete_line("daily duration", i, parse_daily_duration_incomplete)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn parse_daily_duration_incomplete(mut i: &str) -> IResult<&str, DailyDuration> {
|
|
||||||
|
|
||||||
let mut duration = DailyDuration::default();
|
|
||||||
|
|
||||||
if i.starts_with(|c: char| char::is_ascii_alphabetic(&c)) {
|
|
||||||
|
|
||||||
let (n, range_list) = context(
|
|
||||||
"weekday range list",
|
|
||||||
separated_nonempty_list(tag(","), parse_weekdays_range)
|
|
||||||
)(i)?;
|
|
||||||
|
|
||||||
i = space0(n)?.0;
|
|
||||||
|
|
||||||
for range in range_list { duration.days.insert(range); }
|
|
||||||
}
|
|
||||||
|
|
||||||
let (i, start) = parse_hm_time(i)?;
|
|
||||||
|
|
||||||
let i = space0(i)?.0;
|
|
||||||
|
|
||||||
let (i, _) = tag("-")(i)?;
|
|
||||||
|
|
||||||
let i = space0(i)?.0;
|
|
||||||
|
|
||||||
let end_time_start = i;
|
|
||||||
|
|
||||||
let (i, end) = parse_hm_time(i)?;
|
|
||||||
|
|
||||||
if start > end {
|
|
||||||
return Err(parse_error(end_time_start, "end time before start time"));
|
|
||||||
}
|
|
||||||
|
|
||||||
duration.start = start;
|
|
||||||
duration.end = end;
|
|
||||||
|
|
||||||
Ok((i, duration))
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user