Merge pull request #182 from yuankunzhang/add-ctx-err

feat: add a `ctx_err` util function
This commit is contained in:
Daniel Hofstetter
2025-07-20 14:07:21 +02:00
committed by GitHub
2 changed files with 13 additions and 16 deletions
+8 -1
View File
@@ -6,7 +6,7 @@
use winnow::{
ascii::{digit1, multispace0},
combinator::{alt, delimited, not, opt, peek, preceded, repeat, separated},
error::ParserError,
error::{ContextError, ParserError, StrContext, StrContextValue},
stream::AsChar,
token::{none_of, one_of, take_while},
Parser,
@@ -130,3 +130,10 @@ where
.verify_map(|s: &str| s.replace(",", ".").parse().ok())
.parse_next(input)
}
/// Create a context error with a reason.
pub(super) fn ctx_err(reason: &'static str) -> ContextError {
let mut err = ContextError::new();
err.push(StrContext::Expected(StrContextValue::Description(reason)));
err
}
+5 -15
View File
@@ -42,7 +42,7 @@ use std::fmt::Display;
use chrono::FixedOffset;
use winnow::{
combinator::{alt, opt, peek, preceded},
error::{ContextError, ErrMode, StrContext, StrContextValue},
error::{ContextError, ErrMode},
seq,
stream::AsChar,
token::take_while,
@@ -52,7 +52,7 @@ use winnow::{
use crate::ParseDateTimeError;
use super::{
primitive::{dec_uint, float, s},
primitive::{ctx_err, dec_uint, float, s},
relative,
};
@@ -191,11 +191,9 @@ fn am_pm_time(input: &mut &str) -> ModalResult<Time> {
.parse_next(input)?;
if h == 0 {
let mut ctx_err = ContextError::new();
ctx_err.push(StrContext::Expected(StrContextValue::Description(
return Err(ErrMode::Cut(ctx_err(
"hour must be greater than 0 when meridiem is specified",
)));
return Err(ErrMode::Cut(ctx_err));
}
let mut h = h % 12;
@@ -261,19 +259,11 @@ fn timezone_num(input: &mut &str) -> ModalResult<Offset> {
.parse_next(input)
.and_then(|(negative, (hours, minutes))| {
if !(0..=12).contains(&hours) {
let mut ctx_err = ContextError::new();
ctx_err.push(StrContext::Expected(StrContextValue::Description(
"timezone hour between 0 and 12",
)));
return Err(ErrMode::Cut(ctx_err));
return Err(ErrMode::Cut(ctx_err("timezone hour between 0 and 12")));
}
if !(0..=60).contains(&minutes) {
let mut ctx_err = ContextError::new();
ctx_err.push(StrContext::Expected(StrContextValue::Description(
"timezone minute between 0 and 60",
)));
return Err(ErrMode::Cut(ctx_err));
return Err(ErrMode::Cut(ctx_err("timezone minute between 0 and 60")));
}
Ok(Offset {