From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id E5CE28283F for ; Tue, 30 Nov 2021 13:12:12 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id E1DD426928 for ; Tue, 30 Nov 2021 13:12:12 +0100 (CET) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [94.136.29.106]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS id 9B45C268F5 for ; Tue, 30 Nov 2021 13:12:10 +0100 (CET) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 6CDB744F19 for ; Tue, 30 Nov 2021 13:12:10 +0100 (CET) From: Dominik Csapak To: pbs-devel@lists.proxmox.com Date: Tue, 30 Nov 2021 13:11:57 +0100 Message-Id: <20211130121209.216846-4-d.csapak@proxmox.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20211130121209.216846-1-d.csapak@proxmox.com> References: <20211130121209.216846-1-d.csapak@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.185 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [lib.rs] Subject: [pbs-devel] [PATCH proxmox 03/14] proxmox-time: move common parse functions to parse_helpers X-BeenThere: pbs-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Backup Server development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 30 Nov 2021 12:12:12 -0000 Signed-off-by: Dominik Csapak --- 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> = Result<(I, O), nom::Err>; + +pub(crate) fn parse_error<'a>( + i: &'a str, + context: &'static str, +) -> nom::Err> { + 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 +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> = Result<(I, O), nom::Err>; - -fn parse_error<'a>(i: &'a str, context: &'static str) -> nom::Err> { - 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 - 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, } -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