diff --git a/chrono-tz/src/serde.rs b/chrono-tz/src/serde.rs index a79a40f..13a64b3 100644 --- a/chrono-tz/src/serde.rs +++ b/chrono-tz/src/serde.rs @@ -23,7 +23,7 @@ impl<'de> Deserialize<'de> for Tz { } fn visit_str(self, value: &str) -> Result { - value.parse::().map_err(|e| E::custom(e)) + value.parse::().map_err(|_| E::custom(SerdeError(value))) } } @@ -31,6 +31,14 @@ impl<'de> Deserialize<'de> for Tz { } } +struct SerdeError<'a>(&'a str); + +impl fmt::Display for SerdeError<'_> { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "failed to parse timezone: '{}'", self.0) + } +} + #[cfg(test)] mod tests { use crate::timezones::Tz::{self, Etc__UTC, Europe__London, UTC}; @@ -47,8 +55,7 @@ mod tests { fn serde_de_error() { assert_de_tokens_error::( &[Token::Str("Europe/L")], - "'Europe/L' is not a valid timezone", + "failed to parse timezone: 'Europe/L'", ); - assert_de_tokens_error::(&[Token::Str("")], "'' is not a valid timezone"); } }