From 793d18dce7461af00135e13d09e05f6ef824747c Mon Sep 17 00:00:00 2001 From: yuankunzhang Date: Sun, 20 Jul 2025 03:28:52 +0000 Subject: [PATCH] feat: add a `ctx_err` util function --- src/items/primitive.rs | 9 ++++++++- src/items/time.rs | 20 +++++--------------- 2 files changed, 13 insertions(+), 16 deletions(-) diff --git a/src/items/primitive.rs b/src/items/primitive.rs index 80f91fe..0f54359 100644 --- a/src/items/primitive.rs +++ b/src/items/primitive.rs @@ -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 +} diff --git a/src/items/time.rs b/src/items/time.rs index 8a9ecb4..8894615 100644 --- a/src/items/time.rs +++ b/src/items/time.rs @@ -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