diff --git a/src/items/mod.rs b/src/items/mod.rs index 7c9b262..6d8b4c9 100644 --- a/src/items/mod.rs +++ b/src/items/mod.rs @@ -57,7 +57,7 @@ use winnow::{ use crate::ParseDateTimeError; #[derive(PartialEq, Debug)] -pub(crate) enum Item { +enum Item { Timestamp(epoch::Timestamp), DateTime(combined::DateTime), Date(date::Date), @@ -68,8 +68,29 @@ pub(crate) enum Item { Pure(String), } +/// Parse a date and time string based on a specific date. +pub(crate) fn parse_at_date + Clone>( + base: Zoned, + input: S, +) -> Result { + let input = input.as_ref().to_ascii_lowercase(); + match parse(&mut input.as_str()) { + Ok(builder) => at_date(builder, base), + Err(_) => Err(ParseDateTimeError::InvalidInput), + } +} + +/// Parse a date and time string based on the current local time. +pub(crate) fn parse_at_local + Clone>(input: S) -> Result { + let input = input.as_ref().to_ascii_lowercase(); + match parse(&mut input.as_str()) { + Ok(builder) => at_local(builder), + Err(_) => Err(ParseDateTimeError::InvalidInput), + } +} + /// Build a `Zoned` object from a `DateTimeBuilder` and a base `Zoned` object. -pub(crate) fn at_date(builder: DateTimeBuilder, base: Zoned) -> Result { +fn at_date(builder: DateTimeBuilder, base: Zoned) -> Result { builder .set_base(base) .build() @@ -78,7 +99,7 @@ pub(crate) fn at_date(builder: DateTimeBuilder, base: Zoned) -> Result Result { +fn at_local(builder: DateTimeBuilder) -> Result { builder.build().ok_or(ParseDateTimeError::InvalidInput) } @@ -177,7 +198,7 @@ pub(crate) fn at_local(builder: DateTimeBuilder) -> Result ModalResult { +fn parse(input: &mut &str) -> ModalResult { trace("parse", alt((parse_timestamp, parse_items))).parse_next(input) } diff --git a/src/lib.rs b/src/lib.rs index 49936d6..c31565a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -64,12 +64,9 @@ impl Error for ParseDateTimeError {} /// This function will return `Err(ParseDateTimeError::InvalidInput)` if the /// input string cannot be parsed as a relative time. pub fn parse_datetime + Clone>(input: S) -> Result { - let input = input.as_ref().to_ascii_lowercase(); - match items::parse(&mut input.as_str()) { - Ok(x) => items::at_local(x), - Err(_) => Err(ParseDateTimeError::InvalidInput), - } + items::parse_at_local(input) } + /// Parses a time string at a specific date and returns a `Zoned` object /// representing the absolute time of the string. /// @@ -107,11 +104,7 @@ pub fn parse_datetime_at_date + Clone>( date: Zoned, input: S, ) -> Result { - let input = input.as_ref().to_ascii_lowercase(); - match items::parse(&mut input.as_str()) { - Ok(x) => items::at_date(x, date), - Err(_) => Err(ParseDateTimeError::InvalidInput), - } + items::parse_at_date(date, input) } #[cfg(test)]