1
1
use crate :: BigNum ;
2
2
use serde:: { Deserialize , Serialize } ;
3
3
use serde_json:: { value:: Value as SerdeValue , Number } ;
4
- use std:: convert:: TryFrom ;
5
- use std:: { fmt, str:: FromStr } ;
4
+ use std:: { convert:: TryFrom , fmt, str:: FromStr } ;
6
5
7
6
pub type Map = serde_json:: value:: Map < String , SerdeValue > ;
8
7
9
- use super :: { Input , Output , TryGet } ;
8
+ use super :: { Input , Output } ;
10
9
11
10
#[ derive( Debug , Eq , PartialEq ) ]
12
11
pub enum Error {
@@ -52,6 +51,28 @@ impl Value {
52
51
}
53
52
}
54
53
54
+ impl TryFrom < SerdeValue > for Value {
55
+ type Error = Error ;
56
+
57
+ fn try_from ( serde_value : SerdeValue ) -> Result < Self , Self :: Error > {
58
+ match serde_value {
59
+ SerdeValue :: Bool ( bool) => Ok ( Self :: Bool ( bool) ) ,
60
+ SerdeValue :: Number ( number) => Ok ( Self :: Number ( number) ) ,
61
+ // String can be either a String or a BigNum
62
+ // but we handle this case by using Type casting with the Function::Bn
63
+ SerdeValue :: String ( string) => Ok ( Value :: String ( string) ) ,
64
+ SerdeValue :: Array ( serde_array) => {
65
+ let array = serde_array
66
+ . into_iter ( )
67
+ . map ( Value :: try_from)
68
+ . collect :: < Result < _ , _ > > ( ) ?;
69
+ Ok ( Self :: Array ( array) )
70
+ }
71
+ SerdeValue :: Object ( _) | SerdeValue :: Null => Err ( Error :: TypeError ) ,
72
+ }
73
+ }
74
+ }
75
+
55
76
#[ derive( Debug , Serialize , Deserialize , Clone , PartialEq , Eq ) ]
56
77
#[ serde( rename_all = "camelCase" ) ]
57
78
// TODO: https://github.com/AdExNetwork/adex-validator-stack-rust/issues/296
@@ -94,26 +115,6 @@ impl Function {
94
115
}
95
116
}
96
117
97
- impl TryFrom < SerdeValue > for Value {
98
- type Error = Error ;
99
-
100
- fn try_from ( serde_value : SerdeValue ) -> Result < Self , Self :: Error > {
101
- match serde_value {
102
- SerdeValue :: Bool ( bool) => Ok ( Self :: Bool ( bool) ) ,
103
- SerdeValue :: Number ( number) => Ok ( Self :: Number ( number) ) ,
104
- SerdeValue :: String ( string) => Ok ( Value :: String ( string) ) ,
105
- SerdeValue :: Array ( serde_array) => {
106
- let array = serde_array
107
- . into_iter ( )
108
- . map ( Value :: try_from)
109
- . collect :: < Result < _ , _ > > ( ) ?;
110
- Ok ( Self :: Array ( array) )
111
- }
112
- SerdeValue :: Object ( _) | SerdeValue :: Null => Err ( Error :: TypeError ) ,
113
- }
114
- }
115
- }
116
-
117
118
impl Value {
118
119
pub fn try_bool ( self ) -> Result < bool , Error > {
119
120
match self {
@@ -137,6 +138,7 @@ impl Value {
137
138
/// - Number
138
139
/// - String
139
140
/// - Array
141
+ /// - BigNum
140
142
/// - Mutates output
141
143
/// - Throws an error
142
144
fn eval ( input : & Input , output : & mut Output , rule : & Rule ) -> Result < Option < Value > , Error > {
0 commit comments