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
This commit is contained in:
Martin Habovstiak 2022-02-09 20:25:29 +01:00
parent 8bf29271de
commit 662843e73b
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;
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"))
}
}