@@ -47,20 +47,26 @@ pub fn pydate_as_date(py_date: &Bound<'_, PyAny>) -> PyResult<Date> {
47
47
} )
48
48
}
49
49
50
- impl < ' py > IntoPyObject < ' py > for EitherDate < ' py > {
51
- type Target = PyDate ;
52
- type Output = Bound < ' py , PyDate > ;
53
- type Error = PyErr ;
54
-
55
- fn into_pyobject ( self , py : Python < ' py > ) -> PyResult < Self :: Output > {
50
+ impl < ' py > EitherDate < ' py > {
51
+ pub fn try_into_py ( & self , py : Python < ' py > , input : & ( impl Input < ' py > + ?Sized ) ) -> ValResult < PyObject > {
56
52
match self {
57
- Self :: Raw ( date) => PyDate :: new ( py, date. year . into ( ) , date. month , date. day ) ,
58
- Self :: Py ( date) => Ok ( date) ,
53
+ Self :: Raw ( date) => {
54
+ if date. year == 0 {
55
+ return Err ( ValError :: new (
56
+ ErrorType :: DateParsing {
57
+ error : Cow :: Borrowed ( "year 0 is out of range" ) ,
58
+ context : None ,
59
+ } ,
60
+ input,
61
+ ) ) ;
62
+ } ;
63
+ let py_date = PyDate :: new ( py, date. year . into ( ) , date. month , date. day ) ?;
64
+ Ok ( py_date. into ( ) )
65
+ }
66
+ Self :: Py ( py_date) => Ok ( py_date. clone ( ) . into ( ) ) ,
59
67
}
60
68
}
61
- }
62
69
63
- impl EitherDate < ' _ > {
64
70
pub fn as_raw ( & self ) -> PyResult < Date > {
65
71
match self {
66
72
Self :: Raw ( date) => Ok ( date. clone ( ) ) ,
@@ -278,30 +284,36 @@ pub fn pydatetime_as_datetime(py_dt: &Bound<'_, PyAny>) -> PyResult<DateTime> {
278
284
} )
279
285
}
280
286
281
- impl < ' py > IntoPyObject < ' py > for EitherDateTime < ' py > {
282
- type Target = PyDateTime ;
283
- type Output = Bound < ' py , PyDateTime > ;
284
- type Error = PyErr ;
285
-
286
- fn into_pyobject ( self , py : Python < ' py > ) -> PyResult < Self :: Output > {
287
+ impl < ' py > EitherDateTime < ' py > {
288
+ pub fn try_into_py ( & self , py : Python < ' py > , input : & ( impl Input < ' py > + ?Sized ) ) -> ValResult < PyObject > {
287
289
match self {
288
- Self :: Raw ( dt) => PyDateTime :: new (
289
- py,
290
- dt. date . year . into ( ) ,
291
- dt. date . month ,
292
- dt. date . day ,
293
- dt. time . hour ,
294
- dt. time . minute ,
295
- dt. time . second ,
296
- dt. time . microsecond ,
297
- time_as_tzinfo ( py, & dt. time ) ?. as_ref ( ) ,
298
- ) ,
299
- Self :: Py ( dt) => Ok ( dt) ,
290
+ Self :: Raw ( dt) => {
291
+ if dt. date . year == 0 {
292
+ return Err ( ValError :: new (
293
+ ErrorType :: DatetimeParsing {
294
+ error : Cow :: Borrowed ( "year 0 is out of range" ) ,
295
+ context : None ,
296
+ } ,
297
+ input,
298
+ ) ) ;
299
+ } ;
300
+ let py_dt = PyDateTime :: new (
301
+ py,
302
+ dt. date . year . into ( ) ,
303
+ dt. date . month ,
304
+ dt. date . day ,
305
+ dt. time . hour ,
306
+ dt. time . minute ,
307
+ dt. time . second ,
308
+ dt. time . microsecond ,
309
+ time_as_tzinfo ( py, & dt. time ) ?. as_ref ( ) ,
310
+ ) ?;
311
+ Ok ( py_dt. into ( ) )
312
+ }
313
+ Self :: Py ( py_dt) => Ok ( py_dt. clone ( ) . into ( ) ) ,
300
314
}
301
315
}
302
- }
303
316
304
- impl EitherDateTime < ' _ > {
305
317
pub fn as_raw ( & self ) -> PyResult < DateTime > {
306
318
match self {
307
319
Self :: Raw ( dt) => Ok ( dt. clone ( ) ) ,
0 commit comments