From: Dominik Csapak <d.csapak@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [pbs-devel] [PATCH proxmox 03/14] proxmox-time: move common parse functions to parse_helpers
Date: Tue, 30 Nov 2021 13:11:57 +0100 [thread overview]
Message-ID: <20211130121209.216846-4-d.csapak@proxmox.com> (raw)
In-Reply-To: <20211130121209.216846-1-d.csapak@proxmox.com>
Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
---
proxmox-time/src/lib.rs | 2 +
proxmox-time/src/parse_helpers.rs | 75 +++++++++++++++++++++++++++++++
proxmox-time/src/parse_time.rs | 56 +----------------------
3 files changed, 78 insertions(+), 55 deletions(-)
create mode 100644 proxmox-time/src/parse_helpers.rs
diff --git a/proxmox-time/src/lib.rs b/proxmox-time/src/lib.rs
index d72509f..d2a4cb7 100644
--- a/proxmox-time/src/lib.rs
+++ b/proxmox-time/src/lib.rs
@@ -13,6 +13,8 @@ pub use parse_time::*;
mod time;
pub use time::*;
+pub(crate) mod parse_helpers;
+
mod daily_duration;
pub use daily_duration::*;
diff --git a/proxmox-time/src/parse_helpers.rs b/proxmox-time/src/parse_helpers.rs
new file mode 100644
index 0000000..4890b2f
--- /dev/null
+++ b/proxmox-time/src/parse_helpers.rs
@@ -0,0 +1,75 @@
+use anyhow::{bail, Error};
+
+use super::daily_duration::*;
+
+use nom::{
+ bytes::complete::tag,
+ character::complete::digit1,
+ combinator::{all_consuming, map_res, opt, recognize},
+ error::{ParseError, VerboseError},
+ sequence::{preceded, tuple},
+};
+
+pub(crate) type IResult<I, O, E = VerboseError<I>> = Result<(I, O), nom::Err<E>>;
+
+pub(crate) fn parse_error<'a>(
+ i: &'a str,
+ context: &'static str,
+) -> nom::Err<VerboseError<&'a str>> {
+ let err = VerboseError { errors: Vec::new() };
+ let err = VerboseError::add_context(i, context, err);
+ nom::Err::Error(err)
+}
+
+// Parse a 64 bit unsigned integer
+pub(crate) fn parse_u64(i: &str) -> IResult<&str, u64> {
+ map_res(recognize(digit1), str::parse)(i)
+}
+
+// Parse complete input, generate simple error message (use this for sinple line input).
+pub(crate) fn parse_complete_line<'a, F, O>(what: &str, i: &'a str, parser: F) -> Result<O, Error>
+where
+ F: Fn(&'a str) -> IResult<&'a str, O>,
+{
+ match all_consuming(parser)(i) {
+ Err(nom::Err::Error(VerboseError { errors }))
+ | Err(nom::Err::Failure(VerboseError { errors })) => {
+ if errors.is_empty() {
+ bail!("unable to parse {}", what);
+ } else {
+ bail!(
+ "unable to parse {} at '{}' - {:?}",
+ what,
+ errors[0].0,
+ errors[0].1
+ );
+ }
+ }
+ Err(err) => {
+ bail!("unable to parse {} - {}", what, err);
+ }
+ Ok((_, data)) => Ok(data),
+ }
+}
+
+pub(crate) fn parse_time_comp(max: usize) -> impl Fn(&str) -> IResult<&str, u32> {
+ move |i: &str| {
+ let (i, v) = map_res(recognize(digit1), str::parse)(i)?;
+ if (v as usize) >= max {
+ return Err(parse_error(i, "time value too large"));
+ }
+ Ok((i, v))
+ }
+}
+
+pub(crate) fn parse_hm_time(i: &str) -> IResult<&str, HmTime> {
+ let (i, (hour, opt_minute)) = tuple((
+ parse_time_comp(24),
+ opt(preceded(tag(":"), parse_time_comp(60))),
+ ))(i)?;
+
+ match opt_minute {
+ Some(minute) => Ok((i, HmTime { hour, minute })),
+ None => Ok((i, HmTime { hour, minute: 0 })),
+ }
+}
diff --git a/proxmox-time/src/parse_time.rs b/proxmox-time/src/parse_time.rs
index aa1c58e..7b0e168 100644
--- a/proxmox-time/src/parse_time.rs
+++ b/proxmox-time/src/parse_time.rs
@@ -15,38 +15,7 @@ use nom::{
multi::separated_nonempty_list,
};
-type IResult<I, O, E = VerboseError<I>> = Result<(I, O), nom::Err<E>>;
-
-fn parse_error<'a>(i: &'a str, context: &'static str) -> nom::Err<VerboseError<&'a str>> {
- let err = VerboseError { errors: Vec::new() };
- let err = VerboseError::add_context(i, context, err);
- nom::Err::Error(err)
-}
-
-// Parse a 64 bit unsigned integer
-fn parse_u64(i: &str) -> IResult<&str, u64> {
- map_res(recognize(digit1), str::parse)(i)
-}
-
-// Parse complete input, generate simple error message (use this for sinple line input).
-fn parse_complete_line<'a, F, O>(what: &str, i: &'a str, parser: F) -> Result<O, Error>
- where F: Fn(&'a str) -> IResult<&'a str, O>,
-{
- match all_consuming(parser)(i) {
- Err(nom::Err::Error(VerboseError { errors })) |
- Err(nom::Err::Failure(VerboseError { errors })) => {
- if errors.is_empty() {
- bail!("unable to parse {}", what);
- } else {
- bail!("unable to parse {} at '{}' - {:?}", what, errors[0].0, errors[0].1);
- }
- }
- Err(err) => {
- bail!("unable to parse {} - {}", what, err);
- }
- Ok((_, data)) => Ok(data),
- }
-}
+use crate::parse_helpers::{parse_complete_line, parse_error, parse_hm_time, parse_time_comp, parse_u64, IResult};
lazy_static! {
static ref TIME_SPAN_UNITS: HashMap<&'static str, f64> = {
@@ -129,16 +98,6 @@ struct DateSpec {
day: Vec<DateTimeValue>,
}
-fn parse_time_comp(max: usize) -> impl Fn(&str) -> IResult<&str, u32> {
- move |i: &str| {
- let (i, v) = map_res(recognize(digit1), str::parse)(i)?;
- if (v as usize) >= max {
- return Err(parse_error(i, "time value too large"));
- }
- Ok((i, v))
- }
-}
-
fn parse_weekday(i: &str) -> IResult<&str, WeekDays> {
let (i, text) = alpha1(i)?;
@@ -501,16 +460,3 @@ fn parse_daily_duration_incomplete(mut i: &str) -> IResult<&str, DailyDuration>
Ok((i, duration))
}
-
-fn parse_hm_time(i: &str) -> IResult<&str, HmTime> {
-
- let (i, (hour, opt_minute)) = tuple((
- parse_time_comp(24),
- opt(preceded(tag(":"), parse_time_comp(60))),
- ))(i)?;
-
- match opt_minute {
- Some(minute) => Ok((i, HmTime { hour, minute })),
- None => Ok((i, HmTime { hour, minute: 0})),
- }
-}
--
2.30.2
next prev parent reply other threads:[~2021-11-30 12:12 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-11-30 12:11 [pbs-devel] [PATCH proxmox/proxmox-backup 00/14] improve & cleanup proxmox-time Dominik Csapak
2021-11-30 12:11 ` [pbs-devel] [PATCH proxmox 01/14] proxmox-time: calendar-events: implement repeated ranges Dominik Csapak
2021-11-30 12:11 ` [pbs-devel] [PATCH proxmox 02/14] proxmox-time: calendar-events: make hour optional Dominik Csapak
2021-11-30 12:11 ` Dominik Csapak [this message]
2021-11-30 12:11 ` [pbs-devel] [PATCH proxmox 04/14] proxmox-time: move WeekDays into own file Dominik Csapak
2021-11-30 12:11 ` [pbs-devel] [PATCH proxmox 05/14] proxmox-time: split DateTimeValue " Dominik Csapak
2021-11-30 12:12 ` [pbs-devel] [PATCH proxmox 06/14] proxmox-time: move parse_daily_duration to daily_duration.rs Dominik Csapak
2021-11-30 12:12 ` [pbs-devel] [PATCH proxmox 07/14] proxmox-time: daily_duration.rs: rustfmt Dominik Csapak
2021-11-30 12:12 ` [pbs-devel] [PATCH proxmox 08/14] proxmox-time: move CalendarEvent into calendar_events.rs Dominik Csapak
2021-11-30 12:12 ` [pbs-devel] [PATCH proxmox 09/14] proxmox-time: move TimeSpan into time_span.rs Dominik Csapak
2021-11-30 12:12 ` [pbs-devel] [PATCH proxmox 10/14] proxmox-time: move tests from time.rs to test.rs Dominik Csapak
2021-11-30 12:12 ` [pbs-devel] [PATCH proxmox 11/14] proxmox-time: lib.rs: rustfmt Dominik Csapak
2021-11-30 12:12 ` [pbs-devel] [PATCH proxmox 12/14] proxmox-time: calendar-events: make compute_next_event a method Dominik Csapak
2021-11-30 12:12 ` [pbs-devel] [PATCH proxmox 13/14] proxmox-time: calendar_events: implement FromStr Dominik Csapak
2021-11-30 12:12 ` [pbs-devel] [PATCH proxmox 14/14] proxmox-time: time-span: " Dominik Csapak
2021-11-30 12:12 ` [pbs-devel] [PATCH proxmox-backup 1/1] remove use of deprecated functions from proxmox-time Dominik Csapak
2021-12-01 6:26 ` [pbs-devel] applied: " Dietmar Maurer
2021-12-01 6:20 ` [pbs-devel] applied: [PATCH proxmox/proxmox-backup 00/14] improve & cleanup proxmox-time Dietmar Maurer
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20211130121209.216846-4-d.csapak@proxmox.com \
--to=d.csapak@proxmox.com \
--cc=pbs-devel@lists.proxmox.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal