@@ -6,13 +6,13 @@ use pyo3::{intern, prelude::*};
6
6
7
7
use jiter:: JsonValue ;
8
8
9
- use crate :: errors:: { AsLocItem , InputValue , ValResult } ;
9
+ use crate :: errors:: { AsLocItem , ErrorTypeDefaults , InputValue , ValError , ValResult } ;
10
10
use crate :: tools:: py_err;
11
11
use crate :: { PyMultiHostUrl , PyUrl } ;
12
12
13
13
use super :: datetime:: { EitherDate , EitherDateTime , EitherTime , EitherTimedelta } ;
14
14
use super :: return_enums:: { EitherBytes , EitherInt , EitherString } ;
15
- use super :: { EitherFloat , GenericArguments , GenericIterable , GenericIterator , GenericMapping } ;
15
+ use super :: { EitherFloat , GenericArguments , GenericIterable , GenericIterator , GenericMapping , ValidationMatch } ;
16
16
17
17
#[ derive( Debug , Clone , Copy ) ]
18
18
pub enum InputType {
@@ -48,7 +48,7 @@ impl TryFrom<&str> for InputType {
48
48
/// the convention is to either implement:
49
49
/// * `strict_*` & `lax_*` if they have different behavior
50
50
/// * or, `validate_*` and `strict_*` to just call `validate_*` if the behavior for strict and lax is the same
51
- pub trait Input < ' a > : fmt:: Debug + ToPyObject + AsLocItem {
51
+ pub trait Input < ' a > : fmt:: Debug + ToPyObject + AsLocItem + Sized {
52
52
fn as_error_value ( & ' a self ) -> InputValue < ' a > ;
53
53
54
54
fn identity ( & self ) -> Option < usize > {
@@ -91,85 +91,37 @@ pub trait Input<'a>: fmt::Debug + ToPyObject + AsLocItem {
91
91
92
92
fn parse_json ( & ' a self ) -> ValResult < ' a , JsonValue > ;
93
93
94
- fn validate_str ( & ' a self , strict : bool , coerce_numbers_to_str : bool ) -> ValResult < EitherString < ' a > > {
95
- if strict {
96
- self . strict_str ( )
97
- } else {
98
- self . lax_str ( coerce_numbers_to_str)
99
- }
100
- }
101
- fn strict_str ( & ' a self ) -> ValResult < EitherString < ' a > > ;
102
- #[ cfg_attr( has_coverage_attribute, coverage( off) ) ]
103
- fn lax_str ( & ' a self , _coerce_numbers_to_str : bool ) -> ValResult < EitherString < ' a > > {
104
- self . strict_str ( )
105
- }
94
+ fn validate_str (
95
+ & ' a self ,
96
+ strict : bool ,
97
+ coerce_numbers_to_str : bool ,
98
+ ) -> ValResult < ValidationMatch < EitherString < ' a > > > ;
106
99
107
- fn validate_bytes ( & ' a self , strict : bool ) -> ValResult < EitherBytes < ' a > > {
108
- if strict {
109
- self . strict_bytes ( )
110
- } else {
111
- self . lax_bytes ( )
112
- }
113
- }
114
- fn strict_bytes ( & ' a self ) -> ValResult < EitherBytes < ' a > > ;
115
- #[ cfg_attr( has_coverage_attribute, coverage( off) ) ]
116
- fn lax_bytes ( & ' a self ) -> ValResult < EitherBytes < ' a > > {
117
- self . strict_bytes ( )
118
- }
100
+ fn validate_bytes ( & ' a self , strict : bool ) -> ValResult < ValidationMatch < EitherBytes < ' a > > > ;
119
101
120
- fn validate_bool ( & self , strict : bool ) -> ValResult < bool > {
121
- if strict {
122
- self . strict_bool ( )
123
- } else {
124
- self . lax_bool ( )
125
- }
126
- }
127
- fn strict_bool ( & self ) -> ValResult < bool > ;
128
- #[ cfg_attr( has_coverage_attribute, coverage( off) ) ]
129
- fn lax_bool ( & self ) -> ValResult < bool > {
130
- self . strict_bool ( )
131
- }
102
+ fn validate_bool ( & self , strict : bool ) -> ValResult < ' _ , ValidationMatch < bool > > ;
132
103
133
- fn validate_int ( & ' a self , strict : bool ) -> ValResult < EitherInt < ' a > > {
134
- if strict {
135
- self . strict_int ( )
136
- } else {
137
- self . lax_int ( )
138
- }
139
- }
140
- fn strict_int ( & ' a self ) -> ValResult < EitherInt < ' a > > ;
141
- #[ cfg_attr( has_coverage_attribute, coverage( off) ) ]
142
- fn lax_int ( & ' a self ) -> ValResult < EitherInt < ' a > > {
143
- self . strict_int ( )
144
- }
104
+ fn validate_int ( & ' a self , strict : bool ) -> ValResult < ' a , ValidationMatch < EitherInt < ' a > > > ;
145
105
146
- /// Extract an EitherInt from the input, only allowing exact
147
- /// matches for an Int (no subclasses)
148
106
fn exact_int ( & ' a self ) -> ValResult < EitherInt < ' a > > {
149
- self . strict_int ( )
107
+ self . validate_int ( true ) . and_then ( |val_match| {
108
+ val_match
109
+ . require_exact ( )
110
+ . ok_or_else ( || ValError :: new ( ErrorTypeDefaults :: IntType , self ) )
111
+ } )
150
112
}
151
113
152
114
/// Extract a String from the input, only allowing exact
153
115
/// matches for a String (no subclasses)
154
116
fn exact_str ( & ' a self ) -> ValResult < EitherString < ' a > > {
155
- self . strict_str ( )
117
+ self . validate_str ( true , false ) . and_then ( |val_match| {
118
+ val_match
119
+ . require_exact ( )
120
+ . ok_or_else ( || ValError :: new ( ErrorTypeDefaults :: StringType , self ) )
121
+ } )
156
122
}
157
123
158
- fn validate_float ( & ' a self , strict : bool , ultra_strict : bool ) -> ValResult < EitherFloat < ' a > > {
159
- if ultra_strict {
160
- self . ultra_strict_float ( )
161
- } else if strict {
162
- self . strict_float ( )
163
- } else {
164
- self . lax_float ( )
165
- }
166
- }
167
- fn ultra_strict_float ( & ' a self ) -> ValResult < EitherFloat < ' a > > ;
168
- fn strict_float ( & ' a self ) -> ValResult < EitherFloat < ' a > > ;
169
- #[ cfg_attr( has_coverage_attribute, coverage( off) ) ]
170
- fn lax_float ( & ' a self ) -> ValResult < EitherFloat < ' a > > {
171
- self . strict_float ( )
172
- }
124
+ fn validate_float ( & ' a self , strict : bool ) -> ValResult < ' a , ValidationMatch < EitherFloat < ' a > > > ;
173
125
174
126
fn validate_decimal ( & ' a self , strict : bool , py : Python < ' a > ) -> ValResult < & ' a PyAny > {
175
127
if strict {
@@ -257,87 +209,25 @@ pub trait Input<'a>: fmt::Debug + ToPyObject + AsLocItem {
257
209
258
210
fn validate_iter ( & self ) -> ValResult < GenericIterator > ;
259
211
260
- fn validate_date ( & self , strict : bool ) -> ValResult < EitherDate > {
261
- if strict {
262
- self . strict_date ( )
263
- } else {
264
- self . lax_date ( )
265
- }
266
- }
267
- fn strict_date ( & self ) -> ValResult < EitherDate > ;
268
- #[ cfg_attr( has_coverage_attribute, coverage( off) ) ]
269
- fn lax_date ( & self ) -> ValResult < EitherDate > {
270
- self . strict_date ( )
271
- }
212
+ fn validate_date ( & self , strict : bool ) -> ValResult < ValidationMatch < EitherDate > > ;
272
213
273
214
fn validate_time (
274
215
& self ,
275
216
strict : bool ,
276
217
microseconds_overflow_behavior : speedate:: MicrosecondsPrecisionOverflowBehavior ,
277
- ) -> ValResult < EitherTime > {
278
- if strict {
279
- self . strict_time ( microseconds_overflow_behavior)
280
- } else {
281
- self . lax_time ( microseconds_overflow_behavior)
282
- }
283
- }
284
- fn strict_time (
285
- & self ,
286
- microseconds_overflow_behavior : speedate:: MicrosecondsPrecisionOverflowBehavior ,
287
- ) -> ValResult < EitherTime > ;
288
- #[ cfg_attr( has_coverage_attribute, coverage( off) ) ]
289
- fn lax_time (
290
- & self ,
291
- microseconds_overflow_behavior : speedate:: MicrosecondsPrecisionOverflowBehavior ,
292
- ) -> ValResult < EitherTime > {
293
- self . strict_time ( microseconds_overflow_behavior)
294
- }
218
+ ) -> ValResult < ValidationMatch < EitherTime > > ;
295
219
296
220
fn validate_datetime (
297
221
& self ,
298
222
strict : bool ,
299
223
microseconds_overflow_behavior : speedate:: MicrosecondsPrecisionOverflowBehavior ,
300
- ) -> ValResult < EitherDateTime > {
301
- if strict {
302
- self . strict_datetime ( microseconds_overflow_behavior)
303
- } else {
304
- self . lax_datetime ( microseconds_overflow_behavior)
305
- }
306
- }
307
- fn strict_datetime (
308
- & self ,
309
- microseconds_overflow_behavior : speedate:: MicrosecondsPrecisionOverflowBehavior ,
310
- ) -> ValResult < EitherDateTime > ;
311
- #[ cfg_attr( has_coverage_attribute, coverage( off) ) ]
312
- fn lax_datetime (
313
- & self ,
314
- microseconds_overflow_behavior : speedate:: MicrosecondsPrecisionOverflowBehavior ,
315
- ) -> ValResult < EitherDateTime > {
316
- self . strict_datetime ( microseconds_overflow_behavior)
317
- }
224
+ ) -> ValResult < ValidationMatch < EitherDateTime > > ;
318
225
319
226
fn validate_timedelta (
320
227
& self ,
321
228
strict : bool ,
322
229
microseconds_overflow_behavior : speedate:: MicrosecondsPrecisionOverflowBehavior ,
323
- ) -> ValResult < EitherTimedelta > {
324
- if strict {
325
- self . strict_timedelta ( microseconds_overflow_behavior)
326
- } else {
327
- self . lax_timedelta ( microseconds_overflow_behavior)
328
- }
329
- }
330
- fn strict_timedelta (
331
- & self ,
332
- microseconds_overflow_behavior : speedate:: MicrosecondsPrecisionOverflowBehavior ,
333
- ) -> ValResult < EitherTimedelta > ;
334
- #[ cfg_attr( has_coverage_attribute, coverage( off) ) ]
335
- fn lax_timedelta (
336
- & self ,
337
- microseconds_overflow_behavior : speedate:: MicrosecondsPrecisionOverflowBehavior ,
338
- ) -> ValResult < EitherTimedelta > {
339
- self . strict_timedelta ( microseconds_overflow_behavior)
340
- }
230
+ ) -> ValResult < ValidationMatch < EitherTimedelta > > ;
341
231
}
342
232
343
233
/// The problem to solve here is that iterating a `StringMapping` returns an owned
0 commit comments