Skip to content

Upgrade winnow to 0.7.10 #141

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 2 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,5 @@ readme = "README.md"
[dependencies]
regex = "1.10.4"
chrono = { version="0.4.38", default-features=false, features=["std", "alloc", "clock"] }
nom = "8.0.0"
winnow = "0.5.34"
winnow = "0.7.10"
num-traits = "0.2.19"
7 changes: 5 additions & 2 deletions src/items/combined.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
//! > seconds are allowed, with either comma or period preceding the fraction.
//! > ISO 8601 fractional minutes and hours are not supported. Typically, hosts
//! > support nanosecond timestamp resolution; excess precision is silently discarded.
use winnow::{combinator::alt, seq, trace::trace, PResult, Parser};
use winnow::{
combinator::{alt, trace},
seq, ModalResult, Parser,
};

use crate::items::space;

Expand All @@ -29,7 +32,7 @@ pub struct DateTime {
pub(crate) time: Time,
}

pub fn parse(input: &mut &str) -> PResult<DateTime> {
pub fn parse(input: &mut &str) -> ModalResult<DateTime> {
seq!(DateTime {
date: trace("date iso", alt((date::iso1, date::iso2))),
// Note: the `T` is lowercased by the main parse function
Expand Down
29 changes: 14 additions & 15 deletions src/items/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,15 @@
//! > ‘September’.

use winnow::{
ascii::{alpha1, dec_uint},
combinator::{alt, opt, preceded},
ascii::alpha1,
combinator::{alt, opt, preceded, trace},
seq,
stream::AsChar,
token::{take, take_while},
trace::trace,
PResult, Parser,
ModalResult, Parser,
};

use super::s;
use super::{dec_uint, s};
use crate::ParseDateTimeError;

#[derive(PartialEq, Eq, Clone, Debug, Default)]
Expand All @@ -46,14 +45,14 @@ pub struct Date {
pub year: Option<u32>,
}

pub fn parse(input: &mut &str) -> PResult<Date> {
pub fn parse(input: &mut &str) -> ModalResult<Date> {
alt((iso1, iso2, us, literal1, literal2)).parse_next(input)
}

/// Parse `YYYY-MM-DD` or `YY-MM-DD`
///
/// This is also used by [`combined`](super::combined).
pub fn iso1(input: &mut &str) -> PResult<Date> {
pub fn iso1(input: &mut &str) -> ModalResult<Date> {
seq!(Date {
year: year.map(Some),
_: s('-'),
Expand All @@ -67,7 +66,7 @@ pub fn iso1(input: &mut &str) -> PResult<Date> {
/// Parse `YYYYMMDD`
///
/// This is also used by [`combined`](super::combined).
pub fn iso2(input: &mut &str) -> PResult<Date> {
pub fn iso2(input: &mut &str) -> ModalResult<Date> {
s((
take(4usize).try_map(|s: &str| s.parse::<u32>()),
take(2usize).try_map(|s: &str| s.parse::<u32>()),
Expand All @@ -82,7 +81,7 @@ pub fn iso2(input: &mut &str) -> PResult<Date> {
}

/// Parse `MM/DD/YYYY`, `MM/DD/YY` or `MM/DD`
fn us(input: &mut &str) -> PResult<Date> {
fn us(input: &mut &str) -> ModalResult<Date> {
seq!(Date {
month: month,
_: s('/'),
Expand All @@ -93,7 +92,7 @@ fn us(input: &mut &str) -> PResult<Date> {
}

/// Parse `14 November 2022`, `14 Nov 2022`, "14nov2022", "14-nov-2022", "14-nov2022", "14nov-2022"
fn literal1(input: &mut &str) -> PResult<Date> {
fn literal1(input: &mut &str) -> ModalResult<Date> {
seq!(Date {
day: day,
_: opt(s('-')),
Expand All @@ -104,7 +103,7 @@ fn literal1(input: &mut &str) -> PResult<Date> {
}

/// Parse `November 14, 2022` and `Nov 14, 2022`
fn literal2(input: &mut &str) -> PResult<Date> {
fn literal2(input: &mut &str) -> ModalResult<Date> {
seq!(Date {
month: literal_month,
day: day,
Expand All @@ -115,7 +114,7 @@ fn literal2(input: &mut &str) -> PResult<Date> {
.parse_next(input)
}

pub fn year(input: &mut &str) -> PResult<u32> {
pub fn year(input: &mut &str) -> ModalResult<u32> {
// 2147485547 is the maximum value accepted
// by GNU, but chrono only behaves like GNU
// for years in the range: [0, 9999], so we
Expand All @@ -140,7 +139,7 @@ pub fn year(input: &mut &str) -> PResult<u32> {
.parse_next(input)
}

fn month(input: &mut &str) -> PResult<u32> {
fn month(input: &mut &str) -> ModalResult<u32> {
s(dec_uint)
.try_map(|x| {
(1..=12)
Expand All @@ -151,7 +150,7 @@ fn month(input: &mut &str) -> PResult<u32> {
.parse_next(input)
}

fn day(input: &mut &str) -> PResult<u32> {
fn day(input: &mut &str) -> ModalResult<u32> {
s(dec_uint)
.try_map(|x| {
(1..=31)
Expand All @@ -163,7 +162,7 @@ fn day(input: &mut &str) -> PResult<u32> {
}

/// Parse the name of a month (case-insensitive)
fn literal_month(input: &mut &str) -> PResult<u32> {
fn literal_month(input: &mut &str) -> ModalResult<u32> {
s(alpha1)
.verify_map(|s: &str| {
Some(match s {
Expand Down
117 changes: 75 additions & 42 deletions src/items/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,34 +34,33 @@ mod relative;
mod time;
mod weekday;
mod epoch {
use winnow::{ascii::dec_int, combinator::preceded, PResult, Parser};
use winnow::{combinator::preceded, ModalResult, Parser};

use super::s;
pub fn parse(input: &mut &str) -> PResult<i32> {
use super::{dec_int, s};
pub fn parse(input: &mut &str) -> ModalResult<i32> {
s(preceded("@", dec_int)).parse_next(input)
}
}
mod timezone {
use super::time;
use winnow::PResult;
use winnow::ModalResult;

pub(crate) fn parse(input: &mut &str) -> PResult<time::Offset> {
pub(crate) fn parse(input: &mut &str) -> ModalResult<time::Offset> {
time::timezone(input)
}
}

use chrono::NaiveDate;
use chrono::{DateTime, Datelike, FixedOffset, TimeZone, Timelike};

use winnow::error::{AddContext, ParserError, StrContext};
use winnow::error::{ContextError, ErrMode};
use winnow::trace::trace;
use winnow::error::{StrContext, StrContextValue};
use winnow::{
ascii::multispace0,
combinator::{alt, delimited, not, peek, preceded, repeat, separated},
ascii::{digit1, multispace0},
combinator::{alt, delimited, not, opt, peek, preceded, repeat, separated, trace},
error::{ContextError, ErrMode, ParserError},
stream::AsChar,
token::{none_of, take_while},
PResult, Parser,
token::{none_of, one_of, take_while},
ModalResult, Parser,
};

use crate::ParseDateTimeError;
Expand Down Expand Up @@ -93,7 +92,7 @@ where
/// Parse the space in-between tokens
///
/// You probably want to use the [`s`] combinator instead.
fn space<'a, E>(input: &mut &'a str) -> PResult<(), E>
fn space<'a, E>(input: &mut &'a str) -> winnow::Result<(), E>
where
E: ParserError<&'a str>,
{
Expand All @@ -110,7 +109,7 @@ where
/// The last comment should be ignored.
///
/// The plus is undocumented, but it seems to be ignored.
fn ignored_hyphen_or_plus<'a, E>(input: &mut &'a str) -> PResult<(), E>
fn ignored_hyphen_or_plus<'a, E>(input: &mut &'a str) -> winnow::Result<(), E>
where
E: ParserError<&'a str>,
{
Expand All @@ -127,7 +126,7 @@ where
///
/// A comment is given between parentheses, which must be balanced. Any other
/// tokens can be within the comment.
fn comment<'a, E>(input: &mut &'a str) -> PResult<(), E>
fn comment<'a, E>(input: &mut &'a str) -> winnow::Result<(), E>
where
E: ParserError<&'a str>,
{
Expand All @@ -139,8 +138,45 @@ where
.parse_next(input)
}

/// Parse a signed decimal integer.
///
/// Rationale for not using `winnow::ascii::dec_int`: When upgrading winnow from
/// 0.5 to 0.7, we discovered that `winnow::ascii::dec_int` now accepts only the
/// following two forms:
///
/// - 0
/// - [+-][1-9][0-9]*
///
/// Inputs like [+-]0[0-9]* (e.g., `+012`) are therefore rejected. We provide a
/// custom implementation to support such zero-prefixed integers.
fn dec_int<'a, E>(input: &mut &'a str) -> winnow::Result<i32, E>
where
E: ParserError<&'a str>,
{
(opt(one_of(['+', '-'])), digit1)
.void()
.take()
.verify_map(|s: &str| s.parse().ok())
.parse_next(input)
}

/// Parse an unsigned decimal integer.
///
/// See the rationale for `dec_int` for why we don't use
/// `winnow::ascii::dec_uint`.
fn dec_uint<'a, E>(input: &mut &'a str) -> winnow::Result<u32, E>
where
E: ParserError<&'a str>,
{
digit1
.void()
.take()
.verify_map(|s: &str| s.parse().ok())
.parse_next(input)
}

// Parse an item
pub fn parse_one(input: &mut &str) -> PResult<Item> {
pub fn parse_one(input: &mut &str) -> ModalResult<Item> {
trace(
"parse_one",
alt((
Expand All @@ -157,7 +193,7 @@ pub fn parse_one(input: &mut &str) -> PResult<Item> {
.parse_next(input)
}

pub fn parse(input: &mut &str) -> PResult<Vec<Item>> {
pub fn parse(input: &mut &str) -> ModalResult<Vec<Item>> {
let mut items = Vec::new();
let mut date_seen = false;
let mut time_seen = false;
Expand All @@ -170,12 +206,13 @@ pub fn parse(input: &mut &str) -> PResult<Vec<Item>> {
match item {
Item::DateTime(ref dt) => {
if date_seen || time_seen {
return Err(ErrMode::Backtrack(ContextError::new().add_context(
&input,
StrContext::Expected(winnow::error::StrContextValue::Description(
let mut ctx_err = ContextError::new();
ctx_err.push(StrContext::Expected(
winnow::error::StrContextValue::Description(
"date or time cannot appear more than once",
)),
)));
),
));
return Err(ErrMode::Backtrack(ctx_err));
}

date_seen = true;
Expand All @@ -186,12 +223,11 @@ pub fn parse(input: &mut &str) -> PResult<Vec<Item>> {
}
Item::Date(ref d) => {
if date_seen {
return Err(ErrMode::Backtrack(ContextError::new().add_context(
&input,
StrContext::Expected(winnow::error::StrContextValue::Description(
"date cannot appear more than once",
)),
let mut ctx_err = ContextError::new();
ctx_err.push(StrContext::Expected(StrContextValue::Description(
"date cannot appear more than once",
)));
return Err(ErrMode::Backtrack(ctx_err));
}

date_seen = true;
Expand All @@ -201,34 +237,31 @@ pub fn parse(input: &mut &str) -> PResult<Vec<Item>> {
}
Item::Time(_) => {
if time_seen {
return Err(ErrMode::Backtrack(ContextError::new().add_context(
&input,
StrContext::Expected(winnow::error::StrContextValue::Description(
"time cannot appear more than once",
)),
let mut ctx_err = ContextError::new();
ctx_err.push(StrContext::Expected(StrContextValue::Description(
"time cannot appear more than once",
)));
return Err(ErrMode::Backtrack(ctx_err));
}
time_seen = true;
}
Item::Year(_) => {
if year_seen {
return Err(ErrMode::Backtrack(ContextError::new().add_context(
&input,
StrContext::Expected(winnow::error::StrContextValue::Description(
"year cannot appear more than once",
)),
let mut ctx_err = ContextError::new();
ctx_err.push(StrContext::Expected(StrContextValue::Description(
"year cannot appear more than once",
)));
return Err(ErrMode::Backtrack(ctx_err));
}
year_seen = true;
}
Item::TimeZone(_) => {
if tz_seen {
return Err(ErrMode::Backtrack(ContextError::new().add_context(
&input,
StrContext::Expected(winnow::error::StrContextValue::Description(
"timezone cannot appear more than once",
)),
let mut ctx_err = ContextError::new();
ctx_err.push(StrContext::Expected(StrContextValue::Description(
"timezone cannot appear more than once",
)));
return Err(ErrMode::Backtrack(ctx_err));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder why this is marked with backtrack, but it already was so let's leave it.

}
tz_seen = true;
}
Expand Down
Loading