From da556c94be2d6fb067c9e80e51f1e0c2872c4051 Mon Sep 17 00:00:00 2001 From: arvidn Date: Sat, 19 Nov 2022 16:11:42 +0100 Subject: [PATCH] add support for remaining primitive types to json serialization/deserialization trait --- chia-protocol/src/bytes.rs | 6 +++++ chia-protocol/src/from_json_dict.rs | 39 ++++++++++++++--------------- chia-protocol/src/to_json_dict.rs | 33 +++++++++++++----------- 3 files changed, 44 insertions(+), 34 deletions(-) diff --git a/chia-protocol/src/bytes.rs b/chia-protocol/src/bytes.rs index 7ad941942..d05339224 100644 --- a/chia-protocol/src/bytes.rs +++ b/chia-protocol/src/bytes.rs @@ -50,6 +50,12 @@ impl From> for Bytes { } } +impl AsRef<[u8]> for Bytes { + fn as_ref(&self) -> &[u8] { + &self.0 + } +} + impl fmt::Display for Bytes { fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { formatter.write_str(&hex::encode(&self.0)) diff --git a/chia-protocol/src/from_json_dict.rs b/chia-protocol/src/from_json_dict.rs index 8578540a0..5f711490d 100644 --- a/chia-protocol/src/from_json_dict.rs +++ b/chia-protocol/src/from_json_dict.rs @@ -69,29 +69,28 @@ where } } -impl FromJsonDict for bool { - fn from_json_dict(o: &PyAny) -> PyResult { - o.extract() - } -} - -impl FromJsonDict for u32 { - fn from_json_dict(o: &PyAny) -> PyResult { - o.extract() - } -} - -impl FromJsonDict for u64 { - fn from_json_dict(o: &PyAny) -> PyResult { - o.extract() +macro_rules! from_json_primitive { + ($t:ty) => { + impl FromJsonDict for $t { + fn from_json_dict(o: &PyAny) -> PyResult { + o.extract() + } + } } } -impl FromJsonDict for String { - fn from_json_dict(o: &PyAny) -> PyResult { - o.extract() - } -} +from_json_primitive!(bool); +from_json_primitive!(u8); +from_json_primitive!(i8); +from_json_primitive!(u16); +from_json_primitive!(i16); +from_json_primitive!(u32); +from_json_primitive!(i32); +from_json_primitive!(u64); +from_json_primitive!(i64); +from_json_primitive!(u128); +from_json_primitive!(i128); +from_json_primitive!(String); impl FromJsonDict for Vec where diff --git a/chia-protocol/src/to_json_dict.rs b/chia-protocol/src/to_json_dict.rs index cfa829c85..f54c82aa1 100644 --- a/chia-protocol/src/to_json_dict.rs +++ b/chia-protocol/src/to_json_dict.rs @@ -6,23 +6,28 @@ pub trait ToJsonDict { fn to_json_dict(&self, py: Python) -> PyResult; } -impl ToJsonDict for bool { - fn to_json_dict(&self, py: Python) -> PyResult { - Ok(self.to_object(py)) - } -} - -impl ToJsonDict for u32 { - fn to_json_dict(&self, py: Python) -> PyResult { - Ok(self.to_object(py)) +macro_rules! from_json_primitive { + ($t:ty) => { + impl ToJsonDict for $t { + fn to_json_dict(&self, py: Python) -> PyResult { + Ok(self.to_object(py)) + } + } } } -impl ToJsonDict for u64 { - fn to_json_dict(&self, py: Python) -> PyResult { - Ok(self.to_object(py)) - } -} +from_json_primitive!(bool); +from_json_primitive!(u8); +from_json_primitive!(i8); +from_json_primitive!(u16); +from_json_primitive!(i16); +from_json_primitive!(u32); +from_json_primitive!(i32); +from_json_primitive!(u64); +from_json_primitive!(i64); +from_json_primitive!(u128); +from_json_primitive!(i128); +from_json_primitive!(String); impl ToJsonDict for BytesImpl { fn to_json_dict(&self, py: Python) -> PyResult {