Skip to content

Commit 4774adf

Browse files
authored
rename GenericListLike into GenericCollection + improve coverage (#222)
* rename GenericListLike into GenericCollection + update docstring * sets cannot be validated into tuples * add time delta parsing test * add test for bytes * add test for errors on json for ints and floats * small tweaks after @samuelcolvin review
1 parent 741cd85 commit 4774adf

File tree

11 files changed

+156
-105
lines changed

11 files changed

+156
-105
lines changed

src/input/input_abstract.rs

+13-13
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::input::datetime::EitherTime;
88

99
use super::datetime::{EitherDate, EitherDateTime, EitherTimedelta};
1010
use super::return_enums::{EitherBytes, EitherString};
11-
use super::{GenericArguments, GenericListLike, GenericMapping};
11+
use super::{GenericArguments, GenericCollection, GenericMapping};
1212

1313
/// all types have three methods: `validate_*`, `strict_*`, `lax_*`
1414
/// the convention is to either implement:
@@ -126,55 +126,55 @@ pub trait Input<'a>: fmt::Debug + ToPyObject {
126126
self.validate_dict(strict)
127127
}
128128

129-
fn validate_list(&'a self, strict: bool) -> ValResult<GenericListLike<'a>> {
129+
fn validate_list(&'a self, strict: bool) -> ValResult<GenericCollection<'a>> {
130130
if strict {
131131
self.strict_list()
132132
} else {
133133
self.lax_list()
134134
}
135135
}
136-
fn strict_list(&'a self) -> ValResult<GenericListLike<'a>>;
136+
fn strict_list(&'a self) -> ValResult<GenericCollection<'a>>;
137137
#[cfg_attr(has_no_coverage, no_coverage)]
138-
fn lax_list(&'a self) -> ValResult<GenericListLike<'a>> {
138+
fn lax_list(&'a self) -> ValResult<GenericCollection<'a>> {
139139
self.strict_list()
140140
}
141141

142-
fn validate_tuple(&'a self, strict: bool) -> ValResult<GenericListLike<'a>> {
142+
fn validate_tuple(&'a self, strict: bool) -> ValResult<GenericCollection<'a>> {
143143
if strict {
144144
self.strict_tuple()
145145
} else {
146146
self.lax_tuple()
147147
}
148148
}
149-
fn strict_tuple(&'a self) -> ValResult<GenericListLike<'a>>;
149+
fn strict_tuple(&'a self) -> ValResult<GenericCollection<'a>>;
150150
#[cfg_attr(has_no_coverage, no_coverage)]
151-
fn lax_tuple(&'a self) -> ValResult<GenericListLike<'a>> {
151+
fn lax_tuple(&'a self) -> ValResult<GenericCollection<'a>> {
152152
self.strict_tuple()
153153
}
154154

155-
fn validate_set(&'a self, strict: bool) -> ValResult<GenericListLike<'a>> {
155+
fn validate_set(&'a self, strict: bool) -> ValResult<GenericCollection<'a>> {
156156
if strict {
157157
self.strict_set()
158158
} else {
159159
self.lax_set()
160160
}
161161
}
162-
fn strict_set(&'a self) -> ValResult<GenericListLike<'a>>;
162+
fn strict_set(&'a self) -> ValResult<GenericCollection<'a>>;
163163
#[cfg_attr(has_no_coverage, no_coverage)]
164-
fn lax_set(&'a self) -> ValResult<GenericListLike<'a>> {
164+
fn lax_set(&'a self) -> ValResult<GenericCollection<'a>> {
165165
self.strict_set()
166166
}
167167

168-
fn validate_frozenset(&'a self, strict: bool) -> ValResult<GenericListLike<'a>> {
168+
fn validate_frozenset(&'a self, strict: bool) -> ValResult<GenericCollection<'a>> {
169169
if strict {
170170
self.strict_frozenset()
171171
} else {
172172
self.lax_frozenset()
173173
}
174174
}
175-
fn strict_frozenset(&'a self) -> ValResult<GenericListLike<'a>>;
175+
fn strict_frozenset(&'a self) -> ValResult<GenericCollection<'a>>;
176176
#[cfg_attr(has_no_coverage, no_coverage)]
177-
fn lax_frozenset(&'a self) -> ValResult<GenericListLike<'a>> {
177+
fn lax_frozenset(&'a self) -> ValResult<GenericCollection<'a>> {
178178
self.strict_frozenset()
179179
}
180180

src/input/input_json.rs

+17-17
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use super::datetime::{
66
};
77
use super::shared::{float_as_int, int_as_bool, str_as_bool, str_as_int};
88
use super::{
9-
EitherBytes, EitherString, EitherTimedelta, GenericArguments, GenericListLike, GenericMapping, Input, JsonArgs,
9+
EitherBytes, EitherString, EitherTimedelta, GenericArguments, GenericCollection, GenericMapping, Input, JsonArgs,
1010
JsonInput,
1111
};
1212

@@ -149,50 +149,50 @@ impl<'a> Input<'a> for JsonInput {
149149
self.validate_dict(false)
150150
}
151151

152-
fn validate_list(&'a self, _strict: bool) -> ValResult<GenericListLike<'a>> {
152+
fn validate_list(&'a self, _strict: bool) -> ValResult<GenericCollection<'a>> {
153153
match self {
154154
JsonInput::Array(a) => Ok(a.into()),
155155
_ => Err(ValError::new(ErrorKind::ListType, self)),
156156
}
157157
}
158158
#[cfg_attr(has_no_coverage, no_coverage)]
159-
fn strict_list(&'a self) -> ValResult<GenericListLike<'a>> {
159+
fn strict_list(&'a self) -> ValResult<GenericCollection<'a>> {
160160
self.validate_list(false)
161161
}
162162

163-
fn validate_tuple(&'a self, _strict: bool) -> ValResult<GenericListLike<'a>> {
163+
fn validate_tuple(&'a self, _strict: bool) -> ValResult<GenericCollection<'a>> {
164164
// just as in set's case, List has to be allowed
165165
match self {
166166
JsonInput::Array(a) => Ok(a.into()),
167167
_ => Err(ValError::new(ErrorKind::TupleType, self)),
168168
}
169169
}
170170
#[cfg_attr(has_no_coverage, no_coverage)]
171-
fn strict_tuple(&'a self) -> ValResult<GenericListLike<'a>> {
171+
fn strict_tuple(&'a self) -> ValResult<GenericCollection<'a>> {
172172
self.validate_tuple(false)
173173
}
174174

175-
fn validate_set(&'a self, _strict: bool) -> ValResult<GenericListLike<'a>> {
175+
fn validate_set(&'a self, _strict: bool) -> ValResult<GenericCollection<'a>> {
176176
// we allow a list here since otherwise it would be impossible to create a set from JSON
177177
match self {
178178
JsonInput::Array(a) => Ok(a.into()),
179179
_ => Err(ValError::new(ErrorKind::SetType, self)),
180180
}
181181
}
182182
#[cfg_attr(has_no_coverage, no_coverage)]
183-
fn strict_set(&'a self) -> ValResult<GenericListLike<'a>> {
183+
fn strict_set(&'a self) -> ValResult<GenericCollection<'a>> {
184184
self.validate_set(false)
185185
}
186186

187-
fn validate_frozenset(&'a self, _strict: bool) -> ValResult<GenericListLike<'a>> {
187+
fn validate_frozenset(&'a self, _strict: bool) -> ValResult<GenericCollection<'a>> {
188188
// we allow a list here since otherwise it would be impossible to create a frozenset from JSON
189189
match self {
190190
JsonInput::Array(a) => Ok(a.into()),
191191
_ => Err(ValError::new(ErrorKind::FrozenSetType, self)),
192192
}
193193
}
194194
#[cfg_attr(has_no_coverage, no_coverage)]
195-
fn strict_frozenset(&'a self) -> ValResult<GenericListLike<'a>> {
195+
fn strict_frozenset(&'a self) -> ValResult<GenericCollection<'a>> {
196196
self.validate_frozenset(false)
197197
}
198198

@@ -328,38 +328,38 @@ impl<'a> Input<'a> for String {
328328
}
329329

330330
#[cfg_attr(has_no_coverage, no_coverage)]
331-
fn validate_list(&'a self, _strict: bool) -> ValResult<GenericListLike<'a>> {
331+
fn validate_list(&'a self, _strict: bool) -> ValResult<GenericCollection<'a>> {
332332
Err(ValError::new(ErrorKind::ListType, self))
333333
}
334334
#[cfg_attr(has_no_coverage, no_coverage)]
335-
fn strict_list(&'a self) -> ValResult<GenericListLike<'a>> {
335+
fn strict_list(&'a self) -> ValResult<GenericCollection<'a>> {
336336
self.validate_list(false)
337337
}
338338

339339
#[cfg_attr(has_no_coverage, no_coverage)]
340-
fn validate_tuple(&'a self, _strict: bool) -> ValResult<GenericListLike<'a>> {
340+
fn validate_tuple(&'a self, _strict: bool) -> ValResult<GenericCollection<'a>> {
341341
Err(ValError::new(ErrorKind::TupleType, self))
342342
}
343343
#[cfg_attr(has_no_coverage, no_coverage)]
344-
fn strict_tuple(&'a self) -> ValResult<GenericListLike<'a>> {
344+
fn strict_tuple(&'a self) -> ValResult<GenericCollection<'a>> {
345345
self.validate_tuple(false)
346346
}
347347

348348
#[cfg_attr(has_no_coverage, no_coverage)]
349-
fn validate_set(&'a self, _strict: bool) -> ValResult<GenericListLike<'a>> {
349+
fn validate_set(&'a self, _strict: bool) -> ValResult<GenericCollection<'a>> {
350350
Err(ValError::new(ErrorKind::SetType, self))
351351
}
352352
#[cfg_attr(has_no_coverage, no_coverage)]
353-
fn strict_set(&'a self) -> ValResult<GenericListLike<'a>> {
353+
fn strict_set(&'a self) -> ValResult<GenericCollection<'a>> {
354354
self.validate_set(false)
355355
}
356356

357357
#[cfg_attr(has_no_coverage, no_coverage)]
358-
fn validate_frozenset(&'a self, _strict: bool) -> ValResult<GenericListLike<'a>> {
358+
fn validate_frozenset(&'a self, _strict: bool) -> ValResult<GenericCollection<'a>> {
359359
Err(ValError::new(ErrorKind::FrozenSetType, self))
360360
}
361361
#[cfg_attr(has_no_coverage, no_coverage)]
362-
fn strict_frozenset(&'a self) -> ValResult<GenericListLike<'a>> {
362+
fn strict_frozenset(&'a self) -> ValResult<GenericCollection<'a>> {
363363
self.validate_frozenset(false)
364364
}
365365

src/input/input_python.rs

+13-13
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use super::datetime::{
2020
};
2121
use super::shared::{float_as_int, int_as_bool, str_as_bool, str_as_int};
2222
use super::{
23-
py_string_str, repr_string, EitherBytes, EitherString, EitherTimedelta, GenericArguments, GenericListLike,
23+
py_string_str, repr_string, EitherBytes, EitherString, EitherTimedelta, GenericArguments, GenericCollection,
2424
GenericMapping, Input, PyArgs,
2525
};
2626

@@ -274,7 +274,7 @@ impl<'a> Input<'a> for PyAny {
274274
}
275275
}
276276

277-
fn strict_list(&'a self) -> ValResult<GenericListLike<'a>> {
277+
fn strict_list(&'a self) -> ValResult<GenericCollection<'a>> {
278278
if let Ok(list) = self.cast_as::<PyList>() {
279279
Ok(list.into())
280280
} else {
@@ -283,7 +283,7 @@ impl<'a> Input<'a> for PyAny {
283283
}
284284

285285
#[cfg(not(PyPy))]
286-
fn lax_list(&'a self) -> ValResult<GenericListLike<'a>> {
286+
fn lax_list(&'a self) -> ValResult<GenericCollection<'a>> {
287287
if let Ok(list) = self.cast_as::<PyList>() {
288288
Ok(list.into())
289289
} else if let Ok(tuple) = self.cast_as::<PyTuple>() {
@@ -296,7 +296,7 @@ impl<'a> Input<'a> for PyAny {
296296
}
297297

298298
#[cfg(PyPy)]
299-
fn lax_list(&'a self) -> ValResult<GenericListLike<'a>> {
299+
fn lax_list(&'a self) -> ValResult<GenericCollection<'a>> {
300300
if let Ok(list) = self.cast_as::<PyList>() {
301301
Ok(list.into())
302302
} else if let Ok(tuple) = self.cast_as::<PyTuple>() {
@@ -311,7 +311,7 @@ impl<'a> Input<'a> for PyAny {
311311
}
312312
}
313313

314-
fn strict_tuple(&'a self) -> ValResult<GenericListLike<'a>> {
314+
fn strict_tuple(&'a self) -> ValResult<GenericCollection<'a>> {
315315
if let Ok(tuple) = self.cast_as::<PyTuple>() {
316316
Ok(tuple.into())
317317
} else {
@@ -320,7 +320,7 @@ impl<'a> Input<'a> for PyAny {
320320
}
321321

322322
#[cfg(not(PyPy))]
323-
fn lax_tuple(&'a self) -> ValResult<GenericListLike<'a>> {
323+
fn lax_tuple(&'a self) -> ValResult<GenericCollection<'a>> {
324324
if let Ok(tuple) = self.cast_as::<PyTuple>() {
325325
Ok(tuple.into())
326326
} else if let Ok(list) = self.cast_as::<PyList>() {
@@ -333,7 +333,7 @@ impl<'a> Input<'a> for PyAny {
333333
}
334334

335335
#[cfg(PyPy)]
336-
fn lax_tuple(&'a self) -> ValResult<GenericListLike<'a>> {
336+
fn lax_tuple(&'a self) -> ValResult<GenericCollection<'a>> {
337337
if let Ok(tuple) = self.cast_as::<PyTuple>() {
338338
Ok(tuple.into())
339339
} else if let Ok(list) = self.cast_as::<PyList>() {
@@ -348,7 +348,7 @@ impl<'a> Input<'a> for PyAny {
348348
}
349349
}
350350

351-
fn strict_set(&'a self) -> ValResult<GenericListLike<'a>> {
351+
fn strict_set(&'a self) -> ValResult<GenericCollection<'a>> {
352352
if let Ok(set) = self.cast_as::<PySet>() {
353353
Ok(set.into())
354354
} else {
@@ -357,7 +357,7 @@ impl<'a> Input<'a> for PyAny {
357357
}
358358

359359
#[cfg(not(PyPy))]
360-
fn lax_set(&'a self) -> ValResult<GenericListLike<'a>> {
360+
fn lax_set(&'a self) -> ValResult<GenericCollection<'a>> {
361361
if let Ok(set) = self.cast_as::<PySet>() {
362362
Ok(set.into())
363363
} else if let Ok(list) = self.cast_as::<PyList>() {
@@ -374,7 +374,7 @@ impl<'a> Input<'a> for PyAny {
374374
}
375375

376376
#[cfg(PyPy)]
377-
fn lax_set(&'a self) -> ValResult<GenericListLike<'a>> {
377+
fn lax_set(&'a self) -> ValResult<GenericCollection<'a>> {
378378
if let Ok(set) = self.cast_as::<PySet>() {
379379
Ok(set.into())
380380
} else if let Ok(list) = self.cast_as::<PyList>() {
@@ -393,7 +393,7 @@ impl<'a> Input<'a> for PyAny {
393393
}
394394
}
395395

396-
fn strict_frozenset(&'a self) -> ValResult<GenericListLike<'a>> {
396+
fn strict_frozenset(&'a self) -> ValResult<GenericCollection<'a>> {
397397
if let Ok(set) = self.cast_as::<PyFrozenSet>() {
398398
Ok(set.into())
399399
} else {
@@ -402,7 +402,7 @@ impl<'a> Input<'a> for PyAny {
402402
}
403403

404404
#[cfg(not(PyPy))]
405-
fn lax_frozenset(&'a self) -> ValResult<GenericListLike<'a>> {
405+
fn lax_frozenset(&'a self) -> ValResult<GenericCollection<'a>> {
406406
if let Ok(frozen_set) = self.cast_as::<PyFrozenSet>() {
407407
Ok(frozen_set.into())
408408
} else if let Ok(set) = self.cast_as::<PySet>() {
@@ -419,7 +419,7 @@ impl<'a> Input<'a> for PyAny {
419419
}
420420

421421
#[cfg(PyPy)]
422-
fn lax_frozenset(&'a self) -> ValResult<GenericListLike<'a>> {
422+
fn lax_frozenset(&'a self) -> ValResult<GenericCollection<'a>> {
423423
if let Ok(frozen_set) = self.cast_as::<PyFrozenSet>() {
424424
Ok(frozen_set.into())
425425
} else if let Ok(set) = self.cast_as::<PySet>() {

src/input/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub use datetime::{EitherDate, EitherDateTime, EitherTime, EitherTimedelta};
1414
pub use input_abstract::Input;
1515
pub use parse_json::{JsonInput, JsonObject};
1616
pub use return_enums::{
17-
py_string_str, EitherBytes, EitherString, GenericArguments, GenericListLike, GenericMapping, JsonArgs, PyArgs,
17+
py_string_str, EitherBytes, EitherString, GenericArguments, GenericCollection, GenericMapping, JsonArgs, PyArgs,
1818
};
1919

2020
pub fn repr_string(v: &PyAny) -> PyResult<String> {

0 commit comments

Comments
 (0)