@@ -19,8 +19,8 @@ use super::input_abstract::{ConsumeIterator, Never, ValMatch};
19
19
use super :: return_enums:: ValidationMatch ;
20
20
use super :: shared:: { float_as_int, int_as_bool, str_as_bool, str_as_float, str_as_int} ;
21
21
use super :: {
22
- Arguments , BorrowInput , EitherBytes , EitherFloat , EitherInt , EitherString , EitherTimedelta , GenericIterable ,
23
- GenericIterator , Input , KeywordArgs , PositionalArgs , ValidatedDict , ValidatedList ,
22
+ Arguments , BorrowInput , EitherBytes , EitherFloat , EitherInt , EitherString , EitherTimedelta , GenericIterator , Input ,
23
+ KeywordArgs , PositionalArgs , ValidatedDict , ValidatedList , ValidatedSet , ValidatedTuple ,
24
24
} ;
25
25
26
26
/// This is required but since JSON object keys are always strings, I don't think it can be called
@@ -185,55 +185,38 @@ impl<'py> Input<'py> for JsonValue {
185
185
186
186
fn validate_list ( & self , _strict : bool ) -> ValMatch < & JsonArray > {
187
187
match self {
188
- JsonValue :: Array ( a) => Ok ( ValidationMatch :: strict ( a) ) ,
188
+ JsonValue :: Array ( a) => Ok ( ValidationMatch :: exact ( a) ) ,
189
189
_ => Err ( ValError :: new ( ErrorTypeDefaults :: ListType , self ) ) ,
190
190
}
191
191
}
192
192
193
- fn validate_tuple < ' a > ( & ' a self , _strict : bool ) -> ValResult < GenericIterable < ' a , ' py > > {
193
+ type Tuple < ' a > = & ' a JsonArray ;
194
+
195
+ fn validate_tuple ( & self , _strict : bool ) -> ValMatch < & JsonArray > {
194
196
// just as in set's case, List has to be allowed
195
197
match self {
196
- JsonValue :: Array ( a) => Ok ( GenericIterable :: JsonArray ( a) ) ,
198
+ JsonValue :: Array ( a) => Ok ( ValidationMatch :: strict ( a) ) ,
197
199
_ => Err ( ValError :: new ( ErrorTypeDefaults :: TupleType , self ) ) ,
198
200
}
199
201
}
200
- #[ cfg_attr( has_coverage_attribute, coverage( off) ) ]
201
- fn strict_tuple < ' a > ( & ' a self ) -> ValResult < GenericIterable < ' a , ' py > > {
202
- self . validate_tuple ( false )
203
- }
204
202
205
- fn validate_set < ' a > ( & ' a self , _strict : bool ) -> ValResult < GenericIterable < ' a , ' py > > {
203
+ type Set < ' a > = & ' a JsonArray ;
204
+
205
+ fn validate_set ( & self , _strict : bool ) -> ValMatch < & JsonArray > {
206
206
// we allow a list here since otherwise it would be impossible to create a set from JSON
207
207
match self {
208
- JsonValue :: Array ( a) => Ok ( GenericIterable :: JsonArray ( a) ) ,
208
+ JsonValue :: Array ( a) => Ok ( ValidationMatch :: strict ( a) ) ,
209
209
_ => Err ( ValError :: new ( ErrorTypeDefaults :: SetType , self ) ) ,
210
210
}
211
211
}
212
- #[ cfg_attr( has_coverage_attribute, coverage( off) ) ]
213
- fn strict_set < ' a > ( & ' a self ) -> ValResult < GenericIterable < ' a , ' py > > {
214
- self . validate_set ( false )
215
- }
216
212
217
- fn validate_frozenset < ' a > ( & ' a self , _strict : bool ) -> ValResult < GenericIterable < ' a , ' py > > {
213
+ fn validate_frozenset ( & self , _strict : bool ) -> ValMatch < & JsonArray > {
218
214
// we allow a list here since otherwise it would be impossible to create a frozenset from JSON
219
215
match self {
220
- JsonValue :: Array ( a) => Ok ( GenericIterable :: JsonArray ( a) ) ,
216
+ JsonValue :: Array ( a) => Ok ( ValidationMatch :: strict ( a) ) ,
221
217
_ => Err ( ValError :: new ( ErrorTypeDefaults :: FrozenSetType , self ) ) ,
222
218
}
223
219
}
224
- #[ cfg_attr( has_coverage_attribute, coverage( off) ) ]
225
- fn strict_frozenset < ' a > ( & ' a self ) -> ValResult < GenericIterable < ' a , ' py > > {
226
- self . validate_frozenset ( false )
227
- }
228
-
229
- fn extract_generic_iterable ( & self ) -> ValResult < GenericIterable < ' _ , ' py > > {
230
- match self {
231
- JsonValue :: Array ( a) => Ok ( GenericIterable :: JsonArray ( a) ) ,
232
- JsonValue :: Str ( s) => Ok ( GenericIterable :: JsonString ( s) ) ,
233
- JsonValue :: Object ( object) => Ok ( GenericIterable :: JsonObject ( object) ) ,
234
- _ => Err ( ValError :: new ( ErrorTypeDefaults :: IterableType , self ) ) ,
235
- }
236
- }
237
220
238
221
fn validate_iter ( & self ) -> ValResult < GenericIterator > {
239
222
match self {
@@ -392,23 +375,23 @@ impl<'py> Input<'py> for str {
392
375
Err ( ValError :: new ( ErrorTypeDefaults :: ListType , self ) )
393
376
}
394
377
378
+ type Tuple < ' a > = Never ;
379
+
395
380
#[ cfg_attr( has_coverage_attribute, coverage( off) ) ]
396
- fn strict_tuple < ' a > ( & ' a self ) -> ValResult < GenericIterable < ' a , ' py > > {
381
+ fn validate_tuple ( & self , _strict : bool ) -> ValMatch < Never > {
397
382
Err ( ValError :: new ( ErrorTypeDefaults :: TupleType , self ) )
398
383
}
399
384
385
+ type Set < ' a > = Never ;
386
+
400
387
#[ cfg_attr( has_coverage_attribute, coverage( off) ) ]
401
- fn strict_set < ' a > ( & ' a self ) -> ValResult < GenericIterable < ' a , ' py > > {
388
+ fn validate_set ( & self , _strict : bool ) -> ValMatch < Never > {
402
389
Err ( ValError :: new ( ErrorTypeDefaults :: SetType , self ) )
403
390
}
404
391
405
392
#[ cfg_attr( has_coverage_attribute, coverage( off) ) ]
406
- fn strict_frozenset < ' a > ( & ' a self ) -> ValResult < GenericIterable < ' a , ' py > > {
407
- Err ( ValError :: new ( ErrorTypeDefaults :: FrozenSetType , self ) )
408
- }
409
-
410
- fn extract_generic_iterable < ' a > ( & ' a self ) -> ValResult < GenericIterable < ' a , ' py > > {
411
- Ok ( GenericIterable :: JsonString ( self ) )
393
+ fn validate_frozenset ( & self , _strict : bool ) -> ValMatch < Never > {
394
+ Err ( ValError :: new ( ErrorTypeDefaults :: SetType , self ) )
412
395
}
413
396
414
397
fn validate_iter ( & self ) -> ValResult < GenericIterator > {
@@ -504,6 +487,25 @@ impl<'a, 'py> ValidatedList<'py> for &'a JsonArray {
504
487
}
505
488
}
506
489
490
+ impl < ' a , ' py > ValidatedTuple < ' py > for & ' a JsonArray {
491
+ type Item = & ' a JsonValue ;
492
+
493
+ fn len ( & self ) -> Option < usize > {
494
+ Some ( SmallVec :: len ( self ) )
495
+ }
496
+ fn iterate < R > ( self , consumer : impl ConsumeIterator < PyResult < Self :: Item > , Output = R > ) -> ValResult < R > {
497
+ Ok ( consumer. consume_iterator ( self . iter ( ) . map ( Ok ) ) )
498
+ }
499
+ }
500
+
501
+ impl < ' a , ' py > ValidatedSet < ' py > for & ' a JsonArray {
502
+ type Item = & ' a JsonValue ;
503
+
504
+ fn iterate < R > ( self , consumer : impl ConsumeIterator < PyResult < Self :: Item > , Output = R > ) -> ValResult < R > {
505
+ Ok ( consumer. consume_iterator ( self . iter ( ) . map ( Ok ) ) )
506
+ }
507
+ }
508
+
507
509
#[ cfg_attr( debug_assertions, derive( Debug ) ) ]
508
510
pub struct JsonArgs < ' a > {
509
511
args : Option < & ' a [ JsonValue ] > ,
0 commit comments