@@ -24,7 +24,7 @@ pub mod consts;
2424mod line_proto_term;
2525pub mod read_query;
2626pub mod write_query;
27- use std:: fmt;
27+ use std:: { convert :: Infallible , fmt} ;
2828
2929use crate :: { Error , WriteQuery } ;
3030use consts:: {
@@ -70,48 +70,55 @@ impl fmt::Display for Timestamp {
7070}
7171
7272#[ cfg( feature = "chrono" ) ]
73- impl From < Timestamp > for chrono:: DateTime < chrono:: Utc > {
74- fn from ( ts : Timestamp ) -> chrono:: DateTime < chrono:: Utc > {
73+ impl TryFrom < Timestamp > for chrono:: DateTime < chrono:: Utc > {
74+ type Error = <i64 as TryFrom < u128 > >:: Error ;
75+
76+ fn try_from ( ts : Timestamp ) -> Result < Self , Self :: Error > {
7577 use chrono:: TimeZone as _;
76- chrono:: Utc . timestamp_nanos ( ts. nanos ( ) as i64 )
78+ Ok ( chrono:: Utc . timestamp_nanos ( ts. nanos ( ) . try_into ( ) ? ) )
7779 }
7880}
7981
8082#[ cfg( feature = "chrono" ) ]
81- impl < T > From < chrono:: DateTime < T > > for Timestamp
82- where
83- T : chrono:: TimeZone ,
84- {
85- fn from ( date_time : chrono:: DateTime < T > ) -> Self {
86- Timestamp :: Nanoseconds ( date_time. timestamp_nanos_opt ( ) . unwrap ( ) as u128 )
87- }
88- }
83+ impl TryFrom < chrono:: DateTime < chrono:: Utc > > for Timestamp {
84+ type Error = crate :: error:: TimeTryFromError <
85+ crate :: error:: TimestampTooLargeError ,
86+ <u128 as TryFrom < i64 > >:: Error ,
87+ > ;
8988
90- #[ cfg( feature = "time" ) ]
91- impl From < Timestamp > for time:: UtcDateTime {
92- fn from ( value : Timestamp ) -> Self {
93- time:: UtcDateTime :: from_unix_timestamp_nanos ( value. nanos ( ) as i128 ) . unwrap ( )
89+ fn try_from ( dt : chrono:: DateTime < chrono:: Utc > ) -> Result < Self , Self :: Error > {
90+ // unfortunately chrono doesn't give us the nanos as i128, so we have to error
91+ // if it doesn't fit and then cast the i64 to u128 anyways
92+ let nanos = dt
93+ . timestamp_nanos_opt ( )
94+ . ok_or ( Self :: Error :: TimeError (
95+ crate :: error:: TimestampTooLargeError ( ( ) ) ,
96+ ) ) ?
97+ . try_into ( )
98+ . map_err ( Self :: Error :: IntError ) ?;
99+ Ok ( Self :: Nanoseconds ( nanos) )
94100 }
95101}
96102
97103#[ cfg( feature = "time" ) ]
98- impl From < time:: UtcDateTime > for Timestamp {
99- fn from ( value : time:: UtcDateTime ) -> Self {
100- Timestamp :: Nanoseconds ( value. unix_timestamp_nanos ( ) as u128 )
101- }
102- }
104+ impl TryFrom < Timestamp > for time:: UtcDateTime {
105+ type Error =
106+ crate :: error:: TimeTryFromError < time:: error:: ComponentRange , <i128 as TryFrom < u128 > >:: Error > ;
103107
104- #[ cfg( feature = "time" ) ]
105- impl From < Timestamp > for time:: OffsetDateTime {
106- fn from ( value : Timestamp ) -> Self {
107- time:: OffsetDateTime :: from_unix_timestamp_nanos ( value. nanos ( ) as i128 ) . unwrap ( )
108+ fn try_from ( value : Timestamp ) -> Result < Self , Self :: Error > {
109+ let nanos = value. nanos ( ) . try_into ( ) . map_err ( Self :: Error :: IntError ) ?;
110+ time:: UtcDateTime :: from_unix_timestamp_nanos ( nanos) . map_err ( Self :: Error :: TimeError )
108111 }
109112}
110113
111114#[ cfg( feature = "time" ) ]
112- impl From < time:: OffsetDateTime > for Timestamp {
113- fn from ( value : time:: OffsetDateTime ) -> Self {
114- Timestamp :: Nanoseconds ( value. unix_timestamp_nanos ( ) as u128 )
115+ impl TryFrom < time:: UtcDateTime > for Timestamp {
116+ type Error = <u128 as TryFrom < i128 > >:: Error ;
117+
118+ fn try_from ( value : time:: UtcDateTime ) -> Result < Self , Self :: Error > {
119+ Ok ( Timestamp :: Nanoseconds (
120+ value. unix_timestamp_nanos ( ) . try_into ( ) ?,
121+ ) )
115122 }
116123}
117124
@@ -186,12 +193,16 @@ impl<Q: Query> Query for Box<Q> {
186193}
187194
188195pub trait InfluxDbWriteable {
189- fn into_query < I : Into < String > > ( self , name : I ) -> WriteQuery ;
196+ type Error ;
197+
198+ fn try_into_query < I : Into < String > > ( self , name : I ) -> Result < WriteQuery , Self :: Error > ;
190199}
191200
192201impl InfluxDbWriteable for Timestamp {
193- fn into_query < I : Into < String > > ( self , name : I ) -> WriteQuery {
194- WriteQuery :: new ( self , name. into ( ) )
202+ type Error = Infallible ;
203+
204+ fn try_into_query < I : Into < String > > ( self , name : I ) -> Result < WriteQuery , Infallible > {
205+ Ok ( WriteQuery :: new ( self , name. into ( ) ) )
195206 }
196207}
197208
@@ -232,30 +243,35 @@ pub enum QueryType {
232243
233244#[ cfg( test) ]
234245mod tests {
246+ #[ cfg( feature = "chrono" ) ]
235247 use super :: consts:: {
236248 MILLIS_PER_SECOND , MINUTES_PER_HOUR , NANOS_PER_MICRO , NANOS_PER_MILLI , SECONDS_PER_MINUTE ,
237249 } ;
238250 use crate :: query:: { Timestamp , ValidQuery } ;
251+
239252 #[ test]
240253 fn test_equality_str ( ) {
241254 assert_eq ! ( ValidQuery :: from( "hello" ) , "hello" ) ;
242255 }
256+
243257 #[ test]
244258 fn test_equality_string ( ) {
245259 assert_eq ! (
246260 ValidQuery :: from( String :: from( "hello" ) ) ,
247261 String :: from( "hello" )
248262 ) ;
249263 }
264+
250265 #[ test]
251266 fn test_format_for_timestamp_else ( ) {
252267 assert ! ( format!( "{}" , Timestamp :: Nanoseconds ( 100 ) ) == "100" ) ;
253268 }
269+
254270 #[ cfg( feature = "chrono" ) ]
255271 #[ test]
256272 fn test_chrono_datetime_from_timestamp_hours ( ) {
257273 use chrono:: prelude:: * ;
258- let datetime_from_timestamp: DateTime < Utc > = Timestamp :: Hours ( 2 ) . into ( ) ;
274+ let datetime_from_timestamp: DateTime < Utc > = Timestamp :: Hours ( 2 ) . try_into ( ) . unwrap ( ) ;
259275 assert_eq ! (
260276 Utc . timestamp_nanos(
261277 ( 2 * MINUTES_PER_HOUR * SECONDS_PER_MINUTE * MILLIS_PER_SECOND * NANOS_PER_MILLI )
@@ -265,11 +281,12 @@ mod tests {
265281 datetime_from_timestamp
266282 )
267283 }
284+
268285 #[ cfg( feature = "chrono" ) ]
269286 #[ test]
270287 fn test_chrono_datetime_from_timestamp_minutes ( ) {
271288 use chrono:: prelude:: * ;
272- let datetime_from_timestamp: DateTime < Utc > = Timestamp :: Minutes ( 2 ) . into ( ) ;
289+ let datetime_from_timestamp: DateTime < Utc > = Timestamp :: Minutes ( 2 ) . try_into ( ) . unwrap ( ) ;
273290 assert_eq ! (
274291 Utc . timestamp_nanos(
275292 ( 2 * SECONDS_PER_MINUTE * MILLIS_PER_SECOND * NANOS_PER_MILLI )
@@ -279,11 +296,12 @@ mod tests {
279296 datetime_from_timestamp
280297 )
281298 }
299+
282300 #[ cfg( feature = "chrono" ) ]
283301 #[ test]
284302 fn test_chrono_datetime_from_timestamp_seconds ( ) {
285303 use chrono:: prelude:: * ;
286- let datetime_from_timestamp: DateTime < Utc > = Timestamp :: Seconds ( 2 ) . into ( ) ;
304+ let datetime_from_timestamp: DateTime < Utc > = Timestamp :: Seconds ( 2 ) . try_into ( ) . unwrap ( ) ;
287305 assert_eq ! (
288306 Utc . timestamp_nanos(
289307 ( 2 * MILLIS_PER_SECOND * NANOS_PER_MILLI )
@@ -293,33 +311,37 @@ mod tests {
293311 datetime_from_timestamp
294312 )
295313 }
314+
296315 #[ cfg( feature = "chrono" ) ]
297316 #[ test]
298317 fn test_chrono_datetime_from_timestamp_millis ( ) {
299318 use chrono:: prelude:: * ;
300- let datetime_from_timestamp: DateTime < Utc > = Timestamp :: Milliseconds ( 2 ) . into ( ) ;
319+ let datetime_from_timestamp: DateTime < Utc > = Timestamp :: Milliseconds ( 2 ) . try_into ( ) . unwrap ( ) ;
301320 assert_eq ! (
302321 Utc . timestamp_nanos( ( 2 * NANOS_PER_MILLI ) . try_into( ) . unwrap( ) ) ,
303322 datetime_from_timestamp
304323 )
305324 }
325+
306326 #[ cfg( feature = "chrono" ) ]
307327 #[ test]
308328 fn test_chrono_datetime_from_timestamp_nanos ( ) {
309329 use chrono:: prelude:: * ;
310- let datetime_from_timestamp: DateTime < Utc > = Timestamp :: Nanoseconds ( 1 ) . into ( ) ;
330+ let datetime_from_timestamp: DateTime < Utc > = Timestamp :: Nanoseconds ( 1 ) . try_into ( ) . unwrap ( ) ;
311331 assert_eq ! ( Utc . timestamp_nanos( 1 ) , datetime_from_timestamp)
312332 }
333+
313334 #[ cfg( feature = "chrono" ) ]
314335 #[ test]
315336 fn test_chrono_datetime_from_timestamp_micros ( ) {
316337 use chrono:: prelude:: * ;
317- let datetime_from_timestamp: DateTime < Utc > = Timestamp :: Microseconds ( 2 ) . into ( ) ;
338+ let datetime_from_timestamp: DateTime < Utc > = Timestamp :: Microseconds ( 2 ) . try_into ( ) . unwrap ( ) ;
318339 assert_eq ! (
319340 Utc . timestamp_nanos( ( 2 * NANOS_PER_MICRO ) . try_into( ) . unwrap( ) ) ,
320341 datetime_from_timestamp
321342 )
322343 }
344+
323345 #[ cfg( feature = "chrono" ) ]
324346 #[ test]
325347 fn test_timestamp_from_chrono_date ( ) {
@@ -328,7 +350,8 @@ mod tests {
328350 . with_ymd_and_hms ( 1970 , 1 , 1 , 0 , 0 , 1 )
329351 . single ( )
330352 . unwrap ( )
331- . into ( ) ;
353+ . try_into ( )
354+ . unwrap ( ) ;
332355 assert_eq ! (
333356 Timestamp :: Nanoseconds ( MILLIS_PER_SECOND * NANOS_PER_MILLI ) ,
334357 timestamp_from_datetime
0 commit comments