diff --git a/packages/std/src/math/int128.rs b/packages/std/src/math/int128.rs index 3f491850f..9f0f35d59 100644 --- a/packages/std/src/math/int128.rs +++ b/packages/std/src/math/int128.rs @@ -5,7 +5,6 @@ use core::ops::{ ShrAssign, Sub, SubAssign, }; use core::str::FromStr; -use serde::{de, ser, Deserialize, Deserializer, Serialize}; use crate::errors::{DivideByZeroError, DivisionError, OverflowError, OverflowOperation, StdError}; use crate::forward_ref::{forward_ref_binop, forward_ref_op_assign}; @@ -15,6 +14,7 @@ use crate::{ }; use super::conversion::{forward_try_from, try_from_int_to_int}; +use super::impl_int_serde; use super::num_consts::NumConsts; /// An implementation of i128 that is using strings for JSON encoding/decoding, @@ -33,6 +33,7 @@ use super::num_consts::NumConsts; #[derive(Copy, Clone, Default, Debug, PartialEq, Eq, PartialOrd, Ord, schemars::JsonSchema)] pub struct Int128(#[schemars(with = "String")] pub(crate) i128); +impl_int_serde!(Int128); forward_ref_partial_eq!(Int128, Int128); impl Int128 { @@ -537,43 +538,6 @@ impl ShlAssign for Int128 { } forward_ref_op_assign!(impl ShlAssign, shl_assign for Int128, u32); -impl Serialize for Int128 { - /// Serializes as an integer string using base 10 - fn serialize(&self, serializer: S) -> Result - where - S: ser::Serializer, - { - serializer.serialize_str(&self.to_string()) - } -} - -impl<'de> Deserialize<'de> for Int128 { - /// Deserialized from an integer string using base 10 - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - deserializer.deserialize_str(Int128Visitor) - } -} - -struct Int128Visitor; - -impl<'de> de::Visitor<'de> for Int128Visitor { - type Value = Int128; - - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - formatter.write_str("string-encoded integer") - } - - fn visit_str(self, v: &str) -> Result - where - E: de::Error, - { - Int128::try_from(v).map_err(|e| E::custom(format_args!("invalid Int128 '{v}' - {e}"))) - } -} - impl core::iter::Sum for Int128 where Self: Add, diff --git a/packages/std/src/math/int256.rs b/packages/std/src/math/int256.rs index b20bbda48..e12b1d031 100644 --- a/packages/std/src/math/int256.rs +++ b/packages/std/src/math/int256.rs @@ -5,7 +5,6 @@ use core::ops::{ ShrAssign, Sub, SubAssign, }; use core::str::FromStr; -use serde::{de, ser, Deserialize, Deserializer, Serialize}; use crate::errors::{DivideByZeroError, DivisionError, OverflowError, OverflowOperation, StdError}; use crate::forward_ref::{forward_ref_binop, forward_ref_op_assign}; @@ -19,6 +18,7 @@ use crate::{ use bnum::types::{I256, U256}; use super::conversion::{grow_be_int, try_from_int_to_int, try_from_uint_to_int}; +use super::impl_int_serde; use super::num_consts::NumConsts; /// An implementation of i256 that is using strings for JSON encoding/decoding, @@ -44,6 +44,7 @@ use super::num_consts::NumConsts; #[derive(Copy, Clone, Default, Debug, PartialEq, Eq, PartialOrd, Ord, schemars::JsonSchema)] pub struct Int256(#[schemars(with = "String")] pub(crate) I256); +impl_int_serde!(Int256); forward_ref_partial_eq!(Int256, Int256); impl Int256 { @@ -612,43 +613,6 @@ impl ShlAssign for Int256 { } forward_ref_op_assign!(impl ShlAssign, shl_assign for Int256, u32); -impl Serialize for Int256 { - /// Serializes as an integer string using base 10 - fn serialize(&self, serializer: S) -> Result - where - S: ser::Serializer, - { - serializer.serialize_str(&self.to_string()) - } -} - -impl<'de> Deserialize<'de> for Int256 { - /// Deserialized from an integer string using base 10 - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - deserializer.deserialize_str(Int256Visitor) - } -} - -struct Int256Visitor; - -impl<'de> de::Visitor<'de> for Int256Visitor { - type Value = Int256; - - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - formatter.write_str("string-encoded integer") - } - - fn visit_str(self, v: &str) -> Result - where - E: de::Error, - { - Int256::try_from(v).map_err(|e| E::custom(format_args!("invalid Int256 '{v}' - {e}"))) - } -} - impl core::iter::Sum for Int256 where Self: Add, diff --git a/packages/std/src/math/int512.rs b/packages/std/src/math/int512.rs index edbfaa2a4..c51351ae7 100644 --- a/packages/std/src/math/int512.rs +++ b/packages/std/src/math/int512.rs @@ -5,7 +5,6 @@ use core::ops::{ ShrAssign, Sub, SubAssign, }; use core::str::FromStr; -use serde::{de, ser, Deserialize, Deserializer, Serialize}; use crate::errors::{DivideByZeroError, DivisionError, OverflowError, OverflowOperation, StdError}; use crate::forward_ref::{forward_ref_binop, forward_ref_op_assign}; @@ -18,6 +17,7 @@ use crate::{ use bnum::types::{I512, U512}; use super::conversion::{grow_be_int, try_from_uint_to_int}; +use super::impl_int_serde; use super::num_consts::NumConsts; /// An implementation of i512 that is using strings for JSON encoding/decoding, @@ -47,6 +47,7 @@ use super::num_consts::NumConsts; #[derive(Copy, Clone, Default, Debug, PartialEq, Eq, PartialOrd, Ord, schemars::JsonSchema)] pub struct Int512(#[schemars(with = "String")] pub(crate) I512); +impl_int_serde!(Int512); forward_ref_partial_eq!(Int512, Int512); impl Int512 { @@ -611,43 +612,6 @@ impl ShlAssign for Int512 { } forward_ref_op_assign!(impl ShlAssign, shl_assign for Int512, u32); -impl Serialize for Int512 { - /// Serializes as an integer string using base 10 - fn serialize(&self, serializer: S) -> Result - where - S: ser::Serializer, - { - serializer.serialize_str(&self.to_string()) - } -} - -impl<'de> Deserialize<'de> for Int512 { - /// Deserialized from an integer string using base 10 - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - deserializer.deserialize_str(Int512Visitor) - } -} - -struct Int512Visitor; - -impl<'de> de::Visitor<'de> for Int512Visitor { - type Value = Int512; - - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - formatter.write_str("string-encoded integer") - } - - fn visit_str(self, v: &str) -> Result - where - E: de::Error, - { - Int512::try_from(v).map_err(|e| E::custom(format!("invalid Int512 '{v}' - {e}"))) - } -} - impl core::iter::Sum for Int512 where Self: Add, diff --git a/packages/std/src/math/int64.rs b/packages/std/src/math/int64.rs index db2a75115..3a3594fe4 100644 --- a/packages/std/src/math/int64.rs +++ b/packages/std/src/math/int64.rs @@ -5,7 +5,6 @@ use core::ops::{ ShrAssign, Sub, SubAssign, }; use core::str::FromStr; -use serde::{de, ser, Deserialize, Deserializer, Serialize}; use crate::errors::{DivideByZeroError, DivisionError, OverflowError, OverflowOperation, StdError}; use crate::forward_ref::{forward_ref_binop, forward_ref_op_assign}; @@ -15,6 +14,7 @@ use crate::{ }; use super::conversion::{forward_try_from, try_from_int_to_int}; +use super::impl_int_serde; use super::num_consts::NumConsts; /// An implementation of i64 that is using strings for JSON encoding/decoding, @@ -33,6 +33,7 @@ use super::num_consts::NumConsts; #[derive(Copy, Clone, Default, Debug, PartialEq, Eq, PartialOrd, Ord, schemars::JsonSchema)] pub struct Int64(#[schemars(with = "String")] pub(crate) i64); +impl_int_serde!(Int64); forward_ref_partial_eq!(Int64, Int64); impl Int64 { @@ -516,43 +517,6 @@ impl ShlAssign for Int64 { } forward_ref_op_assign!(impl ShlAssign, shl_assign for Int64, u32); -impl Serialize for Int64 { - /// Serializes as an integer string using base 10 - fn serialize(&self, serializer: S) -> Result - where - S: ser::Serializer, - { - serializer.serialize_str(&self.to_string()) - } -} - -impl<'de> Deserialize<'de> for Int64 { - /// Deserialized from an integer string using base 10 - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - deserializer.deserialize_str(Int64Visitor) - } -} - -struct Int64Visitor; - -impl<'de> de::Visitor<'de> for Int64Visitor { - type Value = Int64; - - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - formatter.write_str("string-encoded integer") - } - - fn visit_str(self, v: &str) -> Result - where - E: de::Error, - { - Int64::try_from(v).map_err(|e| E::custom(format_args!("invalid Int64 '{v}' - {e}"))) - } -} - impl core::iter::Sum for Int64 where Self: Add, diff --git a/packages/std/src/math/mod.rs b/packages/std/src/math/mod.rs index 68b02525d..c1c1f2682 100644 --- a/packages/std/src/math/mod.rs +++ b/packages/std/src/math/mod.rs @@ -30,6 +30,53 @@ pub use uint256::Uint256; pub use uint512::Uint512; pub use uint64::Uint64; +macro_rules! impl_int_serde { + ($ty:ty) => { + impl ::serde::Serialize for $ty { + /// Serializes as an integer string using base 10 + fn serialize(&self, serializer: S) -> Result + where + S: ::serde::ser::Serializer, + { + serializer.serialize_str(&self.to_string()) + } + } + + impl<'de> ::serde::Deserialize<'de> for $ty { + /// Deserialized from an integer string using base 10 + fn deserialize(deserializer: D) -> Result + where + D: ::serde::de::Deserializer<'de>, + { + struct IntVisitor; + + impl<'de> ::serde::de::Visitor<'de> for IntVisitor { + type Value = $ty; + + fn expecting( + &self, + formatter: &mut ::core::fmt::Formatter, + ) -> ::core::fmt::Result { + formatter.write_str("string-encoded integer") + } + + fn visit_str(self, v: &str) -> Result + where + E: ::serde::de::Error, + { + <_>::try_from(v).map_err(|e| { + E::custom(format_args!("invalid {} '{v}' - {e}", stringify!($t))) + }) + } + } + + deserializer.deserialize_str(IntVisitor) + } + } + }; +} +use impl_int_serde; + #[cfg(test)] mod tests { use super::*; diff --git a/packages/std/src/math/uint128.rs b/packages/std/src/math/uint128.rs index 197e9157d..1511deca7 100644 --- a/packages/std/src/math/uint128.rs +++ b/packages/std/src/math/uint128.rs @@ -6,8 +6,6 @@ use core::ops::{ }; use core::str::FromStr; -use serde::{de, ser, Deserialize, Deserializer, Serialize}; - use crate::errors::{ CheckedMultiplyFractionError, CheckedMultiplyRatioError, DivideByZeroError, OverflowError, OverflowOperation, StdError, @@ -19,6 +17,7 @@ use crate::{ }; use super::conversion::forward_try_from; +use super::impl_int_serde; use super::num_consts::NumConsts; /// A thin wrapper around u128 that is using strings for JSON encoding/decoding, @@ -43,6 +42,7 @@ use super::num_consts::NumConsts; #[derive(Copy, Clone, Default, Debug, PartialEq, Eq, PartialOrd, Ord, schemars::JsonSchema)] pub struct Uint128(#[schemars(with = "String")] pub(crate) u128); +impl_int_serde!(Uint128); forward_ref_partial_eq!(Uint128, Uint128); impl Uint128 { @@ -583,46 +583,6 @@ impl<'a> ShlAssign<&'a u32> for Uint128 { } } -impl Serialize for Uint128 { - /// Serializes as an integer string using base 10 - fn serialize(&self, serializer: S) -> Result - where - S: ser::Serializer, - { - serializer.serialize_str(&self.to_string()) - } -} - -impl<'de> Deserialize<'de> for Uint128 { - /// Deserialized from an integer string using base 10 - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - deserializer.deserialize_str(Uint128Visitor) - } -} - -struct Uint128Visitor; - -impl<'de> de::Visitor<'de> for Uint128Visitor { - type Value = Uint128; - - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - formatter.write_str("string-encoded integer") - } - - fn visit_str(self, v: &str) -> Result - where - E: de::Error, - { - match v.parse::() { - Ok(u) => Ok(Uint128(u)), - Err(e) => Err(E::custom(format_args!("invalid Uint128 '{v}' - {e}"))), - } - } -} - impl core::iter::Sum for Uint128 where Self: Add, diff --git a/packages/std/src/math/uint256.rs b/packages/std/src/math/uint256.rs index 1e64f0ed5..88d310871 100644 --- a/packages/std/src/math/uint256.rs +++ b/packages/std/src/math/uint256.rs @@ -5,7 +5,6 @@ use core::ops::{ ShrAssign, Sub, SubAssign, }; use core::str::FromStr; -use serde::{de, ser, Deserialize, Deserializer, Serialize}; use crate::errors::{ CheckedMultiplyFractionError, CheckedMultiplyRatioError, ConversionOverflowError, @@ -22,6 +21,7 @@ use crate::{ use bnum::types::U256; use super::conversion::{forward_try_from, try_from_int_to_uint}; +use super::impl_int_serde; use super::num_consts::NumConsts; /// An implementation of u256 that is using strings for JSON encoding/decoding, @@ -47,6 +47,7 @@ use super::num_consts::NumConsts; #[derive(Copy, Clone, Default, Debug, PartialEq, Eq, PartialOrd, Ord, schemars::JsonSchema)] pub struct Uint256(#[schemars(with = "String")] pub(crate) U256); +impl_int_serde!(Uint256); forward_ref_partial_eq!(Uint256, Uint256); impl Uint256 { @@ -648,43 +649,6 @@ impl<'a> ShlAssign<&'a u32> for Uint256 { } } -impl Serialize for Uint256 { - /// Serializes as an integer string using base 10 - fn serialize(&self, serializer: S) -> Result - where - S: ser::Serializer, - { - serializer.serialize_str(&self.to_string()) - } -} - -impl<'de> Deserialize<'de> for Uint256 { - /// Deserialized from an integer string using base 10 - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - deserializer.deserialize_str(Uint256Visitor) - } -} - -struct Uint256Visitor; - -impl<'de> de::Visitor<'de> for Uint256Visitor { - type Value = Uint256; - - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - formatter.write_str("string-encoded integer") - } - - fn visit_str(self, v: &str) -> Result - where - E: de::Error, - { - Uint256::try_from(v).map_err(|e| E::custom(format_args!("invalid Uint256 '{v}' - {e}"))) - } -} - impl core::iter::Sum for Uint256 where Self: Add, diff --git a/packages/std/src/math/uint512.rs b/packages/std/src/math/uint512.rs index cd96353c6..0332808df 100644 --- a/packages/std/src/math/uint512.rs +++ b/packages/std/src/math/uint512.rs @@ -5,7 +5,6 @@ use core::ops::{ ShrAssign, Sub, SubAssign, }; use core::str::FromStr; -use serde::{de, ser, Deserialize, Deserializer, Serialize}; use crate::errors::{ ConversionOverflowError, DivideByZeroError, OverflowError, OverflowOperation, StdError, @@ -20,6 +19,7 @@ use crate::{ use bnum::types::U512; use super::conversion::{forward_try_from, try_from_int_to_uint}; +use super::impl_int_serde; use super::num_consts::NumConsts; /// An implementation of u512 that is using strings for JSON encoding/decoding, @@ -49,6 +49,7 @@ use super::num_consts::NumConsts; #[derive(Copy, Clone, Default, Debug, PartialEq, Eq, PartialOrd, Ord, schemars::JsonSchema)] pub struct Uint512(#[schemars(with = "String")] pub(crate) U512); +impl_int_serde!(Uint512); forward_ref_partial_eq!(Uint512, Uint512); impl Uint512 { @@ -623,43 +624,6 @@ impl<'a> ShlAssign<&'a u32> for Uint512 { } } -impl Serialize for Uint512 { - /// Serializes as an integer string using base 10 - fn serialize(&self, serializer: S) -> Result - where - S: ser::Serializer, - { - serializer.serialize_str(&self.to_string()) - } -} - -impl<'de> Deserialize<'de> for Uint512 { - /// Deserialized from an integer string using base 10 - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - deserializer.deserialize_str(Uint512Visitor) - } -} - -struct Uint512Visitor; - -impl<'de> de::Visitor<'de> for Uint512Visitor { - type Value = Uint512; - - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - formatter.write_str("string-encoded integer") - } - - fn visit_str(self, v: &str) -> Result - where - E: de::Error, - { - Uint512::try_from(v).map_err(|e| E::custom(format_args!("invalid Uint512 '{v}' - {e}"))) - } -} - impl core::iter::Sum for Uint512 where Self: Add, diff --git a/packages/std/src/math/uint64.rs b/packages/std/src/math/uint64.rs index 4c138f193..9fecc2495 100644 --- a/packages/std/src/math/uint64.rs +++ b/packages/std/src/math/uint64.rs @@ -4,7 +4,6 @@ use core::ops::{ Add, AddAssign, Div, DivAssign, Mul, MulAssign, Not, Rem, RemAssign, Shl, ShlAssign, Shr, ShrAssign, Sub, SubAssign, }; -use serde::{de, ser, Deserialize, Deserializer, Serialize}; use crate::errors::{ CheckedMultiplyFractionError, CheckedMultiplyRatioError, DivideByZeroError, OverflowError, @@ -17,6 +16,7 @@ use crate::{ }; use super::conversion::forward_try_from; +use super::impl_int_serde; use super::num_consts::NumConsts; /// A thin wrapper around u64 that is using strings for JSON encoding/decoding, @@ -38,6 +38,7 @@ use super::num_consts::NumConsts; #[derive(Copy, Clone, Default, Debug, PartialEq, Eq, PartialOrd, Ord, schemars::JsonSchema)] pub struct Uint64(#[schemars(with = "String")] pub(crate) u64); +impl_int_serde!(Uint64); forward_ref_partial_eq!(Uint64, Uint64); impl Uint64 { @@ -549,46 +550,6 @@ impl<'a> ShlAssign<&'a u32> for Uint64 { } } -impl Serialize for Uint64 { - /// Serializes as an integer string using base 10 - fn serialize(&self, serializer: S) -> Result - where - S: ser::Serializer, - { - serializer.serialize_str(&self.to_string()) - } -} - -impl<'de> Deserialize<'de> for Uint64 { - /// Deserialized from an integer string using base 10 - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - deserializer.deserialize_str(Uint64Visitor) - } -} - -struct Uint64Visitor; - -impl<'de> de::Visitor<'de> for Uint64Visitor { - type Value = Uint64; - - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - formatter.write_str("string-encoded integer") - } - - fn visit_str(self, v: &str) -> Result - where - E: de::Error, - { - match v.parse::() { - Ok(u) => Ok(Uint64(u)), - Err(e) => Err(E::custom(format_args!("invalid Uint64 '{v}' - {e}"))), - } - } -} - impl core::iter::Sum for Uint64 where Self: Add,