From 06108648cf4b45a5460aa80dad856663cf08d260 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Tue, 24 Sep 2024 17:32:19 +0200 Subject: [PATCH] feat(serde): remove deprecated `num` module (#1350) --- crates/rpc-types-eth/src/fee.rs | 2 +- crates/serde/src/lib.rs | 5 - crates/serde/src/num.rs | 307 -------------------------------- crates/serde/src/optional.rs | 4 +- crates/serde/src/quantity.rs | 84 ++++----- crates/serde/src/storage.rs | 9 +- 6 files changed, 50 insertions(+), 361 deletions(-) delete mode 100644 crates/serde/src/num.rs diff --git a/crates/rpc-types-eth/src/fee.rs b/crates/rpc-types-eth/src/fee.rs index d6222da37f6..b045542f1dc 100644 --- a/crates/rpc-types-eth/src/fee.rs +++ b/crates/rpc-types-eth/src/fee.rs @@ -68,7 +68,7 @@ pub struct FeeHistory { serde( default, skip_serializing_if = "Option::is_none", - with = "alloy_serde::u128_vec_vec_opt_via_ruint" + with = "alloy_serde::quantity::u128_vec_vec_opt" ) )] pub reward: Option>>, diff --git a/crates/serde/src/lib.rs b/crates/serde/src/lib.rs index 633216fc8b8..603300801fa 100644 --- a/crates/serde/src/lib.rs +++ b/crates/serde/src/lib.rs @@ -21,11 +21,6 @@ pub use self::bool::*; mod optional; pub use self::optional::*; -#[cfg_attr(not(test), deprecated = "use `quantity::{self, opt, vec}` instead")] -pub mod num; -#[allow(deprecated)] -pub use num::*; - pub mod quantity; /// Storage related helpers. diff --git a/crates/serde/src/num.rs b/crates/serde/src/num.rs deleted file mode 100644 index e00139e7cf3..00000000000 --- a/crates/serde/src/num.rs +++ /dev/null @@ -1,307 +0,0 @@ -//! Numeric serde helpers. - -/// serde functions for handling `u8` via [U8](alloy_primitives::U8) -pub mod u8_via_ruint { - use alloy_primitives::U8; - use serde::{Deserialize, Deserializer, Serialize, Serializer}; - - /// Deserializes an `u8` from [U8] accepting a hex quantity string with optional 0x prefix - pub fn deserialize<'de, D>(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - U8::deserialize(deserializer).map(|val| val.to()) - } - - /// Serializes u64 as hex string - pub fn serialize(value: &u8, s: S) -> Result { - U8::from(*value).serialize(s) - } -} - -/// serde functions for handling `Option` via [U8](alloy_primitives::U8) -pub mod u8_opt_via_ruint { - use alloy_primitives::U8; - use serde::{Deserialize, Deserializer, Serializer}; - - /// Serializes u64 as hex string - pub fn serialize(value: &Option, s: S) -> Result { - match value { - Some(val) => s.serialize_some(&U8::from(*val)), - None => s.serialize_none(), - } - } - - /// Deserializes an `Option` from [U8] accepting a hex quantity string with optional 0x prefix - pub fn deserialize<'de, D>(deserializer: D) -> Result, D::Error> - where - D: Deserializer<'de>, - { - Ok(U8::deserialize(deserializer).map_or(None, |v| Some(u8::from_be_bytes(v.to_be_bytes())))) - } -} - -/// serde functions for handling `u64` via [U64](alloy_primitives::U64) -pub mod u64_via_ruint { - use alloy_primitives::U64; - use serde::{Deserialize, Deserializer, Serialize, Serializer}; - - /// Deserializes an `u64` from [U64] accepting a hex quantity string with optional 0x prefix - pub fn deserialize<'de, D>(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - U64::deserialize(deserializer).map(|val| val.to()) - } - - /// Serializes u64 as hex string - pub fn serialize(value: &u64, s: S) -> Result { - U64::from(*value).serialize(s) - } -} - -/// serde functions for handling `Option` via [U64](alloy_primitives::U64) -pub mod u64_opt_via_ruint { - use alloy_primitives::U64; - use serde::{Deserialize, Deserializer, Serializer}; - - /// Serializes u64 as hex string - pub fn serialize(value: &Option, s: S) -> Result { - match value { - Some(val) => s.serialize_some(&U64::from(*val)), - None => s.serialize_none(), - } - } - - /// Deserializes an `Option` from [U64] accepting a hex quantity string with optional 0x prefix - pub fn deserialize<'de, D>(deserializer: D) -> Result, D::Error> - where - D: Deserializer<'de>, - { - Ok(U64::deserialize(deserializer) - .map_or(None, |v| Some(u64::from_be_bytes(v.to_be_bytes())))) - } -} - -/// serde functions for handling primitive `u128` via [U128](alloy_primitives::U128) -pub mod u128_via_ruint { - use alloy_primitives::U128; - use serde::{Deserialize, Deserializer, Serialize, Serializer}; - - /// Deserializes an `u128` accepting a hex quantity string with optional 0x prefix or - /// a number - pub fn deserialize<'de, D>(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - U128::deserialize(deserializer).map(|val| val.to()) - } - - /// Serializes u128 as hex string - pub fn serialize(value: &u128, s: S) -> Result { - U128::from(*value).serialize(s) - } -} - -/// serde functions for handling primitive optional `u128` via [U128](alloy_primitives::U128) -pub mod u128_opt_via_ruint { - use alloy_primitives::U128; - use serde::{Deserialize, Deserializer, Serializer}; - - /// Deserializes an `Option` accepting a hex quantity string with optional 0x prefix or - /// a number - pub fn deserialize<'de, D>(deserializer: D) -> Result, D::Error> - where - D: Deserializer<'de>, - { - Option::::deserialize(deserializer)? - .map_or_else(|| Ok(None), |val| Ok(Some(val.to()))) - } - - /// Serializes `Option` as hex string - pub fn serialize(value: &Option, s: S) -> Result { - match value { - Some(val) => s.serialize_some(&U128::from(*val)), - None => s.serialize_none(), - } - } -} - -/// serde functions for handling `Vec` via [U128](alloy_primitives::U128) -pub mod u128_vec_via_ruint { - #[cfg(not(feature = "std"))] - use alloc::vec::Vec; - use alloy_primitives::U128; - use serde::{Deserialize, Deserializer, Serialize, Serializer}; - - /// Deserializes an `u128` accepting a hex quantity string with optional 0x prefix or - /// a number - pub fn deserialize<'de, D>(deserializer: D) -> Result, D::Error> - where - D: Deserializer<'de>, - { - let vec = Vec::::deserialize(deserializer)?; - Ok(vec.into_iter().map(|val| val.to()).collect()) - } - - /// Serializes u128 as hex string - pub fn serialize(value: &[u128], s: S) -> Result { - let vec = value.iter().map(|val| U128::from(*val)).collect::>(); - vec.serialize(s) - } -} - -/// serde functions for handling `Vec>` via [U128](alloy_primitives::U128) -pub mod u128_vec_vec_opt_via_ruint { - use alloy_primitives::U128; - use serde::{Deserialize, Deserializer, Serializer}; - - #[cfg(not(feature = "std"))] - use alloc::vec::Vec; - - /// Deserializes an `u128` accepting a hex quantity string with optional 0x prefix or - /// a number - pub fn deserialize<'de, D>(deserializer: D) -> Result>>, D::Error> - where - D: Deserializer<'de>, - { - Option::>>::deserialize(deserializer)?.map_or_else( - || Ok(None), - |vec| { - Ok(Some( - vec.into_iter().map(|v| v.into_iter().map(|val| val.to()).collect()).collect(), - )) - }, - ) - } - - /// Serializes u128 as hex string - pub fn serialize( - value: &Option>>, - s: S, - ) -> Result { - match value { - Some(vec) => { - let vec = vec - .iter() - .map(|v| v.iter().map(|val| U128::from(*val)).collect::>()) - .collect::>(); - s.serialize_some(&vec) - } - None => s.serialize_none(), - } - } -} - -#[cfg(test)] -mod tests { - use super::*; - use serde::{Deserialize, Serialize}; - - #[cfg(not(feature = "std"))] - use alloc::{string::ToString, vec, vec::Vec}; - - #[test] - fn test_hex_u64() { - #[derive(Debug, PartialEq, Eq, Serialize, Deserialize)] - struct Value { - #[serde(with = "u64_via_ruint")] - inner: u64, - } - - let val = Value { inner: 1000 }; - let s = serde_json::to_string(&val).unwrap(); - assert_eq!(s, "{\"inner\":\"0x3e8\"}"); - - let deserialized: Value = serde_json::from_str(&s).unwrap(); - assert_eq!(val, deserialized); - } - - #[test] - fn test_u128_via_ruint() { - #[derive(Debug, PartialEq, Eq, Serialize, Deserialize)] - struct Value { - #[serde(with = "u128_via_ruint")] - inner: u128, - } - - let val = Value { inner: 1000 }; - let s = serde_json::to_string(&val).unwrap(); - assert_eq!(s, "{\"inner\":\"0x3e8\"}"); - - let deserialized: Value = serde_json::from_str(&s).unwrap(); - assert_eq!(val, deserialized); - - let s = "{\"inner\":\"1000\"}".to_string(); - let deserialized: Value = serde_json::from_str(&s).unwrap(); - - assert_eq!(val, deserialized); - } - - #[test] - fn test_u128_opt_via_ruint() { - #[derive(Debug, PartialEq, Eq, Serialize, Deserialize)] - struct Value { - #[serde(with = "u128_opt_via_ruint")] - inner: Option, - } - - let val = Value { inner: Some(1000) }; - let s = serde_json::to_string(&val).unwrap(); - assert_eq!(s, "{\"inner\":\"0x3e8\"}"); - - let deserialized: Value = serde_json::from_str(&s).unwrap(); - assert_eq!(val, deserialized); - - let s = "{\"inner\":\"1000\"}".to_string(); - let deserialized: Value = serde_json::from_str(&s).unwrap(); - - assert_eq!(val, deserialized); - - let val = Value { inner: None }; - let s = serde_json::to_string(&val).unwrap(); - assert_eq!(s, "{\"inner\":null}"); - - let deserialized: Value = serde_json::from_str(&s).unwrap(); - assert_eq!(val, deserialized); - } - - #[test] - fn test_u128_vec_via_ruint() { - #[derive(Debug, PartialEq, Eq, Serialize, Deserialize)] - struct Value { - #[serde(with = "u128_vec_via_ruint")] - inner: Vec, - } - - let val = Value { inner: vec![1000, 2000] }; - let s = serde_json::to_string(&val).unwrap(); - assert_eq!(s, "{\"inner\":[\"0x3e8\",\"0x7d0\"]}"); - - let deserialized: Value = serde_json::from_str(&s).unwrap(); - assert_eq!(val, deserialized); - } - - #[test] - fn test_u128_vec_vec_opt_via_ruint() { - #[derive(Debug, PartialEq, Eq, Serialize, Deserialize)] - struct Value { - #[serde(with = "u128_vec_vec_opt_via_ruint")] - inner: Option>>, - } - - let val = Value { inner: Some(vec![vec![1000, 2000], vec![3000, 4000]]) }; - let s = serde_json::to_string(&val).unwrap(); - assert_eq!(s, "{\"inner\":[[\"0x3e8\",\"0x7d0\"],[\"0xbb8\",\"0xfa0\"]]}"); - - let deserialized: Value = serde_json::from_str(&s).unwrap(); - assert_eq!(val, deserialized); - - let val = Value { inner: None }; - let s = serde_json::to_string(&val).unwrap(); - assert_eq!(s, "{\"inner\":null}"); - - let deserialized: Value = serde_json::from_str(&s).unwrap(); - assert_eq!(val, deserialized); - } -} diff --git a/crates/serde/src/optional.rs b/crates/serde/src/optional.rs index 5ef3288461c..87362352022 100644 --- a/crates/serde/src/optional.rs +++ b/crates/serde/src/optional.rs @@ -1,4 +1,5 @@ //! Serde functions for encoding optional values. + use serde::{Deserialize, Deserializer}; /// For use with serde's `deserialize_with` on a sequence that must be @@ -8,6 +9,5 @@ where T: Deserialize<'de> + Default, D: Deserializer<'de>, { - let s: Option = Deserialize::deserialize(deserializer)?; - Ok(s.unwrap_or_default()) + Option::::deserialize(deserializer).map(Option::unwrap_or_default) } diff --git a/crates/serde/src/quantity.rs b/crates/serde/src/quantity.rs index 81da909a12f..12096750395 100644 --- a/crates/serde/src/quantity.rs +++ b/crates/serde/src/quantity.rs @@ -89,6 +89,48 @@ pub mod vec { } } +/// serde functions for handling `Vec>` via [U128](alloy_primitives::U128) +pub mod u128_vec_vec_opt { + use alloy_primitives::U128; + use serde::{Deserialize, Deserializer, Serializer}; + + #[cfg(not(feature = "std"))] + use alloc::vec::Vec; + + /// Deserializes an `u128` accepting a hex quantity string with optional 0x prefix or + /// a number + pub fn deserialize<'de, D>(deserializer: D) -> Result>>, D::Error> + where + D: Deserializer<'de>, + { + Option::>>::deserialize(deserializer)?.map_or_else( + || Ok(None), + |vec| { + Ok(Some( + vec.into_iter().map(|v| v.into_iter().map(|val| val.to()).collect()).collect(), + )) + }, + ) + } + + /// Serializes u128 as hex string + pub fn serialize( + value: &Option>>, + s: S, + ) -> Result { + match value { + Some(vec) => { + let vec = vec + .iter() + .map(|v| v.iter().map(|val| U128::from(*val)).collect::>()) + .collect::>(); + s.serialize_some(&vec) + } + None => s.serialize_none(), + } + } +} + /// Private implementation details of the [`quantity`](self) module. #[allow(unnameable_types)] mod private { @@ -133,48 +175,6 @@ mod private { } } -/// serde functions for handling `Vec>` via [U128](alloy_primitives::U128) -pub mod u128_vec_vec_opt { - use alloy_primitives::U128; - use serde::{Deserialize, Deserializer, Serializer}; - - #[cfg(not(feature = "std"))] - use alloc::vec::Vec; - - /// Deserializes an `u128` accepting a hex quantity string with optional 0x prefix or - /// a number - pub fn deserialize<'de, D>(deserializer: D) -> Result>>, D::Error> - where - D: Deserializer<'de>, - { - Option::>>::deserialize(deserializer)?.map_or_else( - || Ok(None), - |vec| { - Ok(Some( - vec.into_iter().map(|v| v.into_iter().map(|val| val.to()).collect()).collect(), - )) - }, - ) - } - - /// Serializes u128 as hex string - pub fn serialize( - value: &Option>>, - s: S, - ) -> Result { - match value { - Some(vec) => { - let vec = vec - .iter() - .map(|v| v.iter().map(|val| U128::from(*val)).collect::>()) - .collect::>(); - s.serialize_some(&vec) - } - None => s.serialize_none(), - } - } -} - #[cfg(test)] mod tests { use serde::{Deserialize, Serialize}; diff --git a/crates/serde/src/storage.rs b/crates/serde/src/storage.rs index 1ab46a6c693..1d32a6becc1 100644 --- a/crates/serde/src/storage.rs +++ b/crates/serde/src/storage.rs @@ -1,7 +1,8 @@ -#[cfg(not(feature = "std"))] -use alloc::string::{String, ToString}; -use alloc::{collections::BTreeMap, fmt::Write}; - +use alloc::{ + collections::BTreeMap, + fmt::Write, + string::{String, ToString}, +}; use alloy_primitives::{Bytes, B256, U256}; use serde::{Deserialize, Deserializer, Serialize};