Skip to content

Commit 651ac49

Browse files
committed
trial simplified iterables
1 parent 76a9a58 commit 651ac49

File tree

6 files changed

+35
-64
lines changed

6 files changed

+35
-64
lines changed

src/input/input_python.rs

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -377,11 +377,7 @@ impl<'a> Input<'a> for PyAny {
377377
Ok(tuple.into())
378378
} else if let Some(collection) = extract_dict_iter!(self) {
379379
Ok(collection)
380-
} else if allow_any_iter && self.iter().is_ok() {
381-
Ok(self.into())
382-
} else if self.downcast::<PyIterator>().is_ok() {
383-
Ok(self.into())
384-
} else if is_deque(self) {
380+
} else if (allow_any_iter && self.iter().is_ok()) || self.downcast::<PyIterator>().is_ok() || is_deque(self) {
385381
Ok(self.into())
386382
} else {
387383
Err(ValError::new(ErrorType::ListType, self))
@@ -403,9 +399,7 @@ impl<'a> Input<'a> for PyAny {
403399
Ok(list.into())
404400
} else if let Some(collection) = extract_dict_iter!(self) {
405401
Ok(collection)
406-
} else if self.downcast::<PyIterator>().is_ok() {
407-
Ok(self.into())
408-
} else if is_deque(self) {
402+
} else if self.downcast::<PyIterator>().is_ok() || is_deque(self) {
409403
Ok(self.into())
410404
} else {
411405
Err(ValError::new(ErrorType::TupleType, self))
@@ -431,9 +425,7 @@ impl<'a> Input<'a> for PyAny {
431425
Ok(frozen_set.into())
432426
} else if let Some(collection) = extract_dict_iter!(self) {
433427
Ok(collection)
434-
} else if self.downcast::<PyIterator>().is_ok() {
435-
Ok(self.into())
436-
} else if is_deque(self) {
428+
} else if self.downcast::<PyIterator>().is_ok() || is_deque(self) {
437429
Ok(self.into())
438430
} else {
439431
Err(ValError::new(ErrorType::SetType, self))
@@ -459,9 +451,7 @@ impl<'a> Input<'a> for PyAny {
459451
Ok(tuple.into())
460452
} else if let Some(collection) = extract_dict_iter!(self) {
461453
Ok(collection)
462-
} else if self.downcast::<PyIterator>().is_ok() {
463-
Ok(self.into())
464-
} else if is_deque(self) {
454+
} else if self.downcast::<PyIterator>().is_ok() || is_deque(self) {
465455
Ok(self.into())
466456
} else {
467457
Err(ValError::new(ErrorType::FrozenSetType, self))

src/input/return_enums.rs

Lines changed: 31 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,22 @@ impl<'a, INPUT: Input<'a>> MaxLengthCheck<'a, INPUT> {
8484
}
8585
}
8686

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)]
87100
fn validate_iter_to_vec<'a, 's>(
88101
py: Python<'a>,
89-
iter: impl Iterator<Item = &'a (impl Input<'a> + 'a)>,
102+
iter: impl Iterator<Item = PyResult<&'a (impl Input<'a> + 'a)>>,
90103
capacity: usize,
91104
mut max_length_check: MaxLengthCheck<'a, impl Input<'a>>,
92105
validator: &'s CombinedValidator,
@@ -96,7 +109,8 @@ fn validate_iter_to_vec<'a, 's>(
96109
) -> ValResult<'a, Vec<PyObject>> {
97110
let mut output: Vec<PyObject> = Vec::with_capacity(capacity);
98111
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))?;
100114
match validator.validate(py, item, extra, definitions, recursion_guard) {
101115
Ok(item) => {
102116
max_length_check.incr()?;
@@ -129,18 +143,6 @@ fn no_validator_iter_to_vec<'a, 's>(
129143
.collect()
130144
}
131145

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-
144146
// pretty arbitrary default capacity when creating vecs from iteration
145147
static DEFAULT_CAPACITY: usize = 10;
146148

@@ -163,7 +165,6 @@ impl<'a> GenericCollection<'a> {
163165
input: &'a impl Input<'a>,
164166
max_length: Option<usize>,
165167
field_type: &'static str,
166-
generator_max_length: Option<usize>,
167168
validator: &'s CombinedValidator,
168169
extra: &Extra,
169170
definitions: &'a [CombinedValidator],
@@ -176,7 +177,7 @@ impl<'a> GenericCollection<'a> {
176177
match self {
177178
Self::List(collection) => validate_iter_to_vec(
178179
py,
179-
collection.iter(),
180+
collection.iter().map(Ok),
180181
capacity,
181182
max_length_check,
182183
validator,
@@ -186,7 +187,7 @@ impl<'a> GenericCollection<'a> {
186187
),
187188
Self::Tuple(collection) => validate_iter_to_vec(
188189
py,
189-
collection.iter(),
190+
collection.iter().map(Ok),
190191
capacity,
191192
max_length_check,
192193
validator,
@@ -196,7 +197,7 @@ impl<'a> GenericCollection<'a> {
196197
),
197198
Self::Set(collection) => validate_iter_to_vec(
198199
py,
199-
collection.iter(),
200+
collection.iter().map(Ok),
200201
capacity,
201202
max_length_check,
202203
validator,
@@ -206,43 +207,27 @@ impl<'a> GenericCollection<'a> {
206207
),
207208
Self::FrozenSet(collection) => validate_iter_to_vec(
208209
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()?,
210221
capacity,
211222
max_length_check,
212223
validator,
213224
extra,
214225
definitions,
215226
recursion_guard,
216227
),
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-
}
243228
Self::JsonArray(collection) => validate_iter_to_vec(
244229
py,
245-
collection.iter(),
230+
collection.iter().map(Ok),
246231
capacity,
247232
max_length_check,
248233
validator,

src/validators/frozenset.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ impl Validator for FrozenSetValidator {
4444
input,
4545
self.max_length,
4646
"Frozenset",
47-
self.generator_max_length,
4847
v,
4948
extra,
5049
definitions,

src/validators/list.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ impl Validator for ListValidator {
9595
input,
9696
self.max_length,
9797
"List",
98-
self.max_length,
9998
v,
10099
extra,
101100
definitions,

src/validators/set.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ impl Validator for SetValidator {
7474
input,
7575
self.max_length,
7676
"Set",
77-
self.generator_max_length,
7877
v,
7978
extra,
8079
definitions,

src/validators/tuple.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ impl Validator for TupleVariableValidator {
5858
input,
5959
self.max_length,
6060
"Tuple",
61-
self.max_length,
6261
v,
6362
extra,
6463
definitions,

0 commit comments

Comments
 (0)