clique_types/value/
rust.rs1use super::{Address, CliqueValue, Int256, Uint256};
2use indexmap::IndexMap;
3
4pub trait NotU8 {}
5
6impl NotU8 for bool {}
7impl NotU8 for String {}
8impl NotU8 for Address {}
9impl NotU8 for f64 {}
10impl NotU8 for i8 {}
11impl NotU8 for i16 {}
12impl NotU8 for u16 {}
13impl NotU8 for i32 {}
14impl NotU8 for u32 {}
15impl NotU8 for i64 {}
16impl NotU8 for u64 {}
17impl NotU8 for i128 {}
18impl NotU8 for u128 {}
19impl NotU8 for Int256 {}
20impl NotU8 for Uint256 {}
21
22impl TryFrom<CliqueValue> for bool {
23 type Error = anyhow::Error;
24
25 fn try_from(value: CliqueValue) -> Result<Self, Self::Error> {
26 if let CliqueValue::Bool(val) = value {
27 Ok(val)
28 } else {
29 anyhow::bail!("Type mismatch: expected CliqueValue::Bool")
30 }
31 }
32}
33
34impl From<bool> for CliqueValue {
35 fn from(value: bool) -> Self {
36 CliqueValue::Bool(value)
37 }
38}
39
40impl TryFrom<CliqueValue> for String {
41 type Error = anyhow::Error;
42
43 fn try_from(value: CliqueValue) -> Result<Self, Self::Error> {
44 if let CliqueValue::String(val) = value {
45 Ok(val)
46 } else {
47 anyhow::bail!("Type mismatch: expected CliqueValue::String")
48 }
49 }
50}
51
52impl From<String> for CliqueValue {
53 fn from(value: String) -> Self {
54 CliqueValue::String(value)
55 }
56}
57
58impl TryFrom<CliqueValue> for Vec<u8> {
59 type Error = anyhow::Error;
60
61 fn try_from(value: CliqueValue) -> Result<Self, Self::Error> {
62 if let CliqueValue::Bytes(val) = value {
63 Ok(val)
64 } else {
65 anyhow::bail!("Type mismatch: expected CliqueValue::Bytes")
66 }
67 }
68}
69
70impl From<Vec<u8>> for CliqueValue {
71 fn from(value: Vec<u8>) -> Self {
72 CliqueValue::Bytes(value)
73 }
74}
75
76impl TryFrom<CliqueValue> for Address {
77 type Error = anyhow::Error;
78
79 fn try_from(value: CliqueValue) -> Result<Self, Self::Error> {
80 if let CliqueValue::Address(val) = value {
81 Ok(val)
82 } else {
83 anyhow::bail!("Type mismatch: expected CliqueValue::Address")
84 }
85 }
86}
87
88impl From<Address> for CliqueValue {
89 fn from(value: Address) -> Self {
90 CliqueValue::Address(value)
91 }
92}
93
94impl TryFrom<CliqueValue> for f64 {
95 type Error = anyhow::Error;
96
97 fn try_from(value: CliqueValue) -> Result<Self, Self::Error> {
98 if let CliqueValue::Float(val) = value {
99 Ok(val)
100 } else {
101 anyhow::bail!("Type mismatch: expected CliqueValue::Float")
102 }
103 }
104}
105
106impl From<f64> for CliqueValue {
107 fn from(value: f64) -> Self {
108 CliqueValue::Float(value)
109 }
110}
111
112impl TryFrom<CliqueValue> for i8 {
113 type Error = anyhow::Error;
114
115 fn try_from(value: CliqueValue) -> Result<Self, Self::Error> {
116 if let CliqueValue::Int8(val) = value {
117 Ok(val)
118 } else {
119 anyhow::bail!("Type mismatch: expected CliqueValue::Int8")
120 }
121 }
122}
123
124impl From<i8> for CliqueValue {
125 fn from(value: i8) -> Self {
126 CliqueValue::Int8(value)
127 }
128}
129
130impl TryFrom<CliqueValue> for u8 {
131 type Error = anyhow::Error;
132
133 fn try_from(value: CliqueValue) -> Result<Self, Self::Error> {
134 if let CliqueValue::Uint8(val) = value {
135 Ok(val)
136 } else {
137 anyhow::bail!("Type mismatch: expected CliqueValue::Uint8")
138 }
139 }
140}
141
142impl From<u8> for CliqueValue {
143 fn from(value: u8) -> Self {
144 CliqueValue::Uint8(value)
145 }
146}
147
148impl TryFrom<CliqueValue> for i16 {
149 type Error = anyhow::Error;
150
151 fn try_from(value: CliqueValue) -> Result<Self, Self::Error> {
152 if let CliqueValue::Int16(val) = value {
153 Ok(val)
154 } else {
155 anyhow::bail!("Type mismatch: expected CliqueValue::Int16")
156 }
157 }
158}
159
160impl From<i16> for CliqueValue {
161 fn from(value: i16) -> Self {
162 CliqueValue::Int16(value)
163 }
164}
165
166impl TryFrom<CliqueValue> for u16 {
167 type Error = anyhow::Error;
168
169 fn try_from(value: CliqueValue) -> Result<Self, Self::Error> {
170 if let CliqueValue::Uint16(val) = value {
171 Ok(val)
172 } else {
173 anyhow::bail!("Type mismatch: expected CliqueValue::Uint16")
174 }
175 }
176}
177
178impl From<u16> for CliqueValue {
179 fn from(value: u16) -> Self {
180 CliqueValue::Uint16(value)
181 }
182}
183
184impl TryFrom<CliqueValue> for i32 {
185 type Error = anyhow::Error;
186
187 fn try_from(value: CliqueValue) -> Result<Self, Self::Error> {
188 if let CliqueValue::Int32(val) = value {
189 Ok(val)
190 } else {
191 anyhow::bail!("Type mismatch: expected CliqueValue::Int32")
192 }
193 }
194}
195
196impl From<i32> for CliqueValue {
197 fn from(value: i32) -> Self {
198 CliqueValue::Int32(value)
199 }
200}
201
202impl TryFrom<CliqueValue> for u32 {
203 type Error = anyhow::Error;
204
205 fn try_from(value: CliqueValue) -> Result<Self, Self::Error> {
206 if let CliqueValue::Uint32(val) = value {
207 Ok(val)
208 } else {
209 anyhow::bail!("Type mismatch: expected CliqueValue::Uint32")
210 }
211 }
212}
213
214impl From<u32> for CliqueValue {
215 fn from(value: u32) -> Self {
216 CliqueValue::Uint32(value)
217 }
218}
219
220impl TryFrom<CliqueValue> for i64 {
221 type Error = anyhow::Error;
222
223 fn try_from(value: CliqueValue) -> Result<Self, Self::Error> {
224 if let CliqueValue::Int64(val) = value {
225 Ok(val)
226 } else {
227 anyhow::bail!("Type mismatch: expected CliqueValue::Int64")
228 }
229 }
230}
231
232impl From<i64> for CliqueValue {
233 fn from(value: i64) -> Self {
234 CliqueValue::Int64(value)
235 }
236}
237
238impl TryFrom<CliqueValue> for u64 {
239 type Error = anyhow::Error;
240
241 fn try_from(value: CliqueValue) -> Result<Self, Self::Error> {
242 if let CliqueValue::Uint64(val) = value {
243 Ok(val)
244 } else {
245 anyhow::bail!("Type mismatch: expected CliqueValue::Uint64")
246 }
247 }
248}
249
250impl From<u64> for CliqueValue {
251 fn from(value: u64) -> Self {
252 CliqueValue::Uint64(value)
253 }
254}
255
256impl TryFrom<CliqueValue> for i128 {
257 type Error = anyhow::Error;
258
259 fn try_from(value: CliqueValue) -> Result<Self, Self::Error> {
260 if let CliqueValue::Int128(val) = value {
261 Ok(val)
262 } else {
263 anyhow::bail!("Type mismatch: expected CliqueValue::Int128")
264 }
265 }
266}
267
268impl From<i128> for CliqueValue {
269 fn from(value: i128) -> Self {
270 CliqueValue::Int128(value)
271 }
272}
273
274impl TryFrom<CliqueValue> for u128 {
275 type Error = anyhow::Error;
276
277 fn try_from(value: CliqueValue) -> Result<Self, Self::Error> {
278 if let CliqueValue::Uint128(val) = value {
279 Ok(val)
280 } else {
281 anyhow::bail!("Type mismatch: expected CliqueValue::Uint128")
282 }
283 }
284}
285
286impl From<u128> for CliqueValue {
287 fn from(value: u128) -> Self {
288 CliqueValue::Uint128(value)
289 }
290}
291
292impl TryFrom<CliqueValue> for Int256 {
293 type Error = anyhow::Error;
294
295 fn try_from(value: CliqueValue) -> Result<Self, Self::Error> {
296 if let CliqueValue::Int256(val) = value {
297 Ok(val)
298 } else {
299 anyhow::bail!("Type mismatch: expected CliqueValue::Int256")
300 }
301 }
302}
303
304impl From<Int256> for CliqueValue {
305 fn from(value: Int256) -> Self {
306 CliqueValue::Int256(value)
307 }
308}
309
310impl TryFrom<CliqueValue> for Uint256 {
311 type Error = anyhow::Error;
312
313 fn try_from(value: CliqueValue) -> Result<Self, Self::Error> {
314 if let CliqueValue::Uint256(val) = value {
315 Ok(val)
316 } else {
317 anyhow::bail!("Type mismatch: expected CliqueValue::Uint256")
318 }
319 }
320}
321
322impl From<Uint256> for CliqueValue {
323 fn from(value: Uint256) -> Self {
324 CliqueValue::Uint256(value)
325 }
326}
327
328impl<T> TryFrom<CliqueValue> for Vec<T>
329where
330 T: TryFrom<CliqueValue> + NotU8,
331{
332 type Error = anyhow::Error;
333
334 fn try_from(value: CliqueValue) -> Result<Self, Self::Error> {
335 if let CliqueValue::Array(vec) = value {
336 vec.into_iter()
337 .map(T::try_from)
338 .collect::<Result<Vec<T>, _>>()
339 .map_err(|_| anyhow::anyhow!("Type mismatch in array"))
340 } else {
341 anyhow::bail!("Type mismatch: expected CliqueValue::Array")
342 }
343 }
344}
345
346impl<T> From<Vec<T>> for CliqueValue
347where
348 CliqueValue: From<T>,
349 T: NotU8,
350{
351 fn from(vec: Vec<T>) -> Self {
352 CliqueValue::Array(vec.into_iter().map(CliqueValue::from).collect())
353 }
354}
355
356impl TryFrom<CliqueValue> for IndexMap<String, CliqueValue> {
357 type Error = anyhow::Error;
358
359 fn try_from(value: CliqueValue) -> Result<Self, Self::Error> {
360 if let CliqueValue::Custom(val) = value {
361 Ok(val)
362 } else {
363 anyhow::bail!("Type mismatch: expected CliqueValue::Custom")
364 }
365 }
366}
367
368impl From<IndexMap<String, CliqueValue>> for CliqueValue {
369 fn from(value: IndexMap<String, CliqueValue>) -> Self {
370 CliqueValue::Custom(value)
371 }
372}