mirror of
https://github.com/paradigmxyz/reth.git
synced 2026-01-29 17:18:08 -05:00
chore: extract serde bytes helper (#2247)
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
use crate::serde_helper::hex_bytes;
|
||||
use bytes::Buf;
|
||||
use reth_codecs::Compact;
|
||||
use reth_rlp::{Decodable, DecodeError, Encodable};
|
||||
use serde::{Deserialize, Deserializer, Serialize, Serializer};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::{
|
||||
borrow::Borrow,
|
||||
clone::Clone,
|
||||
@@ -13,10 +14,7 @@ use thiserror::Error;
|
||||
|
||||
/// Wrapper type around Bytes to deserialize/serialize "0x" prefixed ethereum hex strings
|
||||
#[derive(Clone, Default, PartialEq, Eq, Hash, Ord, PartialOrd, Serialize, Deserialize)]
|
||||
pub struct Bytes(
|
||||
#[serde(serialize_with = "serialize_bytes", deserialize_with = "deserialize_bytes")]
|
||||
pub bytes::Bytes,
|
||||
);
|
||||
pub struct Bytes(#[serde(with = "hex_bytes")] pub bytes::Bytes);
|
||||
|
||||
fn bytes_to_hex(b: &Bytes) -> String {
|
||||
hex::encode(b.0.as_ref())
|
||||
@@ -185,28 +183,6 @@ impl FromStr for Bytes {
|
||||
}
|
||||
}
|
||||
|
||||
fn serialize_bytes<S, T>(x: T, s: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
T: AsRef<[u8]>,
|
||||
{
|
||||
s.serialize_str(&format!("0x{}", hex::encode(x.as_ref())))
|
||||
}
|
||||
|
||||
fn deserialize_bytes<'de, D>(d: D) -> Result<bytes::Bytes, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
let value = String::deserialize(d)?;
|
||||
if let Some(value) = value.strip_prefix("0x") {
|
||||
hex::decode(value)
|
||||
} else {
|
||||
hex::decode(&value)
|
||||
}
|
||||
.map(Into::into)
|
||||
.map_err(|e| serde::de::Error::custom(e.to_string()))
|
||||
}
|
||||
|
||||
impl Compact for Bytes {
|
||||
fn to_compact<B>(self, buf: &mut B) -> usize
|
||||
where
|
||||
|
||||
@@ -27,6 +27,37 @@ pub mod u64_hex {
|
||||
}
|
||||
}
|
||||
|
||||
/// serde functions for handling bytes as hex strings, such as [bytes::Bytes]
|
||||
pub mod hex_bytes {
|
||||
use serde::{Deserialize, Deserializer, Serializer};
|
||||
|
||||
/// Serialize a byte vec as a hex string with 0x prefix
|
||||
pub fn serialize<S, T>(x: T, s: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
T: AsRef<[u8]>,
|
||||
{
|
||||
s.serialize_str(&format!("0x{}", hex::encode(x.as_ref())))
|
||||
}
|
||||
|
||||
/// Deserialize a hex string into a byte vec
|
||||
/// Accepts a hex string with optional 0x prefix
|
||||
pub fn deserialize<'de, T, D>(d: D) -> Result<T, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
T: From<Vec<u8>>,
|
||||
{
|
||||
let value = String::deserialize(d)?;
|
||||
if let Some(value) = value.strip_prefix("0x") {
|
||||
hex::decode(value)
|
||||
} else {
|
||||
hex::decode(&value)
|
||||
}
|
||||
.map(Into::into)
|
||||
.map_err(|e| serde::de::Error::custom(e.to_string()))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
Reference in New Issue
Block a user