forked from Chia-Network/chia_rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfrom_json_dict.rs
146 lines (136 loc) · 3.59 KB
/
from_json_dict.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
use pyo3::exceptions::PyValueError;
use pyo3::PyAny;
use pyo3::PyResult;
use crate::bytes::{Bytes, BytesImpl};
use hex::FromHex;
use std::convert::TryInto;
pub trait FromJsonDict {
fn from_json_dict(o: &PyAny) -> PyResult<Self>
where
Self: Sized;
}
impl<const N: usize> FromJsonDict for BytesImpl<N> {
fn from_json_dict(o: &PyAny) -> PyResult<Self> {
let s: String = o.extract()?;
if !s.starts_with("0x") {
return Err(PyValueError::new_err(
"bytes object is expected to start with 0x",
));
}
let s = &s[2..];
let buf = match Vec::from_hex(s) {
Err(_) => {
return Err(PyValueError::new_err("invalid hex"));
}
Ok(v) => v,
};
if buf.len() != N {
return Err(PyValueError::new_err(format!(
"invalid length {} expected {}",
buf.len(),
N
)));
}
Ok(buf.try_into()?)
}
}
impl FromJsonDict for Bytes {
fn from_json_dict(o: &PyAny) -> PyResult<Self> {
let s: String = o.extract()?;
if !s.starts_with("0x") {
return Err(PyValueError::new_err(
"bytes object is expected to start with 0x",
));
}
let s = &s[2..];
let buf = match Vec::from_hex(s) {
Err(_) => {
return Err(PyValueError::new_err("invalid hex"));
}
Ok(v) => v,
};
Ok(buf.into())
}
}
impl<T> FromJsonDict for Option<T>
where
T: FromJsonDict,
{
fn from_json_dict(o: &PyAny) -> PyResult<Self> {
if o.is_none() {
return Ok(None);
}
Ok(Some(<T as FromJsonDict>::from_json_dict(o)?))
}
}
macro_rules! from_json_primitive {
($t:ty) => {
impl FromJsonDict for $t {
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
T: FromJsonDict,
{
fn from_json_dict(o: &PyAny) -> PyResult<Self> {
let mut ret = Vec::<T>::new();
for v in o.iter()? {
ret.push(<T as FromJsonDict>::from_json_dict(v?)?);
}
Ok(ret)
}
}
impl<T, U> FromJsonDict for (T, U)
where
T: FromJsonDict,
U: FromJsonDict,
{
fn from_json_dict(o: &PyAny) -> PyResult<Self> {
if o.len()? != 2 {
return Err(PyValueError::new_err(format!(
"expected 2 elements, got {}",
o.len()?
)));
}
Ok((
<T as FromJsonDict>::from_json_dict(o.get_item(0)?)?,
<U as FromJsonDict>::from_json_dict(o.get_item(1)?)?,
))
}
}
impl<T, U, V> FromJsonDict for (T, U, V)
where
T: FromJsonDict,
U: FromJsonDict,
V: FromJsonDict,
{
fn from_json_dict(o: &PyAny) -> PyResult<Self> {
if o.len()? != 3 {
return Err(PyValueError::new_err(format!(
"expected 3 elements, got {}",
o.len()?
)));
}
Ok((
<T as FromJsonDict>::from_json_dict(o.get_item(0)?)?,
<U as FromJsonDict>::from_json_dict(o.get_item(1)?)?,
<V as FromJsonDict>::from_json_dict(o.get_item(2)?)?,
))
}
}