Skip to content

Commit

Permalink
Merge #400: Improved error handling in Parity serde impl
Browse files Browse the repository at this point in the history
662843e 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 662843e

Tree-SHA512: 2506f06305b01793f64818640931d00564334d96a1e0ef00574faacf1ec8733da13fbf91e57e49fa7c9c06587863fe66145f25afae8d8cabe546dd0ecc48caea
  • Loading branch information
apoelstra committed Feb 9, 2022
2 parents 8bf2927 + 662843e commit 3e815b7
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/key.rs
Expand Up @@ -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<E>(self, v: i32) -> Result<Self::Value, E>
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"))
}
}

Expand Down

0 comments on commit 3e815b7

Please sign in to comment.