Skip to content

Commit e225cf0

Browse files
authored
Merge pull request #215 from yuankunzhang/improve-api-encapsulation
refactor: improve api encapsulation
2 parents 7d0c3e4 + 84d4e29 commit e225cf0

File tree

2 files changed

+28
-14
lines changed

2 files changed

+28
-14
lines changed

src/items/mod.rs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ use winnow::{
5757
use crate::ParseDateTimeError;
5858

5959
#[derive(PartialEq, Debug)]
60-
pub(crate) enum Item {
60+
enum Item {
6161
Timestamp(epoch::Timestamp),
6262
DateTime(combined::DateTime),
6363
Date(date::Date),
@@ -68,8 +68,29 @@ pub(crate) enum Item {
6868
Pure(String),
6969
}
7070

71+
/// Parse a date and time string based on a specific date.
72+
pub(crate) fn parse_at_date<S: AsRef<str> + Clone>(
73+
base: Zoned,
74+
input: S,
75+
) -> Result<Zoned, ParseDateTimeError> {
76+
let input = input.as_ref().to_ascii_lowercase();
77+
match parse(&mut input.as_str()) {
78+
Ok(builder) => at_date(builder, base),
79+
Err(_) => Err(ParseDateTimeError::InvalidInput),
80+
}
81+
}
82+
83+
/// Parse a date and time string based on the current local time.
84+
pub(crate) fn parse_at_local<S: AsRef<str> + Clone>(input: S) -> Result<Zoned, ParseDateTimeError> {
85+
let input = input.as_ref().to_ascii_lowercase();
86+
match parse(&mut input.as_str()) {
87+
Ok(builder) => at_local(builder),
88+
Err(_) => Err(ParseDateTimeError::InvalidInput),
89+
}
90+
}
91+
7192
/// Build a `Zoned` object from a `DateTimeBuilder` and a base `Zoned` object.
72-
pub(crate) fn at_date(builder: DateTimeBuilder, base: Zoned) -> Result<Zoned, ParseDateTimeError> {
93+
fn at_date(builder: DateTimeBuilder, base: Zoned) -> Result<Zoned, ParseDateTimeError> {
7394
builder
7495
.set_base(base)
7596
.build()
@@ -78,7 +99,7 @@ pub(crate) fn at_date(builder: DateTimeBuilder, base: Zoned) -> Result<Zoned, Pa
7899

79100
/// Build a `Zoned` object from a `DateTimeBuilder` and a default `Zoned` object
80101
/// (the current time in the local timezone).
81-
pub(crate) fn at_local(builder: DateTimeBuilder) -> Result<Zoned, ParseDateTimeError> {
102+
fn at_local(builder: DateTimeBuilder) -> Result<Zoned, ParseDateTimeError> {
82103
builder.build().ok_or(ParseDateTimeError::InvalidInput)
83104
}
84105

@@ -177,7 +198,7 @@ pub(crate) fn at_local(builder: DateTimeBuilder) -> Result<Zoned, ParseDateTimeE
177198
///
178199
/// optional_whitespace = { whitespace } ;
179200
/// ```
180-
pub(crate) fn parse(input: &mut &str) -> ModalResult<DateTimeBuilder> {
201+
fn parse(input: &mut &str) -> ModalResult<DateTimeBuilder> {
181202
trace("parse", alt((parse_timestamp, parse_items))).parse_next(input)
182203
}
183204

src/lib.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,9 @@ impl Error for ParseDateTimeError {}
6464
/// This function will return `Err(ParseDateTimeError::InvalidInput)` if the
6565
/// input string cannot be parsed as a relative time.
6666
pub fn parse_datetime<S: AsRef<str> + Clone>(input: S) -> Result<Zoned, ParseDateTimeError> {
67-
let input = input.as_ref().to_ascii_lowercase();
68-
match items::parse(&mut input.as_str()) {
69-
Ok(x) => items::at_local(x),
70-
Err(_) => Err(ParseDateTimeError::InvalidInput),
71-
}
67+
items::parse_at_local(input)
7268
}
69+
7370
/// Parses a time string at a specific date and returns a `Zoned` object
7471
/// representing the absolute time of the string.
7572
///
@@ -107,11 +104,7 @@ pub fn parse_datetime_at_date<S: AsRef<str> + Clone>(
107104
date: Zoned,
108105
input: S,
109106
) -> Result<Zoned, ParseDateTimeError> {
110-
let input = input.as_ref().to_ascii_lowercase();
111-
match items::parse(&mut input.as_str()) {
112-
Ok(x) => items::at_date(x, date),
113-
Err(_) => Err(ParseDateTimeError::InvalidInput),
114-
}
107+
items::parse_at_date(date, input)
115108
}
116109

117110
#[cfg(test)]

0 commit comments

Comments
 (0)