Skip to content

Commit

Permalink
Fully move UnexpectedTrailingCharacters error
Browse files Browse the repository at this point in the history
  • Loading branch information
jhpratt committed Aug 27, 2023
1 parent cf16454 commit 1426492
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 38 deletions.
29 changes: 14 additions & 15 deletions tests/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ fn unexpected_trailing_characters() -> Parse {
Time::parse("a", format_description!("")).unwrap_err()
}

fn unexpected_trailing_characters_from_description() -> ParseFromDescription {
match Time::parse("0", format_description!("[end]")) {
Err(Parse::ParseFromDescription(
err @ ParseFromDescription::UnexpectedTrailingCharacters { .. },
)) => err,
_ => panic!("unexpected result"),
}
}
// fn unexpected_trailing_characters() -> ParseFromDescription {
// match Time::parse("0", format_description!("[end]")) {
// Err(Parse::ParseFromDescription(
// err @ ParseFromDescription::UnexpectedTrailingCharacters { .. },
// )) => err,
// _ => panic!("unexpected result"),
// }
// }

fn invalid_format_description() -> InvalidFormatDescription {
format_description::parse("[").unwrap_err()
Expand Down Expand Up @@ -104,10 +104,6 @@ fn display() {
ParseFromDescription::InvalidComponent("a"),
Parse::from(ParseFromDescription::InvalidComponent("a"))
);
assert_display_eq!(
unexpected_trailing_characters_from_description(),
Parse::from(unexpected_trailing_characters_from_description())
);
assert_display_eq!(
component_range(),
Parse::from(TryFromParsed::from(component_range()))
Expand Down Expand Up @@ -151,8 +147,11 @@ fn source() {
Error::from(ParseFromDescription::InvalidComponent("a")),
ParseFromDescription
);
assert_source!(unexpected_trailing_characters(), None);
assert_source!(Error::from(unexpected_trailing_characters()), None);
assert_source!(unexpected_trailing_characters(), ParseFromDescription);
assert_source!(
Error::from(unexpected_trailing_characters()),
ParseFromDescription
);
assert_source!(
Error::from(invalid_format_description()),
InvalidFormatDescription
Expand All @@ -176,6 +175,7 @@ fn conversion() {
assert!(InvalidFormatDescription::try_from(Error::from(invalid_format_description())).is_ok());
assert!(ParseFromDescription::try_from(Error::from(invalid_literal())).is_ok());
assert!(ParseFromDescription::try_from(Parse::from(invalid_literal())).is_ok());
assert!(ParseFromDescription::try_from(unexpected_trailing_characters()).is_ok());
assert!(Parse::try_from(Error::from(unexpected_trailing_characters())).is_ok());
assert!(Parse::try_from(Error::from(invalid_literal())).is_ok());
assert!(Parse::try_from(Error::from(TryFromParsed::InsufficientInformation)).is_ok());
Expand All @@ -192,7 +192,6 @@ fn conversion() {
assert!(IndeterminateOffset::try_from(Error::from(ConversionRange)).is_err());
assert!(InvalidFormatDescription::try_from(Error::from(IndeterminateOffset)).is_err());
assert!(ParseFromDescription::try_from(Error::from(IndeterminateOffset)).is_err());
assert!(ParseFromDescription::try_from(unexpected_trailing_characters()).is_err());
assert!(Parse::try_from(Error::from(IndeterminateOffset)).is_err());
assert!(DifferentVariant::try_from(Error::from(IndeterminateOffset)).is_err());
assert!(InvalidVariant::try_from(Error::from(IndeterminateOffset)).is_err());
Expand Down
52 changes: 39 additions & 13 deletions tests/parsing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,9 @@ fn rfc_3339_err() {

assert!(matches!(
OffsetDateTime::parse("2021-01-02T03:04:05Z ", &Rfc3339),
Err(error::Parse::UnexpectedTrailingCharacters { .. })
Err(error::Parse::ParseFromDescription(
error::ParseFromDescription::UnexpectedTrailingCharacters { .. }
))
));
assert!(matches!(
OffsetDateTime::parse("x", &Rfc3339),
Expand Down Expand Up @@ -571,43 +573,63 @@ fn iso_8601() {
fn iso_8601_error() {
assert!(matches!(
OffsetDateTime::parse("20210102T03:04Z", &Iso8601::DEFAULT),
Err(error::Parse::UnexpectedTrailingCharacters { .. })
Err(error::Parse::ParseFromDescription(
error::ParseFromDescription::UnexpectedTrailingCharacters { .. }
))
));
assert!(matches!(
OffsetDateTime::parse("20210102T03.", &Iso8601::DEFAULT),
Err(error::Parse::UnexpectedTrailingCharacters { .. })
Err(error::Parse::ParseFromDescription(
error::ParseFromDescription::UnexpectedTrailingCharacters { .. }
))
));
assert!(matches!(
OffsetDateTime::parse("2021-0102", &Iso8601::DEFAULT),
Err(error::Parse::UnexpectedTrailingCharacters { .. })
Err(error::Parse::ParseFromDescription(
error::ParseFromDescription::UnexpectedTrailingCharacters { .. }
))
));
assert!(matches!(
OffsetDateTime::parse("2021-01-x", &Iso8601::DEFAULT),
Err(error::Parse::UnexpectedTrailingCharacters { .. })
Err(error::Parse::ParseFromDescription(
error::ParseFromDescription::UnexpectedTrailingCharacters { .. }
))
));
assert!(matches!(
OffsetDateTime::parse("2021-Wx", &Iso8601::DEFAULT),
Err(error::Parse::UnexpectedTrailingCharacters { .. })
Err(error::Parse::ParseFromDescription(
error::ParseFromDescription::UnexpectedTrailingCharacters { .. }
))
));
assert!(matches!(
OffsetDateTime::parse("2021-W012", &Iso8601::DEFAULT),
Err(error::Parse::UnexpectedTrailingCharacters { .. })
Err(error::Parse::ParseFromDescription(
error::ParseFromDescription::UnexpectedTrailingCharacters { .. }
))
));
assert!(matches!(
OffsetDateTime::parse("2021-W01-x", &Iso8601::DEFAULT),
Err(error::Parse::UnexpectedTrailingCharacters { .. })
Err(error::Parse::ParseFromDescription(
error::ParseFromDescription::UnexpectedTrailingCharacters { .. }
))
));
assert!(matches!(
OffsetDateTime::parse("2021-01-02T03:x", &Iso8601::DEFAULT),
Err(error::Parse::UnexpectedTrailingCharacters { .. })
Err(error::Parse::ParseFromDescription(
error::ParseFromDescription::UnexpectedTrailingCharacters { .. }
))
));
assert!(matches!(
OffsetDateTime::parse("2021-01-02T03:04x", &Iso8601::DEFAULT),
Err(error::Parse::UnexpectedTrailingCharacters { .. })
Err(error::Parse::ParseFromDescription(
error::ParseFromDescription::UnexpectedTrailingCharacters { .. }
))
));
assert!(matches!(
OffsetDateTime::parse("2021-01-02T03:04:", &Iso8601::DEFAULT),
Err(error::Parse::UnexpectedTrailingCharacters { .. })
Err(error::Parse::ParseFromDescription(
error::ParseFromDescription::UnexpectedTrailingCharacters { .. }
))
));
assert!(matches!(
OffsetDateTime::parse("01:02", &Iso8601::DEFAULT),
Expand Down Expand Up @@ -693,7 +715,9 @@ fn parse_time_err() -> time::Result<()> {
));
assert!(matches!(
Time::parse(" ", &fd::parse("")?),
Err(error::Parse::UnexpectedTrailingCharacters { .. })
Err(error::Parse::ParseFromDescription(
error::ParseFromDescription::UnexpectedTrailingCharacters { .. }
))
));
assert!(matches!(
Time::parse("a", &fd::parse("[subsecond digits:1]")?),
Expand Down Expand Up @@ -1048,7 +1072,9 @@ fn parse_primitive_date_time_err() -> time::Result<()> {
"2023-07-27 23:30",
&fd::parse("[year]-[month]-[day] [hour]")?
),
Err(error::Parse::UnexpectedTrailingCharacters { .. })
Err(error::Parse::ParseFromDescription(
error::ParseFromDescription::UnexpectedTrailingCharacters { .. }
))
));

Ok(())
Expand Down
2 changes: 1 addition & 1 deletion tests/serde/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ fn time_error() {
);
assert_de_tokens_error::<Readable<Time>>(
&[Token::BorrowedStr("00:00:00.0x")],
"unexpected trailing characters",
"unexpected trailing characters; the end of input was expected",
);
assert_de_tokens_error::<Readable<Time>>(
&[Token::Bool(false)],
Expand Down
10 changes: 8 additions & 2 deletions time/src/error/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ pub enum Error {
ParseFromDescription(ParseFromDescription),
#[cfg(feature = "parsing")]
#[non_exhaustive]
#[deprecated(
since = "0.3.28",
note = "no longer output. moved to the `ParseFromDescription` variant"
)]
UnexpectedTrailingCharacters,
#[cfg(feature = "parsing")]
TryFromParsed(TryFromParsed),
Expand All @@ -76,7 +80,8 @@ impl fmt::Display for Error {
#[cfg(feature = "parsing")]
Self::ParseFromDescription(e) => e.fmt(f),
#[cfg(feature = "parsing")]
Self::UnexpectedTrailingCharacters => f.write_str("unexpected trailing characters"),
#[allow(deprecated)]
Self::UnexpectedTrailingCharacters => bug!("variant should not be used"),
#[cfg(feature = "parsing")]
Self::TryFromParsed(e) => e.fmt(f),
#[cfg(all(any(feature = "formatting", feature = "parsing"), feature = "alloc"))]
Expand All @@ -100,7 +105,8 @@ impl std::error::Error for Error {
#[cfg(feature = "parsing")]
Self::ParseFromDescription(err) => Some(err),
#[cfg(feature = "parsing")]
Self::UnexpectedTrailingCharacters => None,
#[allow(deprecated)]
Self::UnexpectedTrailingCharacters => bug!("variant should not be used"),
#[cfg(feature = "parsing")]
Self::TryFromParsed(err) => Some(err),
#[cfg(all(any(feature = "formatting", feature = "parsing"), feature = "alloc"))]
Expand Down
16 changes: 12 additions & 4 deletions time/src/error/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ pub enum Parse {
ParseFromDescription(ParseFromDescription),
/// The input should have ended, but there were characters remaining.
#[non_exhaustive]
#[deprecated(
since = "0.3.28",
note = "no longer output. moved to the `ParseFromDescription` variant"
)]
UnexpectedTrailingCharacters,
}

Expand All @@ -23,7 +27,8 @@ impl fmt::Display for Parse {
match self {
Self::TryFromParsed(err) => err.fmt(f),
Self::ParseFromDescription(err) => err.fmt(f),
Self::UnexpectedTrailingCharacters => f.write_str("unexpected trailing characters"),
#[allow(deprecated)]
Self::UnexpectedTrailingCharacters => bug!("variant should not be used"),
}
}
}
Expand All @@ -34,7 +39,8 @@ impl std::error::Error for Parse {
match self {
Self::TryFromParsed(err) => Some(err),
Self::ParseFromDescription(err) => Some(err),
Self::UnexpectedTrailingCharacters => None,
#[allow(deprecated)]
Self::UnexpectedTrailingCharacters => bug!("variant should not be used"),
}
}
}
Expand Down Expand Up @@ -78,7 +84,8 @@ impl From<Parse> for crate::Error {
match err {
Parse::TryFromParsed(err) => Self::TryFromParsed(err),
Parse::ParseFromDescription(err) => Self::ParseFromDescription(err),
Parse::UnexpectedTrailingCharacters => Self::UnexpectedTrailingCharacters,
#[allow(deprecated)]
Parse::UnexpectedTrailingCharacters => bug!("variant should not be used"),
}
}
}
Expand All @@ -89,7 +96,8 @@ impl TryFrom<crate::Error> for Parse {
fn try_from(err: crate::Error) -> Result<Self, Self::Error> {
match err {
crate::Error::ParseFromDescription(err) => Ok(Self::ParseFromDescription(err)),
crate::Error::UnexpectedTrailingCharacters => Ok(Self::UnexpectedTrailingCharacters),
#[allow(deprecated)]
crate::Error::UnexpectedTrailingCharacters => bug!("variant should not be used"),
crate::Error::TryFromParsed(err) => Ok(Self::TryFromParsed(err)),
_ => Err(error::DifferentVariant),
}
Expand Down
12 changes: 9 additions & 3 deletions time/src/parsing/parsable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ mod sealed {
if self.parse_into(input, &mut parsed)?.is_empty() {
Ok(parsed)
} else {
Err(error::Parse::UnexpectedTrailingCharacters)
Err(error::Parse::ParseFromDescription(
error::ParseFromDescription::UnexpectedTrailingCharacters,
))
}
}

Expand Down Expand Up @@ -430,7 +432,9 @@ impl sealed::Sealed for Rfc2822 {
};

if !input.is_empty() {
return Err(error::Parse::UnexpectedTrailingCharacters);
return Err(error::Parse::ParseFromDescription(
error::ParseFromDescription::UnexpectedTrailingCharacters,
));
}

let mut nanosecond = 0;
Expand Down Expand Up @@ -662,7 +666,9 @@ impl sealed::Sealed for Rfc3339 {
};

if !input.is_empty() {
return Err(error::Parse::UnexpectedTrailingCharacters);
return Err(error::Parse::ParseFromDescription(
error::ParseFromDescription::UnexpectedTrailingCharacters,
));
}

// The RFC explicitly permits leap seconds. We don't currently support them, so treat it as
Expand Down

0 comments on commit 1426492

Please sign in to comment.