From 662843e73b85cbe7e61b72b459a9a7d8c13d580f Mon Sep 17 00:00:00 2001 From: Martin Habovstiak Date: Wed, 9 Feb 2022 20:25:29 +0100 Subject: [PATCH 1/2] Improved error handling in `Parity` serde impl * Fixes error message to be according to the trait documentation * Uses `unexpected_value` to provide more information about the error --- src/key.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/key.rs b/src/key.rs index 536dc71e1..e7a0be0b1 100644 --- a/src/key.rs +++ b/src/key.rs @@ -1292,13 +1292,16 @@ impl<'de> ::serde::Deserialize<'de> for Parity { type Value = Parity; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - formatter.write_str("Expecting a 4 byte int i32") + formatter.write_str("32-bit integer with value 0 or 1") } fn visit_i32(self, v: i32) -> Result where E: ::serde::de::Error { - Parity::from_i32(v).map_err(E::custom) + use serde::de::Unexpected; + + Parity::from_i32(v) + .map_err(|_| E::invalid_value(Unexpected::Signed(v.into()), &"0 or 1")) } } From e6cb588a23c13774df02677f18faab7bfd3a35ad Mon Sep 17 00:00:00 2001 From: Martin Habovstiak Date: Wed, 9 Feb 2022 20:35:06 +0100 Subject: [PATCH 2/2] Breaking: changed `Parity` serialization to `u8` Serializing the value as `u8` is more compact but this is a breaking change. `Visitor` was renamed to avoid hungarian notation and maybe allow other integers in the future. --- src/key.rs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/key.rs b/src/key.rs index e7a0be0b1..4046c056c 100644 --- a/src/key.rs +++ b/src/key.rs @@ -1271,41 +1271,41 @@ impl BitXor for Parity { } } -/// The parity is serialized as `i32` - `0` for even, `1` for odd. +/// The parity is serialized as `u8` - `0` for even, `1` for odd. #[cfg(feature = "serde")] #[cfg_attr(docsrs, doc(cfg(feature = "serde")))] impl ::serde::Serialize for Parity { fn serialize(&self, s: S) -> Result { - s.serialize_i32(self.to_i32()) + s.serialize_u8(self.to_u8()) } } -/// The parity is deserialized as `i32` - `0` for even, `1` for odd. +/// The parity is deserialized as `u8` - `0` for even, `1` for odd. #[cfg(feature = "serde")] #[cfg_attr(docsrs, doc(cfg(feature = "serde")))] impl<'de> ::serde::Deserialize<'de> for Parity { fn deserialize>(d: D) -> Result { - struct I32Visitor; + struct Visitor; - impl<'de> ::serde::de::Visitor<'de> for I32Visitor + impl<'de> ::serde::de::Visitor<'de> for Visitor { type Value = Parity; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - formatter.write_str("32-bit integer with value 0 or 1") + formatter.write_str("8-bit integer (byte) with value 0 or 1") } - fn visit_i32(self, v: i32) -> Result + fn visit_u8(self, v: u8) -> Result where E: ::serde::de::Error { use serde::de::Unexpected; - Parity::from_i32(v) - .map_err(|_| E::invalid_value(Unexpected::Signed(v.into()), &"0 or 1")) + Parity::from_u8(v) + .map_err(|_| E::invalid_value(Unexpected::Unsigned(v.into()), &"0 or 1")) } } - d.deserialize_i32(I32Visitor) + d.deserialize_u8(Visitor) } }