diff --git a/Cargo.toml b/Cargo.toml index 707025a..73d8ec3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,7 +22,7 @@ version = "0.1.36" default-features = false [dependencies.nom] -version = "6" +version = "7.0.0" default-features = false [dependencies.chrono] diff --git a/src/common.rs b/src/common.rs index 225b430..1df8aa6 100644 --- a/src/common.rs +++ b/src/common.rs @@ -96,7 +96,7 @@ pub fn take_n_digits(n: usize) -> impl FnMut(&str) -> StrResult<&str> { pub fn two_digits(remain: &str) -> StrResult { let (remain, two) = take_n_digits(2)(remain)?; // NonZeroU8's FromStr implementation rejects 00. - let (_, parsed) = nom::parse_to!(two, T)?; + let parsed = two.parse_to_err()?; Ok((remain, parsed)) } @@ -104,7 +104,7 @@ pub fn two_digits(remain: &str) -> StrResult { pub fn two_digits_zero_none(remain: &str) -> StrResult> { let (remain, two) = take_n_digits(2).parse(remain)?; // NonZeroU8's FromStr implementation rejects 00. - let (_, parsed) = nom::parse_to!(two, u8)?; + let parsed = two.parse_to_err()?; Ok((remain, NonZeroU8::new(parsed))) } @@ -124,10 +124,29 @@ pub enum UnvalidatedTz { HoursMinutes { positive: bool, hh: u8, mm: u8 }, } +pub(crate) trait ParseToExt: ParseTo +where + R: FromStr, +{ + fn parse_to_err(self) -> Result>> + where + Self: Sized, + { + self.parse_to().ok_or_else(|| { + nom::Err::Error(NomParseError::from_error_kind( + self, + // whatever. nom should have an Other errorkind. + nom::error::ErrorKind::MapRes, + )) + }) + } +} +impl, R: FromStr> ParseToExt for S {} + pub fn year_n(n: usize) -> impl FnMut(&str) -> StrResult { move |remain| { let (remain, four) = take_n_digits(n)(remain)?; - let (_, parsed) = nom::parse_to!(four, i32)?; + let parsed: i32 = four.parse_to_err()?; Ok((remain, parsed)) } } @@ -136,7 +155,7 @@ pub fn year_n_signed(n: usize) -> impl FnMut(&str) -> StrResult { move |remain| { let (remain, sign) = minus_sign(-1i32, 1)(remain)?; let (remain, four) = take_n_digits(n)(remain)?; - let (_, parsed) = nom::parse_to!(four, i32)?; + let parsed: i32 = four.parse_to_err()?; if sign == -1 && parsed == 0 { return Err(nom::Err::Error(NomParseError::from_error_kind( remain, @@ -153,7 +172,7 @@ pub fn signed_year_min_n(n: usize) -> impl FnMut(&str) -> StrResult { let nonzero_digit = ncc::satisfy(|c| c.is_ascii_digit() && c != '0'); let (remain, sign) = minus_sign(-1i64, 1i64).parse(remain)?; let (remain, digs) = nc::recognize(nonzero_digit.and(take_min_n_digits(n - 1)))(remain)?; - let (_, parsed) = nom::parse_to!(digs, i64)?; + let parsed: i64 = digs.parse_to_err()?; Ok((remain, parsed * sign)) } } diff --git a/src/level0/parser.rs b/src/level0/parser.rs index a2c5871..565a938 100644 --- a/src/level0/parser.rs +++ b/src/level0/parser.rs @@ -4,8 +4,8 @@ // // Copyright © 2021 Corporation for Digital Scholarship -use crate::helpers::ParserExt; use crate::ParseError; +use crate::{common::ParseToExt, helpers::ParserExt}; #[allow(unused_imports)] use nom::{ @@ -78,7 +78,7 @@ pub(crate) fn date(remain: &str) -> StrResult { /// Level 0 year only, so simply exactly four digits 0-9. That's it. fn year4(remain: &str) -> StrResult { let (remain, four) = take_n_digits(4)(remain)?; - let (_, parsed) = nom::parse_to!(four, i32)?; + let parsed = four.parse_to_err()?; Ok((remain, parsed)) } diff --git a/src/level2/parser.rs b/src/level2/parser.rs index 8cc03ed..63c56fa 100644 --- a/src/level2/parser.rs +++ b/src/level2/parser.rs @@ -12,7 +12,7 @@ use nom::{ }; use crate::{ - common::{signed_year_min_n, year_n_signed, StrResult}, + common::{signed_year_min_n, year_n_signed, ParseToExt, StrResult}, helpers::ParserExt, ParseError, }; @@ -80,14 +80,8 @@ fn scientific_y(remain: &str) -> StrResult { .or(ns::preceded(ncc::char('Y'), signed_year_min_n(5)).map(|y| (y, None))) .and(ns::preceded(s, ncc::digit1).optional()) .parse(remain)?; - let exponent = opt_e - .map(|e| nom::parse_to!(e, u16)) - .transpose()? - .map(|x| x.1); - let sig_digits = opt_s - .map(|s| nom::parse_to!(s, u16)) - .transpose()? - .map(|x| x.1); + let exponent = opt_e.map(ParseToExt::::parse_to_err).transpose()?; + let sig_digits = opt_s.map(ParseToExt::::parse_to_err).transpose()?; Ok(( remain, ScientificYear { @@ -103,7 +97,7 @@ fn scientific_4digit(remain: &str) -> StrResult { let (remain, (year, sd)) = year_n_signed(4) .and(ns::preceded(s, ncc::digit1)) .parse(remain)?; - let (_, sd) = nom::parse_to!(sd, u16)?; + let sd: u16 = sd.parse_to_err()?; Ok(( remain, ScientificYear {