Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
12 changes: 8 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@ categories = ["cryptography::cryptocurrencies"]
name = "serde_utils"

[dependencies]
alloy-primitives = { version = "1.0", features = ["serde"] }
serde = { version = "1.0.0", features = ["derive"] }
alloy-primitives = { version = "1.0", default-features = false, features = ["serde"] }
serde = { version = "1.0.0", default-features = false, features = ["derive", "alloc"] }
serde_derive = "1.0.0"
serde_json = "1.0.0"
hex = "0.4.3"
serde_json = { version = "1.0.0", default-features = false, features = ["alloc"] }
hex = { version = "0.4.3", default-features = false, features = ["alloc"] }

[features]
default = ["std"]
std = ["alloy-primitives/std", "serde/std", "serde_json/std", "hex/std"]
8 changes: 6 additions & 2 deletions src/address_hex.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
use alloc::{
format,
string::{String, ToString},
};
use alloy_primitives::Address;
use serde::de::Error;
use serde::{Deserializer, Serializer};
Expand All @@ -9,7 +13,7 @@ where
S: Serializer,
{
let mut hex_string: String = "0x".to_string();
hex_string.push_str(&hex::encode(&address));
hex_string.push_str(&hex::encode(address));

serializer.serialize_str(&hex_string)
}
Expand All @@ -35,7 +39,7 @@ where

#[cfg(test)]
mod test {
use std::str::FromStr;
use core::str::FromStr;

use alloy_primitives::Address;
use serde::{Deserialize, Serialize};
Expand Down
6 changes: 5 additions & 1 deletion src/b256_hex.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
use alloc::{
format,
string::{String, ToString},
};
use alloy_primitives::B256;
use serde::de::Error;
use serde::{Deserializer, Serializer};
Expand All @@ -9,7 +13,7 @@ where
S: Serializer,
{
let mut hex_string: String = "0x".to_string();
hex_string.push_str(&hex::encode(&hash));
hex_string.push_str(&hex::encode(hash));

serializer.serialize_str(&hex_string)
}
Expand Down
4 changes: 4 additions & 0 deletions src/fixed_bytes_hex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
//! E.g., `[0, 1, 2, 3]` serializes as `"0x00010203"`.

use crate::hex::PrefixedHexVisitor;
use alloc::{
format,
string::{String, ToString},
};
use serde::de::Error;
use serde::{Deserializer, Serializer};

Expand Down
7 changes: 6 additions & 1 deletion src/hex.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
//! Provides utilities for parsing 0x-prefixed hex strings.

use alloc::{
format,
string::{String, ToString},
vec::Vec,
};
use core::fmt;
use serde::de::{self, Visitor};
use std::fmt;

/// Encode `data` as a 0x-prefixed hex string.
pub fn encode<T: AsRef<[u8]>>(data: T) -> String {
Expand Down
4 changes: 4 additions & 0 deletions src/hex_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
//! E.g., `vec![0, 1, 2, 3]` serializes as `"0x00010203"`.

use crate::hex::PrefixedHexVisitor;
use alloc::{
string::{String, ToString},
vec::Vec,
};
use serde::{Deserializer, Serializer};

pub fn serialize<S>(bytes: &[u8], serializer: S) -> Result<S::Ok, S::Error>
Expand Down
1 change: 1 addition & 0 deletions src/json_str.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//! Serialize a datatype as a JSON-blob within a single string.
use alloc::string::String;
use serde::{
de::{DeserializeOwned, Error as _},
ser::Error as _,
Expand Down
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
#![cfg_attr(not(feature = "std"), no_std)]

#[macro_use]
extern crate alloc;

mod quoted_int;

pub mod address_hex;
Expand Down
3 changes: 2 additions & 1 deletion src/list_of_bytes_lists.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
//! Quotes can be optional during decoding.

use crate::hex;
use alloc::{string::String, vec::Vec};
use serde::ser::SerializeSeq;
use serde::{de, Deserializer, Serializer};

pub struct ListOfBytesListVisitor;
impl<'a> serde::de::Visitor<'a> for ListOfBytesListVisitor {
type Value = Vec<Vec<u8>>;

fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
fn expecting(&self, formatter: &mut core::fmt::Formatter) -> core::fmt::Result {
write!(formatter, "a list of 0x-prefixed byte lists")
}

Expand Down
9 changes: 5 additions & 4 deletions src/quoted_int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
//!
//! Quotes can be optional during decoding.

use alloc::format;
use alloy_primitives::U256;
use core::convert::TryFrom;
use core::marker::PhantomData;
use serde::{Deserializer, Serializer};
use serde_derive::{Deserialize, Serialize};
use std::convert::TryFrom;
use std::marker::PhantomData;

macro_rules! define_mod {
($int: ty) => {
Expand All @@ -26,7 +27,7 @@ macro_rules! define_mod {
{
type Value = T;

fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
fn expecting(&self, formatter: &mut core::fmt::Formatter) -> core::fmt::Result {
if self.require_quotes {
write!(formatter, "a quoted integer")
} else {
Expand Down Expand Up @@ -169,7 +170,7 @@ pub mod quoted_u256 {
impl<'de> serde::de::Visitor<'de> for U256Visitor {
type Value = U256;

fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
fn expecting(&self, formatter: &mut core::fmt::Formatter) -> core::fmt::Result {
formatter.write_str("a quoted U256 integer")
}

Expand Down
3 changes: 2 additions & 1 deletion src/quoted_u64_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
//!
//! Quotes can be optional during decoding.

use alloc::vec::Vec;
use serde::ser::SerializeSeq;
use serde::{Deserializer, Serializer};
use serde_derive::{Deserialize, Serialize};
Expand All @@ -19,7 +20,7 @@ pub struct QuotedIntVecVisitor;
impl<'a> serde::de::Visitor<'a> for QuotedIntVecVisitor {
type Value = Vec<u64>;

fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
fn expecting(&self, formatter: &mut core::fmt::Formatter) -> core::fmt::Result {
write!(formatter, "a list of quoted or unquoted integers")
}

Expand Down
4 changes: 4 additions & 0 deletions src/u256_dec.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
use alloc::{
format,
string::{String, ToString},
};
use alloy_primitives::U256;
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};

Expand Down
8 changes: 6 additions & 2 deletions src/u256_hex_be.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
use alloc::{
format,
string::{String, ToString},
};
use alloy_primitives::U256;
use core::fmt;
use core::str::FromStr;
use serde::de::Visitor;
use serde::{de, Deserializer, Serialize, Serializer};
use std::fmt;
use std::str::FromStr;

pub fn serialize<S>(num: &U256, serializer: S) -> Result<S::Ok, S::Error>
where
Expand Down
1 change: 1 addition & 0 deletions src/u32_hex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
//! E.g., `0` serializes as `"0x00000000"`.

use crate::bytes_4_hex;
use alloc::format;
use serde::{Deserializer, Serializer};

pub fn serialize<S>(num: &u32, serializer: S) -> Result<S::Ok, S::Error>
Expand Down
5 changes: 3 additions & 2 deletions src/u64_hex_be.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
//!
//! E.g., `0` serializes as `"0x0000000000000000"`.

use alloc::{format, vec::Vec};
use core::fmt;
use serde::de::{self, Error, Visitor};
use serde::{Deserializer, Serializer};
use std::fmt;

const BYTES_LEN: usize = 8;

Expand Down Expand Up @@ -35,7 +36,7 @@ impl<'de> Visitor<'de> for QuantityVisitor {
Ok(vec![0])
} else if stripped.starts_with('0') {
Err(de::Error::custom("cannot have leading zero"))
} else if stripped.len() % 2 != 0 {
} else if !stripped.len().is_multiple_of(2) {
hex::decode(format!("0{}", stripped))
.map_err(|e| de::Error::custom(format!("invalid hex ({:?})", e)))
} else {
Expand Down
1 change: 1 addition & 0 deletions src/u8_hex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
//! E.g., `0` serializes as `"0x00"`.

use crate::hex::PrefixedHexVisitor;
use alloc::format;
use serde::de::Error;
use serde::{Deserializer, Serializer};

Expand Down