1
1
use pyo3:: intern;
2
2
use pyo3:: prelude:: * ;
3
- use pyo3:: types:: { PyDelta , PyDict , PyString } ;
3
+ use pyo3:: types:: { PyDelta , PyDict } ;
4
4
use speedate:: Duration ;
5
5
6
6
use crate :: build_tools:: is_strict;
7
7
use crate :: errors:: { ErrorType , ValError , ValResult } ;
8
- use crate :: input:: { EitherTimedelta , Input } ;
8
+ use crate :: input:: { duration_as_pytimedelta , pytimedelta_as_duration , Input } ;
9
9
use crate :: recursion_guard:: RecursionGuard ;
10
10
use crate :: tools:: SchemaDict ;
11
11
@@ -24,6 +24,7 @@ struct TimedeltaConstraints {
24
24
ge : Option < Duration > ,
25
25
gt : Option < Duration > ,
26
26
}
27
+
27
28
impl BuildValidator for TimeDeltaValidator {
28
29
const EXPECTED_TYPE : & ' static str = "timedelta" ;
29
30
@@ -32,23 +33,29 @@ impl BuildValidator for TimeDeltaValidator {
32
33
config : Option < & PyDict > ,
33
34
_definitions : & mut DefinitionsBuilder < CombinedValidator > ,
34
35
) -> PyResult < CombinedValidator > {
35
- let py = schema. py ( ) ;
36
- let has_constraints = schema. get_item ( intern ! ( py, "le" ) ) . is_some ( )
37
- || schema. get_item ( intern ! ( py, "lt" ) ) . is_some ( )
38
- || schema. get_item ( intern ! ( py, "ge" ) ) . is_some ( )
39
- || schema. get_item ( intern ! ( py, "gt" ) ) . is_some ( ) ;
36
+ let py: Python < ' _ > = schema. py ( ) ;
37
+ let constraints = TimedeltaConstraints {
38
+ le : schema
39
+ . get_as :: < & PyDelta > ( intern ! ( py, "le" ) ) ?
40
+ . map ( pytimedelta_as_duration) ,
41
+ lt : schema
42
+ . get_as :: < & PyDelta > ( intern ! ( py, "lt" ) ) ?
43
+ . map ( pytimedelta_as_duration) ,
44
+ ge : schema
45
+ . get_as :: < & PyDelta > ( intern ! ( py, "ge" ) ) ?
46
+ . map ( pytimedelta_as_duration) ,
47
+ gt : schema
48
+ . get_as :: < & PyDelta > ( intern ! ( py, "gt" ) ) ?
49
+ . map ( pytimedelta_as_duration) ,
50
+ } ;
40
51
41
52
Ok ( Self {
42
53
strict : is_strict ( schema, config) ?,
43
- constraints : match has_constraints {
44
- true => Some ( TimedeltaConstraints {
45
- le : py_timedelta_as_timedelta ( schema, intern ! ( py, "le" ) ) ?,
46
- lt : py_timedelta_as_timedelta ( schema, intern ! ( py, "lt" ) ) ?,
47
- ge : py_timedelta_as_timedelta ( schema, intern ! ( py, "ge" ) ) ?,
48
- gt : py_timedelta_as_timedelta ( schema, intern ! ( py, "gt" ) ) ?,
49
- } ) ,
50
- false => None ,
51
- } ,
54
+ constraints : ( constraints. le . is_some ( )
55
+ || constraints. lt . is_some ( )
56
+ || constraints. ge . is_some ( )
57
+ || constraints. gt . is_some ( ) )
58
+ . then_some ( constraints) ,
52
59
}
53
60
. into ( ) )
54
61
}
@@ -64,6 +71,7 @@ impl Validator for TimeDeltaValidator {
64
71
_recursion_guard : & ' s mut RecursionGuard ,
65
72
) -> ValResult < ' data , PyObject > {
66
73
let timedelta = input. validate_timedelta ( extra. strict . unwrap_or ( self . strict ) ) ?;
74
+ let py_timedelta = timedelta. try_into_py ( py) ?;
67
75
if let Some ( constraints) = & self . constraints {
68
76
let raw_timedelta = timedelta. as_raw ( ) ;
69
77
@@ -73,9 +81,12 @@ impl Validator for TimeDeltaValidator {
73
81
if !raw_timedelta. $constraint( constraint) {
74
82
return Err ( ValError :: new(
75
83
ErrorType :: $error {
76
- $constraint: constraint. to_string( ) . into( ) ,
84
+ $constraint: duration_as_pytimedelta( py, constraint) ?
85
+ . repr( ) ?
86
+ . to_string( )
87
+ . into( ) ,
77
88
} ,
78
- input ,
89
+ py_timedelta . as_ref ( ) ,
79
90
) ) ;
80
91
}
81
92
}
@@ -87,7 +98,7 @@ impl Validator for TimeDeltaValidator {
87
98
check_constraint ! ( ge, GreaterThanEqual ) ;
88
99
check_constraint ! ( gt, GreaterThan ) ;
89
100
}
90
- Ok ( timedelta . try_into_py ( py ) ? )
101
+ Ok ( py_timedelta . into ( ) )
91
102
}
92
103
93
104
fn different_strict_behavior (
@@ -106,10 +117,3 @@ impl Validator for TimeDeltaValidator {
106
117
Ok ( ( ) )
107
118
}
108
119
}
109
-
110
- fn py_timedelta_as_timedelta ( schema : & PyDict , field : & PyString ) -> PyResult < Option < Duration > > {
111
- match schema. get_as :: < & PyDelta > ( field) ? {
112
- Some ( timedelta) => Ok ( Some ( EitherTimedelta :: Py ( timedelta) . as_raw ( ) ) ) ,
113
- None => Ok ( None ) ,
114
- }
115
- }
0 commit comments