Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/items/primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use winnow::{
ascii::{digit1, multispace0},
combinator::{alt, delimited, not, opt, peek, preceded, repeat, separated},
error::ParserError,
error::{ContextError, ParserError, StrContext, StrContextValue},
stream::AsChar,
token::{none_of, one_of, take_while},
Parser,
Expand Down Expand Up @@ -130,3 +130,10 @@ where
.verify_map(|s: &str| s.replace(",", ".").parse().ok())
.parse_next(input)
}

/// Create a context error with a reason.
pub(super) fn ctx_err(reason: &'static str) -> ContextError {
let mut err = ContextError::new();
err.push(StrContext::Expected(StrContextValue::Description(reason)));
err
}
20 changes: 5 additions & 15 deletions src/items/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ use std::fmt::Display;
use chrono::FixedOffset;
use winnow::{
combinator::{alt, opt, peek, preceded},
error::{ContextError, ErrMode, StrContext, StrContextValue},
error::{ContextError, ErrMode},
seq,
stream::AsChar,
token::take_while,
Expand All @@ -52,7 +52,7 @@ use winnow::{
use crate::ParseDateTimeError;

use super::{
primitive::{dec_uint, float, s},
primitive::{ctx_err, dec_uint, float, s},
relative,
};

Expand Down Expand Up @@ -191,11 +191,9 @@ fn am_pm_time(input: &mut &str) -> ModalResult<Time> {
.parse_next(input)?;

if h == 0 {
let mut ctx_err = ContextError::new();
ctx_err.push(StrContext::Expected(StrContextValue::Description(
return Err(ErrMode::Cut(ctx_err(
"hour must be greater than 0 when meridiem is specified",
)));
return Err(ErrMode::Cut(ctx_err));
}

let mut h = h % 12;
Expand Down Expand Up @@ -261,19 +259,11 @@ fn timezone_num(input: &mut &str) -> ModalResult<Offset> {
.parse_next(input)
.and_then(|(negative, (hours, minutes))| {
if !(0..=12).contains(&hours) {
let mut ctx_err = ContextError::new();
ctx_err.push(StrContext::Expected(StrContextValue::Description(
"timezone hour between 0 and 12",
)));
return Err(ErrMode::Cut(ctx_err));
return Err(ErrMode::Cut(ctx_err("timezone hour between 0 and 12")));
}

if !(0..=60).contains(&minutes) {
let mut ctx_err = ContextError::new();
ctx_err.push(StrContext::Expected(StrContextValue::Description(
"timezone minute between 0 and 60",
)));
return Err(ErrMode::Cut(ctx_err));
return Err(ErrMode::Cut(ctx_err("timezone minute between 0 and 60")));
}

Ok(Offset {
Expand Down
Loading