@@ -45,21 +45,32 @@ pub fn pydate_as_date(py_date: &Bound<'_, PyAny>) -> PyResult<Date> {
4545 } )
4646}
4747
48- impl EitherDate < ' _ > {
48+ impl < ' py > EitherDate < ' py > {
49+ pub fn try_into_py ( self , py : Python < ' py > , input : & ( impl Input < ' py > + ?Sized ) ) -> ValResult < PyObject > {
50+ match self {
51+ Self :: Raw ( date) => {
52+ if date. year == 0 {
53+ return Err ( ValError :: new (
54+ ErrorType :: DateParsing {
55+ error : Cow :: Borrowed ( "year 0 is out of range" ) ,
56+ context : None ,
57+ } ,
58+ input,
59+ ) ) ;
60+ } ;
61+ let py_date = PyDate :: new_bound ( py, date. year . into ( ) , date. month , date. day ) ?;
62+ Ok ( py_date. into ( ) )
63+ }
64+ Self :: Py ( py_date) => Ok ( py_date. into ( ) ) ,
65+ }
66+ }
67+
4968 pub fn as_raw ( & self ) -> PyResult < Date > {
5069 match self {
5170 Self :: Raw ( date) => Ok ( date. clone ( ) ) ,
5271 Self :: Py ( py_date) => pydate_as_date ( py_date) ,
5372 }
5473 }
55-
56- pub fn try_into_py ( self , py : Python < ' _ > ) -> PyResult < PyObject > {
57- let date = match self {
58- Self :: Py ( date) => Ok ( date) ,
59- Self :: Raw ( date) => PyDate :: new_bound ( py, date. year . into ( ) , date. month , date. day ) ,
60- } ?;
61- Ok ( date. into_py ( py) )
62- }
6374}
6475
6576#[ cfg_attr( debug_assertions, derive( Debug ) ) ]
@@ -259,31 +270,42 @@ pub fn pydatetime_as_datetime(py_dt: &Bound<'_, PyAny>) -> PyResult<DateTime> {
259270 } )
260271}
261272
262- impl < ' a > EitherDateTime < ' a > {
273+ impl < ' py > EitherDateTime < ' py > {
274+ pub fn try_into_py ( self , py : Python < ' py > , input : & ( impl Input < ' py > + ?Sized ) ) -> ValResult < PyObject > {
275+ match self {
276+ Self :: Raw ( dt) => {
277+ if dt. date . year == 0 {
278+ return Err ( ValError :: new (
279+ ErrorType :: DatetimeParsing {
280+ error : Cow :: Borrowed ( "year 0 is out of range" ) ,
281+ context : None ,
282+ } ,
283+ input,
284+ ) ) ;
285+ } ;
286+ let py_dt = PyDateTime :: new_bound (
287+ py,
288+ dt. date . year . into ( ) ,
289+ dt. date . month ,
290+ dt. date . day ,
291+ dt. time . hour ,
292+ dt. time . minute ,
293+ dt. time . second ,
294+ dt. time . microsecond ,
295+ time_as_tzinfo ( py, & dt. time ) ?. as_ref ( ) ,
296+ ) ?;
297+ Ok ( py_dt. into ( ) )
298+ }
299+ Self :: Py ( py_dt) => Ok ( py_dt. into ( ) ) ,
300+ }
301+ }
302+
263303 pub fn as_raw ( & self ) -> PyResult < DateTime > {
264304 match self {
265305 Self :: Raw ( dt) => Ok ( dt. clone ( ) ) ,
266306 Self :: Py ( py_dt) => pydatetime_as_datetime ( py_dt) ,
267307 }
268308 }
269-
270- pub fn try_into_py ( self , py : Python < ' a > ) -> PyResult < PyObject > {
271- let dt = match self {
272- Self :: Raw ( datetime) => PyDateTime :: new_bound (
273- py,
274- datetime. date . year . into ( ) ,
275- datetime. date . month ,
276- datetime. date . day ,
277- datetime. time . hour ,
278- datetime. time . minute ,
279- datetime. time . second ,
280- datetime. time . microsecond ,
281- time_as_tzinfo ( py, & datetime. time ) ?. as_ref ( ) ,
282- ) ?,
283- Self :: Py ( dt) => dt. clone ( ) ,
284- } ;
285- Ok ( dt. into_py ( py) )
286- }
287309}
288310
289311pub fn bytes_as_date < ' py > ( input : & ( impl Input < ' py > + ?Sized ) , bytes : & [ u8 ] ) -> ValResult < EitherDate < ' py > > {
0 commit comments