Merge rust-bitcoin/rust-secp256k1#400: Improved error handling in `Parity` serde impl

662843e73b Improved error handling in `Parity` serde impl (Martin Habovstiak)

Pull request description:

  * Fixes error message to be according to the trait documentation
  * Uses `unexpected_value` to provide more information about the error

ACKs for top commit:
  apoelstra:
    ACK 662843e73b

Tree-SHA512: 2506f06305b01793f64818640931d00564334d96a1e0ef00574faacf1ec8733da13fbf91e57e49fa7c9c06587863fe66145f25afae8d8cabe546dd0ecc48caea
This commit is contained in:
Andrew Poelstra 2022-02-09 20:49:40 +00:00
commit 3e815b7428
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
1 changed files with 5 additions and 2 deletions

View File

@ -1292,13 +1292,16 @@ impl<'de> ::serde::Deserialize<'de> for Parity {
type Value = Parity; type Value = Parity;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { 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<E>(self, v: i32) -> Result<Self::Value, E> fn visit_i32<E>(self, v: i32) -> Result<Self::Value, E>
where E: ::serde::de::Error 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"))
} }
} }