Skip to content

Commit

Permalink
fix: Hex serde deserialize (#1396)
Browse files Browse the repository at this point in the history
  • Loading branch information
yangby-cryptape authored Sep 4, 2023
1 parent c944e24 commit b77f29f
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion protocol/src/types/primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ impl<'de> Deserialize<'de> for Hex {
where
D: de::Deserializer<'de>,
{
Ok(Hex(withpfx_lowercase::deserialize(deserializer)?))
String::deserialize(deserializer)
.and_then(|s| Hex::from_str(&s).map_err(serde::de::Error::custom))
}
}

Expand Down Expand Up @@ -534,6 +535,20 @@ mod tests {
);
}

#[test]
fn test_hex_serde_deserialize() {
#[derive(Deserialize)]
struct Params {
key: Hex,
}
let test_params: &str = r#"
key = "0x1234"
"#;
let params: Params = toml::from_str(test_params).unwrap();
let expected = Hex::from_str("0x1234").unwrap();
assert_eq!(params.key, expected);
}

#[test]
fn test_default_values() {
let bytes = Hex::empty();
Expand Down

0 comments on commit b77f29f

Please sign in to comment.