diff --git a/consensus/serde_utils/src/bytes_4_hex.rs b/consensus/serde_utils/src/bytes_4_hex.rs index 8e5ab915534..e057d1a1288 100644 --- a/consensus/serde_utils/src/bytes_4_hex.rs +++ b/consensus/serde_utils/src/bytes_4_hex.rs @@ -2,8 +2,9 @@ //! //! E.g., `[0, 1, 2, 3]` serializes as `"0x00010203"`. +use crate::hex::PrefixedHexVisitor; use serde::de::Error; -use serde::{Deserialize, Deserializer, Serializer}; +use serde::{Deserializer, Serializer}; const BYTES_LEN: usize = 4; @@ -21,24 +22,17 @@ pub fn deserialize<'de, D>(deserializer: D) -> Result<[u8; BYTES_LEN], D::Error> where D: Deserializer<'de>, { - let s: String = Deserialize::deserialize(deserializer)?; - let mut array = [0 as u8; BYTES_LEN]; - - let start = s - .as_str() - .get(2..) - .ok_or_else(|| D::Error::custom("string length too small"))?; - let decoded: Vec = hex::decode(&start).map_err(D::Error::custom)?; + let decoded = deserializer.deserialize_str(PrefixedHexVisitor)?; if decoded.len() != BYTES_LEN { - return Err(D::Error::custom("Fork length too long")); + return Err(D::Error::custom(format!( + "expected {} bytes for array, got {}", + BYTES_LEN, + decoded.len() + ))); } - for (i, item) in array.iter_mut().enumerate() { - if i > decoded.len() { - break; - } - *item = decoded[i]; - } + let mut array = [0; BYTES_LEN]; + array.copy_from_slice(&decoded); Ok(array) } diff --git a/consensus/serde_utils/src/u32_hex.rs b/consensus/serde_utils/src/u32_hex.rs index d39732ac903..c1ab3537b2a 100644 --- a/consensus/serde_utils/src/u32_hex.rs +++ b/consensus/serde_utils/src/u32_hex.rs @@ -2,17 +2,14 @@ //! //! E.g., `0` serializes as `"0x00000000"`. -use serde::de::Error; -use serde::{Deserialize, Deserializer, Serializer}; +use crate::bytes_4_hex; +use serde::{Deserializer, Serializer}; pub fn serialize(num: &u32, serializer: S) -> Result where S: Serializer, { - let mut hex: String = "0x".to_string(); - let bytes = num.to_le_bytes(); - hex.push_str(&hex::encode(&bytes)); - + let hex = format!("0x{}", hex::encode(num.to_le_bytes())); serializer.serialize_str(&hex) } @@ -20,13 +17,5 @@ pub fn deserialize<'de, D>(deserializer: D) -> Result where D: Deserializer<'de>, { - let s: String = Deserialize::deserialize(deserializer)?; - let start = s - .as_str() - .get(2..) - .ok_or_else(|| D::Error::custom("string length too small"))?; - - u32::from_str_radix(&start, 16) - .map_err(D::Error::custom) - .map(u32::from_be) + bytes_4_hex::deserialize(deserializer).map(u32::from_le_bytes) } diff --git a/consensus/serde_utils/src/u8_hex.rs b/consensus/serde_utils/src/u8_hex.rs index 7f8635579f4..8083e1d120b 100644 --- a/consensus/serde_utils/src/u8_hex.rs +++ b/consensus/serde_utils/src/u8_hex.rs @@ -2,16 +2,15 @@ //! //! E.g., `0` serializes as `"0x00"`. +use crate::hex::PrefixedHexVisitor; use serde::de::Error; -use serde::{Deserialize, Deserializer, Serializer}; +use serde::{Deserializer, Serializer}; pub fn serialize(byte: &u8, serializer: S) -> Result where S: Serializer, { - let mut hex: String = "0x".to_string(); - hex.push_str(&hex::encode(&[*byte])); - + let hex = format!("0x{}", hex::encode([*byte])); serializer.serialize_str(&hex) } @@ -19,11 +18,12 @@ pub fn deserialize<'de, D>(deserializer: D) -> Result where D: Deserializer<'de>, { - let s: String = Deserialize::deserialize(deserializer)?; - - let start = match s.as_str().get(2..) { - Some(start) => start, - None => return Err(D::Error::custom("string length too small")), - }; - u8::from_str_radix(&start, 16).map_err(D::Error::custom) + let bytes = deserializer.deserialize_str(PrefixedHexVisitor)?; + if bytes.len() != 1 { + return Err(D::Error::custom(format!( + "expected 1 byte for u8, got {}", + bytes.len() + ))); + } + Ok(bytes[0]) }