Skip to content

Commit

Permalink
Return the same error message when deserializing with no_std
Browse files Browse the repository at this point in the history
  • Loading branch information
pitdicker committed Apr 5, 2024
1 parent 0d37925 commit d508734
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions chrono-tz/src/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,22 @@ impl<'de> Deserialize<'de> for Tz {
}

fn visit_str<E: de::Error>(self, value: &str) -> Result<Tz, E> {
value.parse::<Tz>().map_err(|e| E::custom(e))
value.parse::<Tz>().map_err(|_| E::custom(SerdeError(value)))
}
}

deserializer.deserialize_str(Visitor)
}
}

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};
Expand All @@ -47,8 +55,7 @@ mod tests {
fn serde_de_error() {
assert_de_tokens_error::<Tz>(
&[Token::Str("Europe/L")],
"'Europe/L' is not a valid timezone",
"failed to parse timezone: 'Europe/L'",
);
assert_de_tokens_error::<Tz>(&[Token::Str("")], "'' is not a valid timezone");
}
}

0 comments on commit d508734

Please sign in to comment.