Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: bytes handles numeric arrays and bytearrays in deser #202

Merged
merged 6 commits into from
Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ proptest-derive = { workspace = true, optional = true }

[dev-dependencies]
serde_json.workspace = true
serde = { workspace = true, features = ["derive"] }

[features]
default = ["std"]
Expand Down
102 changes: 98 additions & 4 deletions crates/primitives/src/bits/serde.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,98 @@
use super::FixedBytes;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use alloc::fmt;
use serde::{
de::{self, Visitor},
Deserialize, Deserializer, Serialize, Serializer,
};

impl<const N: usize> Serialize for FixedBytes<N> {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
let mut buf = hex::Buffer::<N, true>::new();
serializer.serialize_str(buf.format(&self.0))
if serializer.is_human_readable() {
let mut buf = hex::Buffer::<N, true>::new();
serializer.serialize_str(buf.format(&self.0))
} else {
serializer.serialize_bytes(self.as_slice())
}
}
}

impl<'de, const N: usize> Deserialize<'de> for FixedBytes<N> {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
hex::deserialize::<'de, D, [u8; N]>(deserializer).map(Self)
struct FixedVisitor<const N: usize>;

impl<'de, const N: usize> Visitor<'de> for FixedVisitor<N> {
type Value = FixedBytes<N>;

fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
formatter,
"{} bytes, represented as a hex string of length {}, an array of u8, or raw bytes",
N,
N * 2
)
}

fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
where
E: de::Error,
{
<[u8; N]>::try_from(v)
.map(FixedBytes)
prestwich marked this conversation as resolved.
Show resolved Hide resolved
.map_err(de::Error::custom)
}

fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
where
A: de::SeqAccess<'de>,
{
let mut bytes = [0u8; N];

bytes.iter_mut().enumerate().try_for_each(|(i, b)| {
*b = seq.next_element()?.ok_or_else(|| {
de::Error::invalid_length(i, &format!("exactly {} bytes", N).as_str())
})?;
Ok(())
})?;

if let Ok(Some(_)) = seq.next_element::<u8>() {
return Err(de::Error::invalid_length(
N + 1,
&format!("exactly {} bytes", N).as_str(),
))
}

Ok(FixedBytes(bytes))
}

fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: de::Error,
{
let v = hex::decode(v).map_err(|_| {
de::Error::invalid_value(de::Unexpected::Str(v), &"a valid hex string")
})?;
<[u8; N]>::try_from(v.as_slice())
.map_err(|_| {
de::Error::invalid_length(
v.len(),
&format!("exactly {} bytes, as {} hex chars", N, N * 2).as_str(),
)
})
.map(FixedBytes)
}
}
deserializer.deserialize_any(FixedVisitor::<N>)
DaniPopes marked this conversation as resolved.
Show resolved Hide resolved
}
}

#[cfg(test)]
mod tests {
use super::*;
use serde::Deserialize;
#[derive(Debug, Deserialize)]
struct TestCase<const N: usize> {
fixed: FixedBytes<N>,
}

#[test]
fn serde() {
Expand All @@ -25,4 +101,22 @@ mod tests {
assert_eq!(ser, "\"0x000000000123456789abcdef\"");
assert_eq!(serde_json::from_str::<FixedBytes<12>>(&ser).unwrap(), bytes);
}

#[test]
fn serde_num_array() {
let json = serde_json::json! {{"fixed": [0,1,2,3,4]}};

assert_eq!(
serde_json::from_value::<TestCase<5>>(json.clone())
.unwrap()
.fixed,
FixedBytes([0, 1, 2, 3, 4])
);

assert!(format!(
"{}",
serde_json::from_value::<TestCase<4>>(json.clone()).unwrap_err()
)
.contains("invalid length 5, expected exactly 4 bytes"),);
}
}
79 changes: 76 additions & 3 deletions crates/primitives/src/bytes/serde.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,84 @@
use super::Bytes;
use serde::de::{self, Visitor};

use core::result::Result;

use crate::Bytes;
use alloc::vec::Vec;

impl serde::Serialize for Bytes {
#[inline]
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
hex::serialize(self, serializer)
if serializer.is_human_readable() {
hex::serialize(self, serializer)
} else {
serializer.serialize_bytes(self.as_ref())
}
}
}

impl<'de> serde::Deserialize<'de> for Bytes {
#[inline]
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
hex::deserialize::<'de, D, alloc::vec::Vec<u8>>(deserializer).map(Into::into)
struct BytesVisitor;

impl<'de> Visitor<'de> for BytesVisitor {
type Value = Bytes;

fn expecting(&self, formatter: &mut alloc::fmt::Formatter<'_>) -> alloc::fmt::Result {
formatter.write_str("a variable number of bytes, represented as a hex string, an array of u8, or raw bytes")
}

fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
Ok(Bytes::from(v.to_vec()))
}

fn visit_byte_buf<E>(self, v: Vec<u8>) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
Ok(Bytes::from(v))
}

fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
where
A: serde::de::SeqAccess<'de>,
{
let mut bytes = Vec::new();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


while let Some(byte) = seq.next_element()? {
bytes.push(byte);
}

Ok(Bytes::from(bytes))
}

fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: de::Error,
{
hex::decode(v)
.map_err(|_| {
de::Error::invalid_value(de::Unexpected::Str(v), &"a valid hex string")
})
.map(From::from)
Comment on lines +48 to +52
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
hex::decode(v)
.map_err(|_| {
de::Error::invalid_value(de::Unexpected::Str(v), &"a valid hex string")
})
.map(From::from)
v.parse()
.map_err(|_| {
de::Error::invalid_value(de::Unexpected::Str(v), &"a valid hex string")
})

}
}

deserializer.deserialize_any(BytesVisitor)
}
}

#[cfg(test)]
mod tests {
use super::*;
use serde::Deserialize;
#[derive(Debug, Deserialize)]
struct TestCase {
variable: Bytes,
}

#[test]
fn serde() {
Expand All @@ -26,4 +87,16 @@ mod tests {
assert_eq!(ser, "\"0x0123456789abcdef\"");
assert_eq!(serde_json::from_str::<Bytes>(&ser).unwrap(), bytes);
}

#[test]
fn serde_num_array() {
let json = serde_json::json! {{"variable": [0,1,2,3,4]}};

assert_eq!(
serde_json::from_value::<TestCase>(json.clone())
.unwrap()
.variable,
Bytes::from(Vec::from([0, 1, 2, 3, 4]))
);
}
}
2 changes: 2 additions & 0 deletions crates/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ extern crate alloc;

// Used in Serde tests.
#[cfg(test)]
use serde as _;
#[cfg(test)]
use serde_json as _;

pub mod aliases;
Expand Down