Skip to content

Commit

Permalink
add support for remaining primitive types to json serialization/deser…
Browse files Browse the repository at this point in the history
…ialization trait
  • Loading branch information
arvidn committed Nov 19, 2022
1 parent 1053e4f commit da556c9
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 34 deletions.
6 changes: 6 additions & 0 deletions chia-protocol/src/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ impl From<Vec<u8>> 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))
Expand Down
39 changes: 19 additions & 20 deletions chia-protocol/src/from_json_dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,29 +69,28 @@ where
}
}

impl FromJsonDict for bool {
fn from_json_dict(o: &PyAny) -> PyResult<Self> {
o.extract()
}
}

impl FromJsonDict for u32 {
fn from_json_dict(o: &PyAny) -> PyResult<Self> {
o.extract()
}
}

impl FromJsonDict for u64 {
fn from_json_dict(o: &PyAny) -> PyResult<Self> {
o.extract()
macro_rules! from_json_primitive {
($t:ty) => {
impl FromJsonDict for $t {
fn from_json_dict(o: &PyAny) -> PyResult<Self> {
o.extract()
}
}
}
}

impl FromJsonDict for String {
fn from_json_dict(o: &PyAny) -> PyResult<Self> {
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<T> FromJsonDict for Vec<T>
where
Expand Down
33 changes: 19 additions & 14 deletions chia-protocol/src/to_json_dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,28 @@ pub trait ToJsonDict {
fn to_json_dict(&self, py: Python) -> PyResult<PyObject>;
}

impl ToJsonDict for bool {
fn to_json_dict(&self, py: Python) -> PyResult<PyObject> {
Ok(self.to_object(py))
}
}

impl ToJsonDict for u32 {
fn to_json_dict(&self, py: Python) -> PyResult<PyObject> {
Ok(self.to_object(py))
macro_rules! from_json_primitive {
($t:ty) => {
impl ToJsonDict for $t {
fn to_json_dict(&self, py: Python) -> PyResult<PyObject> {
Ok(self.to_object(py))
}
}
}
}

impl ToJsonDict for u64 {
fn to_json_dict(&self, py: Python) -> PyResult<PyObject> {
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<const N: usize> ToJsonDict for BytesImpl<N> {
fn to_json_dict(&self, py: Python) -> PyResult<PyObject> {
Expand Down

0 comments on commit da556c9

Please sign in to comment.