From 662843e73b85cbe7e61b72b459a9a7d8c13d580f Mon Sep 17 00:00:00 2001 From: Martin Habovstiak Date: Wed, 9 Feb 2022 20:25:29 +0100 Subject: [PATCH] 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 536dc71..e7a0be0 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")) } }