diff --git a/Cargo.toml b/Cargo.toml index 7801efb..32428b7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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"] diff --git a/src/address_hex.rs b/src/address_hex.rs index 1b39b15..70aba46 100644 --- a/src/address_hex.rs +++ b/src/address_hex.rs @@ -1,3 +1,7 @@ +use alloc::{ + format, + string::{String, ToString}, +}; use alloy_primitives::Address; use serde::de::Error; use serde::{Deserializer, Serializer}; @@ -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) } @@ -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}; diff --git a/src/b256_hex.rs b/src/b256_hex.rs index 8a2ddc1..608c9b2 100644 --- a/src/b256_hex.rs +++ b/src/b256_hex.rs @@ -1,3 +1,7 @@ +use alloc::{ + format, + string::{String, ToString}, +}; use alloy_primitives::B256; use serde::de::Error; use serde::{Deserializer, Serializer}; @@ -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) } diff --git a/src/fixed_bytes_hex.rs b/src/fixed_bytes_hex.rs index 438db57..2408e27 100644 --- a/src/fixed_bytes_hex.rs +++ b/src/fixed_bytes_hex.rs @@ -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}; diff --git a/src/hex.rs b/src/hex.rs index 9a2cd65..94a76d3 100644 --- a/src/hex.rs +++ b/src/hex.rs @@ -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>(data: T) -> String { diff --git a/src/hex_vec.rs b/src/hex_vec.rs index 08a0d00..1bf4c90 100644 --- a/src/hex_vec.rs +++ b/src/hex_vec.rs @@ -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(bytes: &[u8], serializer: S) -> Result diff --git a/src/json_str.rs b/src/json_str.rs index d007cf5..f610ca5 100644 --- a/src/json_str.rs +++ b/src/json_str.rs @@ -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 _, diff --git a/src/lib.rs b/src/lib.rs index a2a1f64..f148844 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,8 @@ +#![cfg_attr(not(feature = "std"), no_std)] + +#[macro_use] +extern crate alloc; + mod quoted_int; pub mod address_hex; diff --git a/src/list_of_bytes_lists.rs b/src/list_of_bytes_lists.rs index c7007c3..5a160ea 100644 --- a/src/list_of_bytes_lists.rs +++ b/src/list_of_bytes_lists.rs @@ -5,6 +5,7 @@ //! Quotes can be optional during decoding. use crate::hex; +use alloc::{string::String, vec::Vec}; use serde::ser::SerializeSeq; use serde::{de, Deserializer, Serializer}; @@ -12,7 +13,7 @@ pub struct ListOfBytesListVisitor; impl<'a> serde::de::Visitor<'a> for ListOfBytesListVisitor { type Value = Vec>; - 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") } diff --git a/src/quoted_int.rs b/src/quoted_int.rs index 3a7d9eb..ee2ee97 100644 --- a/src/quoted_int.rs +++ b/src/quoted_int.rs @@ -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) => { @@ -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 { @@ -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") } diff --git a/src/quoted_u64_vec.rs b/src/quoted_u64_vec.rs index f124c98..0380d69 100644 --- a/src/quoted_u64_vec.rs +++ b/src/quoted_u64_vec.rs @@ -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}; @@ -19,7 +20,7 @@ pub struct QuotedIntVecVisitor; impl<'a> serde::de::Visitor<'a> for QuotedIntVecVisitor { type Value = Vec; - 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") } diff --git a/src/u256_dec.rs b/src/u256_dec.rs index 5163dc1..36b80e6 100644 --- a/src/u256_dec.rs +++ b/src/u256_dec.rs @@ -1,3 +1,7 @@ +use alloc::{ + format, + string::{String, ToString}, +}; use alloy_primitives::U256; use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; diff --git a/src/u256_hex_be.rs b/src/u256_hex_be.rs index 3d7bdca..351cd6b 100644 --- a/src/u256_hex_be.rs +++ b/src/u256_hex_be.rs @@ -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(num: &U256, serializer: S) -> Result where diff --git a/src/u32_hex.rs b/src/u32_hex.rs index ab41eab..6f46764 100644 --- a/src/u32_hex.rs +++ b/src/u32_hex.rs @@ -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(num: &u32, serializer: S) -> Result diff --git a/src/u64_hex_be.rs b/src/u64_hex_be.rs index e3364a2..7b0a9fa 100644 --- a/src/u64_hex_be.rs +++ b/src/u64_hex_be.rs @@ -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; @@ -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 { diff --git a/src/u8_hex.rs b/src/u8_hex.rs index c75d2af..df5ff7c 100644 --- a/src/u8_hex.rs +++ b/src/u8_hex.rs @@ -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};