@@ -145,20 +145,20 @@ use serde_with::{DeserializeAs, SerializeAs};
145145/// }
146146/// # }
147147/// ```
148- /// ### The `serde_with` feature flag
148+ /// ### The `serde_with-3 ` feature flag
149149///
150- /// The `serde_with` feature can be enabled to support more ergonomic serde attributes for
150+ /// The `serde_with-3 ` feature can be enabled to support more ergonomic serde attributes for
151151/// (de)serializing [`chrono::DateTime`] from/to BSON via the [`serde_with`](https://docs.rs/serde_with/1.11.0/serde_with/)
152- /// crate. The main benefit of this compared to the regular `serde_helpers` is that `serde_with` can
153- /// handle nested [`chrono::DateTime`] values (e.g. in [`Option`]), whereas the former only works on
154- /// fields that are exactly [`chrono::DateTime`].
152+ /// crate. The main benefit of this compared to the regular `serde_helpers` is that `serde_with-3`
153+ /// can handle nested [`chrono::DateTime`] values (e.g. in [`Option`]), whereas the former only
154+ /// works on fields that are exactly [`chrono::DateTime`].
155155/// ```
156- /// # #[cfg(all(feature = "chrono-0_4", feature = "serde_with"))]
156+ /// # #[cfg(all(feature = "chrono-0_4", feature = "serde_with-3 "))]
157157/// # {
158158/// use serde::{Deserialize, Serialize};
159159/// use bson::doc;
160160///
161- /// #[serde_with ::serde_as]
161+ /// #[serde_with_3 ::serde_as]
162162/// #[derive(Deserialize, Serialize, PartialEq, Debug)]
163163/// struct Foo {
164164/// /// Serializes as a BSON datetime rather than using [`chrono::DateTime`]'s serialization
@@ -482,6 +482,36 @@ impl SerializeAs<chrono::DateTime<Utc>> for crate::DateTime {
482482 }
483483}
484484
485+ #[ cfg( all( feature = "chrono-0_4" , feature = "serde_with-3" ) ) ]
486+ #[ cfg_attr(
487+ docsrs,
488+ doc( cfg( all( feature = "chrono-0_4" , feature = "serde_with-3" ) ) )
489+ ) ]
490+ impl < ' de > serde_with_3:: DeserializeAs < ' de , chrono:: DateTime < Utc > > for crate :: DateTime {
491+ fn deserialize_as < D > ( deserializer : D ) -> std:: result:: Result < chrono:: DateTime < Utc > , D :: Error >
492+ where
493+ D : Deserializer < ' de > ,
494+ {
495+ let dt = DateTime :: deserialize ( deserializer) ?;
496+ Ok ( dt. to_chrono ( ) )
497+ }
498+ }
499+
500+ #[ cfg( all( feature = "chrono-0_4" , feature = "serde_with-3" ) ) ]
501+ #[ cfg_attr( docsrs, doc( cfg( all( feature = "chrono-0_4" , feature = "chrono-0_4" ) ) ) ) ]
502+ impl serde_with_3:: SerializeAs < chrono:: DateTime < Utc > > for crate :: DateTime {
503+ fn serialize_as < S > (
504+ source : & chrono:: DateTime < Utc > ,
505+ serializer : S ,
506+ ) -> std:: result:: Result < S :: Ok , S :: Error >
507+ where
508+ S : serde:: Serializer ,
509+ {
510+ let dt = DateTime :: from_chrono ( * source) ;
511+ dt. serialize ( serializer)
512+ }
513+ }
514+
485515#[ cfg( feature = "time-0_3" ) ]
486516#[ cfg_attr( docsrs, doc( cfg( feature = "time-0_3" ) ) ) ]
487517impl From < crate :: DateTime > for time:: OffsetDateTime {
@@ -525,6 +555,33 @@ impl SerializeAs<time::OffsetDateTime> for crate::DateTime {
525555 }
526556}
527557
558+ #[ cfg( all( feature = "time-0_3" , feature = "serde_with-3" ) ) ]
559+ #[ cfg_attr( docsrs, doc( cfg( all( feature = "time-0_3" , feature = "serde_with-3" ) ) ) ) ]
560+ impl < ' de > serde_with_3:: DeserializeAs < ' de , time:: OffsetDateTime > for crate :: DateTime {
561+ fn deserialize_as < D > ( deserializer : D ) -> std:: result:: Result < time:: OffsetDateTime , D :: Error >
562+ where
563+ D : Deserializer < ' de > ,
564+ {
565+ let dt = DateTime :: deserialize ( deserializer) ?;
566+ Ok ( dt. to_time_0_3 ( ) )
567+ }
568+ }
569+
570+ #[ cfg( all( feature = "time-0_3" , feature = "serde_with-3" ) ) ]
571+ #[ cfg_attr( docsrs, doc( cfg( all( feature = "time-0_3" , feature = "chrono-0_4" ) ) ) ) ]
572+ impl serde_with_3:: SerializeAs < time:: OffsetDateTime > for crate :: DateTime {
573+ fn serialize_as < S > (
574+ source : & time:: OffsetDateTime ,
575+ serializer : S ,
576+ ) -> std:: result:: Result < S :: Ok , S :: Error >
577+ where
578+ S : serde:: Serializer ,
579+ {
580+ let dt = DateTime :: from_time_0_3 ( * source) ;
581+ dt. serialize ( serializer)
582+ }
583+ }
584+
528585/// Errors that can occur during [`DateTime`] construction and generation.
529586#[ derive( Clone , Debug ) ]
530587#[ non_exhaustive]
0 commit comments