Skip to content

Commit 662843e

Browse files
committed
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
1 parent 8bf2927 commit 662843e

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

src/key.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1292,13 +1292,16 @@ impl<'de> ::serde::Deserialize<'de> for Parity {
12921292
type Value = Parity;
12931293

12941294
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
1295-
formatter.write_str("Expecting a 4 byte int i32")
1295+
formatter.write_str("32-bit integer with value 0 or 1")
12961296
}
12971297

12981298
fn visit_i32<E>(self, v: i32) -> Result<Self::Value, E>
12991299
where E: ::serde::de::Error
13001300
{
1301-
Parity::from_i32(v).map_err(E::custom)
1301+
use serde::de::Unexpected;
1302+
1303+
Parity::from_i32(v)
1304+
.map_err(|_| E::invalid_value(Unexpected::Signed(v.into()), &"0 or 1"))
13021305
}
13031306
}
13041307

0 commit comments

Comments
 (0)