Skip to content

Commit d6bde13

Browse files
authored
Merge pull request #221 from yuankunzhang/misc-refactors
refactor: misc minor refactors
2 parents c31711d + 43842eb commit d6bde13

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

src/items/epoch.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// For the full copyright and license information, please view the LICENSE
22
// file that was distributed with this source code.
33

4+
//! Parse a timestamp item.
5+
//!
46
//! From the GNU docs:
57
//!
68
//! > If you precede a number with ‘@’, it represents an internal timestamp as
@@ -22,7 +24,7 @@ use winnow::{
2224
ModalResult, Parser,
2325
};
2426

25-
use super::primitive::{dec_uint, s};
27+
use super::primitive::{dec_uint, plus_or_minus, s};
2628

2729
/// Represents a timestamp with nanosecond accuracy.
2830
///
@@ -43,7 +45,7 @@ impl TryFrom<Timestamp> for jiff::Timestamp {
4345
fn try_from(ts: Timestamp) -> Result<Self, Self::Error> {
4446
jiff::Timestamp::new(
4547
ts.second,
46-
i32::try_from(ts.nanosecond).map_err(|_| "nanosecond value exceeds i32::MAX")?,
48+
i32::try_from(ts.nanosecond).map_err(|_| "nanosecond in timestamp exceeds i32::MAX")?,
4749
)
4850
.map_err(|_| "timestamp value is out of valid range")
4951
}
@@ -52,7 +54,7 @@ impl TryFrom<Timestamp> for jiff::Timestamp {
5254
/// Parse a timestamp in the form of `@1234567890` or `@-1234567890.12345` or
5355
/// `@1234567890,12345`.
5456
pub(super) fn parse(input: &mut &str) -> ModalResult<Timestamp> {
55-
(s("@"), opt(s(one_of(['-', '+']))), sec_and_nsec)
57+
(s("@"), opt(plus_or_minus), s(sec_and_nsec))
5658
.verify_map(|(_, sign, (sec, nsec))| {
5759
let sec = i64::try_from(sec).ok()?;
5860
let (second, nanosecond) = match (sign, nsec) {
@@ -74,7 +76,7 @@ pub(super) fn parse(input: &mut &str) -> ModalResult<Timestamp> {
7476
/// (padded with zeros on the right if fewer digits are present). If the second
7577
/// part is omitted, it defaults to 0 nanoseconds.
7678
pub(super) fn sec_and_nsec(input: &mut &str) -> ModalResult<(u64, u32)> {
77-
(s(dec_uint), opt(preceded(one_of(['.', ',']), digit1)))
79+
(dec_uint, opt(preceded(one_of(['.', ',']), digit1)))
7880
.verify_map(|(sec, opt_nsec_str)| match opt_nsec_str {
7981
Some(nsec_str) if nsec_str.len() >= 9 => Some((sec, nsec_str[..9].parse().ok()?)),
8082
Some(nsec_str) => {

src/items/primitive.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,14 @@ where
122122
s(':').void().parse_next(input)
123123
}
124124

125+
/// Parse a plus or minus character optionally preceeded by whitespace.
126+
pub(super) fn plus_or_minus<'a, E>(input: &mut &'a str) -> winnow::Result<char, E>
127+
where
128+
E: ParserError<&'a str>,
129+
{
130+
s(alt(('+', '-'))).parse_next(input)
131+
}
132+
125133
/// Create a context error with a reason.
126134
pub(super) fn ctx_err(reason: &'static str) -> ContextError {
127135
let mut err = ContextError::new();

src/items/time.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ pub(super) fn minute(input: &mut &str) -> ModalResult<u8> {
166166
/// Parse a number of seconds in `0..60` and an optional number of nanoseconds
167167
/// (default to 0 if not set).
168168
fn second(input: &mut &str) -> ModalResult<(u8, u32)> {
169-
sec_and_nsec
169+
s(sec_and_nsec)
170170
.verify_map(|(s, ns)| if s < 60 { Some((s as u8, ns)) } else { None })
171171
.parse_next(input)
172172
}

0 commit comments

Comments
 (0)