1
+ use crate :: shared:: DataTypeIdentifier ;
1
2
use crate :: shared:: data_type_input_types_rule_config:: DataTypeInputType ;
2
3
use crate :: shared:: {
3
4
DataTypeContainsKeyRuleConfig , DataTypeContainsTypeRuleConfig , DataTypeInputTypesRuleConfig ,
@@ -14,20 +15,24 @@ impl RuleBuilder {
14
15
Self { rules : Vec :: new ( ) }
15
16
}
16
17
17
- pub fn add_contains_key ( mut self , key : String , data_type_identifier : String ) -> Self {
18
+ pub fn add_contains_key (
19
+ mut self ,
20
+ key : String ,
21
+ data_type_identifier : DataTypeIdentifier ,
22
+ ) -> Self {
18
23
self . rules . push ( DataTypeRule {
19
24
config : Some ( Config :: ContainsKey ( DataTypeContainsKeyRuleConfig {
20
25
key,
21
- data_type_identifier,
26
+ data_type_identifier : Some ( data_type_identifier ) ,
22
27
} ) ) ,
23
28
} ) ;
24
29
self
25
30
}
26
31
27
- pub fn add_contains_type ( mut self , data_type_identifier : String ) -> Self {
32
+ pub fn add_contains_type ( mut self , data_type_identifier : DataTypeIdentifier ) -> Self {
28
33
self . rules . push ( DataTypeRule {
29
34
config : Some ( Config :: ContainsType ( DataTypeContainsTypeRuleConfig {
30
- data_type_identifier,
35
+ data_type_identifier : Some ( data_type_identifier ) ,
31
36
} ) ) ,
32
37
} ) ;
33
38
self
@@ -69,10 +74,10 @@ impl RuleBuilder {
69
74
self
70
75
}
71
76
72
- pub fn add_return_type ( mut self , data_type_identifier : String ) -> Self {
77
+ pub fn add_return_type ( mut self , data_type_identifier : DataTypeIdentifier ) -> Self {
73
78
self . rules . push ( DataTypeRule {
74
79
config : Some ( Config :: ReturnType ( DataTypeReturnTypeRuleConfig {
75
- data_type_identifier,
80
+ data_type_identifier : Some ( data_type_identifier ) ,
76
81
} ) ) ,
77
82
} ) ;
78
83
self
@@ -87,32 +92,40 @@ impl RuleBuilder {
87
92
mod tests {
88
93
use super :: * ;
89
94
use crate :: shared:: {
90
- data_type_input_types_rule_config :: DataTypeInputType , data_type_rule :: Config ,
91
- helper:: value:: ToValue ,
95
+ data_type_identifier :: Type , data_type_input_types_rule_config :: DataTypeInputType ,
96
+ data_type_rule :: Config , helper:: value:: ToValue ,
92
97
} ;
93
98
99
+ fn to_data_type ( str : & str ) -> DataTypeIdentifier {
100
+ DataTypeIdentifier {
101
+ r#type : Some ( Type :: DataTypeIdentifier ( str. into ( ) ) ) ,
102
+ }
103
+ }
104
+
94
105
#[ test]
95
106
fn test_add_contains_key ( ) {
96
107
let rules = RuleBuilder :: new ( )
97
- . add_contains_key ( "id" . into ( ) , "User" . into ( ) )
108
+ . add_contains_key ( "id" . into ( ) , to_data_type ( "User" ) )
98
109
. build ( ) ;
99
110
100
111
match & rules[ 0 ] . config {
101
112
Some ( Config :: ContainsKey ( cfg) ) => {
102
113
assert_eq ! ( cfg. key, "id" ) ;
103
- assert_eq ! ( cfg. data_type_identifier, "User" ) ;
114
+ assert_eq ! ( cfg. data_type_identifier, Some ( to_data_type ( "User" ) ) ) ;
104
115
}
105
116
_ => panic ! ( "Expected ContainsKey config" ) ,
106
117
}
107
118
}
108
119
109
120
#[ test]
110
121
fn test_add_contains_type ( ) {
111
- let rules = RuleBuilder :: new ( ) . add_contains_type ( "User" . into ( ) ) . build ( ) ;
122
+ let rules = RuleBuilder :: new ( )
123
+ . add_contains_type ( to_data_type ( "User" ) )
124
+ . build ( ) ;
112
125
113
126
match & rules[ 0 ] . config {
114
127
Some ( Config :: ContainsType ( cfg) ) => {
115
- assert_eq ! ( cfg. data_type_identifier, "User" ) ;
128
+ assert_eq ! ( cfg. data_type_identifier, Some ( to_data_type ( "User" ) ) ) ;
116
129
}
117
130
_ => panic ! ( "Expected ContainsType config" ) ,
118
131
}
@@ -163,11 +176,11 @@ mod tests {
163
176
fn test_add_input_types ( ) {
164
177
let input_types = vec ! [
165
178
DataTypeInputType {
166
- data_type_identifier: "Type1" . into ( ) ,
179
+ data_type_identifier: Some ( to_data_type ( "Type1" ) ) ,
167
180
input_identifier: "input1" . into( ) ,
168
181
} ,
169
182
DataTypeInputType {
170
- data_type_identifier: "Type2" . into ( ) ,
183
+ data_type_identifier: Some ( to_data_type ( "Type2" ) ) ,
171
184
input_identifier: "input2" . into( ) ,
172
185
} ,
173
186
] ;
@@ -186,11 +199,13 @@ mod tests {
186
199
187
200
#[ test]
188
201
fn test_add_return_type ( ) {
189
- let rules = RuleBuilder :: new ( ) . add_return_type ( "Result" . into ( ) ) . build ( ) ;
202
+ let rules = RuleBuilder :: new ( )
203
+ . add_return_type ( to_data_type ( "Result" ) )
204
+ . build ( ) ;
190
205
191
206
match & rules[ 0 ] . config {
192
207
Some ( Config :: ReturnType ( cfg) ) => {
193
- assert_eq ! ( cfg. data_type_identifier, "Result" ) ;
208
+ assert_eq ! ( cfg. data_type_identifier, Some ( to_data_type ( "Result" ) ) ) ;
194
209
}
195
210
_ => panic ! ( "Expected ReturnType config" ) ,
196
211
}
@@ -199,23 +214,23 @@ mod tests {
199
214
#[ test]
200
215
fn test_add_many_rules ( ) {
201
216
let rules = RuleBuilder :: new ( )
202
- . add_contains_key ( "id" . into ( ) , "User" . into ( ) )
203
- . add_return_type ( "Result" . into ( ) )
217
+ . add_contains_key ( "id" . into ( ) , to_data_type ( "User" ) )
218
+ . add_return_type ( to_data_type ( "Result" ) )
204
219
. add_regex ( r"^\d+$" . into ( ) )
205
- . add_contains_key ( "id" . into ( ) , "User" . into ( ) )
220
+ . add_contains_key ( "id" . into ( ) , to_data_type ( "User" ) )
206
221
. build ( ) ;
207
222
208
223
match & rules[ 0 ] . config {
209
224
Some ( Config :: ContainsKey ( cfg) ) => {
210
225
assert_eq ! ( cfg. key, "id" ) ;
211
- assert_eq ! ( cfg. data_type_identifier, "User" ) ;
226
+ assert_eq ! ( cfg. data_type_identifier, Some ( to_data_type ( "User" ) ) ) ;
212
227
}
213
228
_ => panic ! ( "Expected ContainsKey config" ) ,
214
229
}
215
230
216
231
match & rules[ 1 ] . config {
217
232
Some ( Config :: ReturnType ( cfg) ) => {
218
- assert_eq ! ( cfg. data_type_identifier, "Result" ) ;
233
+ assert_eq ! ( cfg. data_type_identifier, Some ( to_data_type ( "Result" ) ) ) ;
219
234
}
220
235
_ => panic ! ( "Expected ReturnType config" ) ,
221
236
}
@@ -230,7 +245,7 @@ mod tests {
230
245
match & rules[ 3 ] . config {
231
246
Some ( Config :: ContainsKey ( cfg) ) => {
232
247
assert_eq ! ( cfg. key, "id" ) ;
233
- assert_eq ! ( cfg. data_type_identifier, "User" ) ;
248
+ assert_eq ! ( cfg. data_type_identifier, Some ( to_data_type ( "User" ) ) ) ;
234
249
}
235
250
_ => panic ! ( "Expected ContainsKey config" ) ,
236
251
}
0 commit comments