From 59eac72c44b706e1e1d645264557685ddbf9bdf0 Mon Sep 17 00:00:00 2001 From: yuankunzhang Date: Sat, 23 Aug 2025 22:17:21 +0800 Subject: [PATCH] refactor: improve timezone offset parsing and validation --- src/items/primitive.rs | 14 +- src/items/time.rs | 32 +--- src/items/timezone.rs | 324 +++++++++++++++++++++++++++-------------- 3 files changed, 232 insertions(+), 138 deletions(-) diff --git a/src/items/primitive.rs b/src/items/primitive.rs index e1de350..d4e13b3 100644 --- a/src/items/primitive.rs +++ b/src/items/primitive.rs @@ -107,14 +107,20 @@ where O: Uint + FromStr, E: ParserError<&'a str>, { - digit1 - .void() - .take() + dec_uint_str .verify_map(|s: &str| s.parse().ok()) .parse_next(input) } -/// Parse a colon preceded by whitespace +/// Parse an unsigned decimal integer as a string slice. +pub(super) fn dec_uint_str<'a, E>(input: &mut &'a str) -> winnow::Result<&'a str, E> +where + E: ParserError<&'a str>, +{ + digit1.void().take().parse_next(input) +} + +/// Parse a colon preceded by whitespace. pub(super) fn colon<'a, E>(input: &mut &'a str) -> winnow::Result<(), E> where E: ParserError<&'a str>, diff --git a/src/items/time.rs b/src/items/time.rs index 2a79943..bbad9a8 100644 --- a/src/items/time.rs +++ b/src/items/time.rs @@ -46,7 +46,7 @@ use winnow::{ use super::{ epoch::sec_and_nsec, primitive::{colon, ctx_err, dec_uint, s}, - timezone::{timezone_num, Offset}, + timezone::{timezone_offset, Offset}, }; #[derive(PartialEq, Clone, Debug, Default)] @@ -55,7 +55,7 @@ pub(crate) struct Time { pub(crate) minute: u8, pub(crate) second: u8, pub(crate) nanosecond: u32, - pub(crate) offset: Option, + pub(super) offset: Option, } impl TryFrom