@@ -84,9 +84,22 @@ impl<'a, INPUT: Input<'a>> MaxLengthCheck<'a, INPUT> {
84
84
}
85
85
}
86
86
87
+ macro_rules! any_next_error {
88
+ ( $py: expr, $err: ident, $input: expr, $index: ident) => {
89
+ ValError :: new_with_loc(
90
+ ErrorType :: IterationError {
91
+ error: py_err_string( $py, $err) ,
92
+ } ,
93
+ $input,
94
+ $index,
95
+ )
96
+ } ;
97
+ }
98
+
99
+ #[ allow( clippy:: too_many_arguments) ]
87
100
fn validate_iter_to_vec < ' a , ' s > (
88
101
py : Python < ' a > ,
89
- iter : impl Iterator < Item = & ' a ( impl Input < ' a > + ' a ) > ,
102
+ iter : impl Iterator < Item = PyResult < & ' a ( impl Input < ' a > + ' a ) > > ,
90
103
capacity : usize ,
91
104
mut max_length_check : MaxLengthCheck < ' a , impl Input < ' a > > ,
92
105
validator : & ' s CombinedValidator ,
@@ -96,7 +109,8 @@ fn validate_iter_to_vec<'a, 's>(
96
109
) -> ValResult < ' a , Vec < PyObject > > {
97
110
let mut output: Vec < PyObject > = Vec :: with_capacity ( capacity) ;
98
111
let mut errors: Vec < ValLineError > = Vec :: new ( ) ;
99
- for ( index, item) in iter. enumerate ( ) {
112
+ for ( index, item_result) in iter. enumerate ( ) {
113
+ let item = item_result. map_err ( |e| any_next_error ! ( py, e, max_length_check. input, index) ) ?;
100
114
match validator. validate ( py, item, extra, definitions, recursion_guard) {
101
115
Ok ( item) => {
102
116
max_length_check. incr ( ) ?;
@@ -129,18 +143,6 @@ fn no_validator_iter_to_vec<'a, 's>(
129
143
. collect ( )
130
144
}
131
145
132
- macro_rules! any_next_error {
133
- ( $py: expr, $err: ident, $input: ident, $index: ident) => {
134
- ValError :: new_with_loc(
135
- ErrorType :: IterationError {
136
- error: py_err_string( $py, $err) ,
137
- } ,
138
- $input,
139
- $index,
140
- )
141
- } ;
142
- }
143
-
144
146
// pretty arbitrary default capacity when creating vecs from iteration
145
147
static DEFAULT_CAPACITY : usize = 10 ;
146
148
@@ -163,7 +165,6 @@ impl<'a> GenericCollection<'a> {
163
165
input : & ' a impl Input < ' a > ,
164
166
max_length : Option < usize > ,
165
167
field_type : & ' static str ,
166
- generator_max_length : Option < usize > ,
167
168
validator : & ' s CombinedValidator ,
168
169
extra : & Extra ,
169
170
definitions : & ' a [ CombinedValidator ] ,
@@ -176,7 +177,7 @@ impl<'a> GenericCollection<'a> {
176
177
match self {
177
178
Self :: List ( collection) => validate_iter_to_vec (
178
179
py,
179
- collection. iter ( ) ,
180
+ collection. iter ( ) . map ( Ok ) ,
180
181
capacity,
181
182
max_length_check,
182
183
validator,
@@ -186,7 +187,7 @@ impl<'a> GenericCollection<'a> {
186
187
) ,
187
188
Self :: Tuple ( collection) => validate_iter_to_vec (
188
189
py,
189
- collection. iter ( ) ,
190
+ collection. iter ( ) . map ( Ok ) ,
190
191
capacity,
191
192
max_length_check,
192
193
validator,
@@ -196,7 +197,7 @@ impl<'a> GenericCollection<'a> {
196
197
) ,
197
198
Self :: Set ( collection) => validate_iter_to_vec (
198
199
py,
199
- collection. iter ( ) ,
200
+ collection. iter ( ) . map ( Ok ) ,
200
201
capacity,
201
202
max_length_check,
202
203
validator,
@@ -206,43 +207,27 @@ impl<'a> GenericCollection<'a> {
206
207
) ,
207
208
Self :: FrozenSet ( collection) => validate_iter_to_vec (
208
209
py,
209
- collection. iter ( ) ,
210
+ collection. iter ( ) . map ( Ok ) ,
211
+ capacity,
212
+ max_length_check,
213
+ validator,
214
+ extra,
215
+ definitions,
216
+ recursion_guard,
217
+ ) ,
218
+ Self :: PyAny ( collection) => validate_iter_to_vec (
219
+ py,
220
+ collection. iter ( ) ?,
210
221
capacity,
211
222
max_length_check,
212
223
validator,
213
224
extra,
214
225
definitions,
215
226
recursion_guard,
216
227
) ,
217
- Self :: PyAny ( collection) => {
218
- let iter = collection. iter ( ) ?;
219
- let mut output: Vec < PyObject > = Vec :: with_capacity ( capacity) ;
220
- let mut errors: Vec < ValLineError > = Vec :: new ( ) ;
221
- let mut max_length_check = MaxLengthCheck :: new ( generator_max_length, field_type, input) ;
222
- for ( index, item_result) in iter. enumerate ( ) {
223
- let item = item_result. map_err ( |e| any_next_error ! ( collection. py( ) , e, input, index) ) ?;
224
- match validator. validate ( py, item, extra, definitions, recursion_guard) {
225
- Ok ( item) => {
226
- max_length_check. incr ( ) ?;
227
- output. push ( item) ;
228
- }
229
- Err ( ValError :: LineErrors ( line_errors) ) => {
230
- errors. extend ( line_errors. into_iter ( ) . map ( |err| err. with_outer_location ( index. into ( ) ) ) ) ;
231
- }
232
- Err ( ValError :: Omit ) => ( ) ,
233
- Err ( err) => return Err ( err) ,
234
- }
235
- }
236
-
237
- if errors. is_empty ( ) {
238
- Ok ( output)
239
- } else {
240
- Err ( ValError :: LineErrors ( errors) )
241
- }
242
- }
243
228
Self :: JsonArray ( collection) => validate_iter_to_vec (
244
229
py,
245
- collection. iter ( ) ,
230
+ collection. iter ( ) . map ( Ok ) ,
246
231
capacity,
247
232
max_length_check,
248
233
validator,
0 commit comments