refactor: improve api encapsulation

This commit is contained in:
yuankunzhang
2025-08-17 22:19:15 +08:00
parent 7d0c3e4500
commit 84d4e294df
2 changed files with 28 additions and 14 deletions
+25 -4
View File
@@ -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<S: AsRef<str> + Clone>(
base: Zoned,
input: S,
) -> Result<Zoned, ParseDateTimeError> {
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<S: AsRef<str> + Clone>(input: S) -> Result<Zoned, ParseDateTimeError> {
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<Zoned, ParseDateTimeError> {
fn at_date(builder: DateTimeBuilder, base: Zoned) -> Result<Zoned, ParseDateTimeError> {
builder
.set_base(base)
.build()
@@ -78,7 +99,7 @@ pub(crate) fn at_date(builder: DateTimeBuilder, base: Zoned) -> Result<Zoned, Pa
/// Build a `Zoned` object from a `DateTimeBuilder` and a default `Zoned` object
/// (the current time in the local timezone).
pub(crate) fn at_local(builder: DateTimeBuilder) -> Result<Zoned, ParseDateTimeError> {
fn at_local(builder: DateTimeBuilder) -> Result<Zoned, ParseDateTimeError> {
builder.build().ok_or(ParseDateTimeError::InvalidInput)
}
@@ -177,7 +198,7 @@ pub(crate) fn at_local(builder: DateTimeBuilder) -> Result<Zoned, ParseDateTimeE
///
/// optional_whitespace = { whitespace } ;
/// ```
pub(crate) fn parse(input: &mut &str) -> ModalResult<DateTimeBuilder> {
fn parse(input: &mut &str) -> ModalResult<DateTimeBuilder> {
trace("parse", alt((parse_timestamp, parse_items))).parse_next(input)
}
+3 -10
View File
@@ -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<S: AsRef<str> + Clone>(input: S) -> Result<Zoned, ParseDateTimeError> {
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<S: AsRef<str> + Clone>(
date: Zoned,
input: S,
) -> Result<Zoned, ParseDateTimeError> {
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)]