diff --git a/tests/error.rs b/tests/error.rs index b57e0b70a..3d22aa09c 100644 --- a/tests/error.rs +++ b/tests/error.rs @@ -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() @@ -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())) @@ -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 @@ -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()); @@ -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()); diff --git a/tests/parsing.rs b/tests/parsing.rs index 98840b7f9..adb573175 100644 --- a/tests/parsing.rs +++ b/tests/parsing.rs @@ -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), @@ -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), @@ -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]")?), @@ -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(()) diff --git a/tests/serde/mod.rs b/tests/serde/mod.rs index f27a0e77d..dc2e1ca75 100644 --- a/tests/serde/mod.rs +++ b/tests/serde/mod.rs @@ -73,7 +73,7 @@ fn time_error() { ); assert_de_tokens_error::>( &[Token::BorrowedStr("00:00:00.0x")], - "unexpected trailing characters", + "unexpected trailing characters; the end of input was expected", ); assert_de_tokens_error::>( &[Token::Bool(false)], diff --git a/time/src/error/mod.rs b/time/src/error/mod.rs index 346b89f74..5f7c02a39 100644 --- a/time/src/error/mod.rs +++ b/time/src/error/mod.rs @@ -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), @@ -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"))] @@ -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"))] diff --git a/time/src/error/parse.rs b/time/src/error/parse.rs index b90ac74e7..1f30ea1f4 100644 --- a/time/src/error/parse.rs +++ b/time/src/error/parse.rs @@ -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, } @@ -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"), } } } @@ -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"), } } } @@ -78,7 +84,8 @@ impl From 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"), } } } @@ -89,7 +96,8 @@ impl TryFrom for Parse { fn try_from(err: crate::Error) -> Result { 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), } diff --git a/time/src/parsing/parsable.rs b/time/src/parsing/parsable.rs index aacaaa045..b153e850e 100644 --- a/time/src/parsing/parsable.rs +++ b/time/src/parsing/parsable.rs @@ -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, + )) } } @@ -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; @@ -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