Skip to content

Commit

Permalink
Simplify serde_utils for hex ints
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelsproul authored and paulhauner committed Sep 28, 2020
1 parent df3bba1 commit f1e8fa1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 42 deletions.
26 changes: 10 additions & 16 deletions consensus/serde_utils/src/bytes_4_hex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<u8> = 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)
}
19 changes: 4 additions & 15 deletions consensus/serde_utils/src/u32_hex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,20 @@
//!
//! 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<S>(num: &u32, serializer: S) -> Result<S::Ok, S::Error>
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)
}

pub fn deserialize<'de, D>(deserializer: D) -> Result<u32, D::Error>
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)
}
22 changes: 11 additions & 11 deletions consensus/serde_utils/src/u8_hex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,28 @@
//!
//! 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<S>(byte: &u8, serializer: S) -> Result<S::Ok, S::Error>
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)
}

pub fn deserialize<'de, D>(deserializer: D) -> Result<u8, D::Error>
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])
}

0 comments on commit f1e8fa1

Please sign in to comment.