Skip to content

Commit f14d6d1

Browse files
RUST-1748 Refactor BSON DateTime serde converters using serde_conv_doc macro (#570)
1 parent 7e8c859 commit f14d6d1

File tree

5 files changed

+494
-575
lines changed

5 files changed

+494
-575
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ provides a `DateTime` type, but its `Serialize` and `Deserialize` implementation
230230
instead, so when using it with BSON, the BSON datetime type is not used. To work around this, the
231231
`chrono-0_4` feature flag can be enabled. This flag exposes a number of convenient conversions
232232
between `bson::DateTime` and `chrono::DateTime`, including the
233-
[`bson_datetime::FromChronoDateTime`](https://docs.rs/bson/latest/bson/serde_helpers/bson_datetime/struct.FromChronoDateTime/index.html)
233+
[`datetime::FromChrono04DateTime`](https://docs.rs/bson/latest/bson/serde_helpers/datetime/struct.FromChrono04DateTime/index.html)
234234
serde helper, which can be used to (de)serialize `chrono::DateTime`s to/from BSON datetimes, and the
235235
`From<chrono::DateTime>` implementation for `Bson`, which allows `chrono::DateTime` values to be
236236
used in the `doc!` and `bson!` macros.
@@ -251,7 +251,7 @@ struct Foo {
251251

252252
// serializes as a BSON datetime.
253253
// this requires the "chrono-0_4" feature flag
254-
#[serde_as(as = "bson::serde_helpers::bson_datetime::FromChronoDateTime")]
254+
#[serde_as(as = "bson::serde_helpers::datetime::FromChrono04DateTime")]
255255
chrono_as_bson: chrono::DateTime<chrono::Utc>,
256256
}
257257

src/datetime.rs

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -108,20 +108,25 @@ use crate::error::{Error, Result};
108108
///
109109
/// ### `serde_helpers`
110110
/// The `bson` crate provides a number of useful helpers for serializing and deserializing
111-
/// various datetime types to and from different formats. For example, to serialize a
112-
/// [`chrono::DateTime`] as a BSON datetime, you can use
113-
/// [`crate::serde_helpers::bson_datetime::FromChronoDateTime`].
114-
/// Similarly, to serialize a BSON [`DateTime`] to a string, you can use
115-
/// [`crate::serde_helpers::bson_datetime::AsRfc3339String`]. Check out the
116-
/// [`crate::serde_helpers`] module documentation for a list of all of the helpers offered by the
111+
/// various datetime types to and from different formats using the [`serde_with`](https://docs.rs/serde_with/1.11.0/serde_with/)
117112
/// crate.
118113
///
114+
/// > **Note:** All helpers in this module require use of the [`#[serde_as]`](https://docs.rs/serde_with/latest/serde_with/attr.serde_as.html)
115+
/// > attribute on the struct. This enables the enhanced serialization behavior provided by
116+
/// > `serde_with-3`.
117+
///
118+
/// For example, to serialize a [`chrono::DateTime`] as a BSON datetime, you can use
119+
/// [`crate::serde_helpers::datetime::FromChrono04DateTime`].
120+
/// Similarly, to serialize a BSON [`DateTime`] to a string, you can use
121+
/// [`crate::serde_helpers::datetime::AsRfc3339String`]. Check out the
122+
/// [`crate::serde_helpers`] module documentation for a list of all of the helpers
123+
/// offered by the crate.
119124
/// ```rust
120-
/// # #[cfg(feature = "chrono-0_4")]
125+
/// # #[cfg(all(feature = "chrono-0_4", feature = "serde_with-3"))]
121126
/// # {
122127
/// use serde::{Serialize, Deserialize};
123128
/// use serde_with::serde_as;
124-
/// use bson::serde_helpers::bson_datetime;
129+
/// use bson::serde_helpers::datetime;
125130
///
126131
/// #[serde_as]
127132
/// #[derive(Serialize, Deserialize)]
@@ -134,33 +139,30 @@ use crate::error::{Error, Result};
134139
///
135140
/// // serializes as a BSON datetime.
136141
/// // this requires the "chrono-0_4" feature flag
137-
/// #[serde_as(as = "bson_datetime::FromChronoDateTime")]
142+
/// #[serde_as(as = "datetime::FromChrono04DateTime")]
138143
/// chrono_as_bson: chrono::DateTime<chrono::Utc>,
139144
///
140145
/// // serializes as an RFC 3339 / ISO-8601 string.
141-
/// #[serde_as(as = "bson_datetime::AsRfc3339String")]
146+
/// // this requires the "serde_with-3" feature flag
147+
/// #[serde_as(as = "datetime::AsRfc3339String")]
142148
/// bson_as_string: bson::DateTime,
143149
/// }
144150
/// # }
145151
/// ```
146-
/// ### The `serde_with-3` feature flag
147-
///
148-
/// The `serde_with-3` feature can be enabled to support more ergonomic serde attributes for
149-
/// (de)serializing [`chrono::DateTime`] from/to BSON via the [`serde_with`](https://docs.rs/serde_with/1.11.0/serde_with/)
150-
/// crate. The main benefit of this compared to the regular `serde_helpers` is that `serde_with-3`
151-
/// can handle nested [`chrono::DateTime`] values (e.g. in [`Option`]), whereas the former only
152-
/// works on fields that are exactly [`chrono::DateTime`].
152+
/// The main benefit of using the [`serde_with`](https://docs.rs/serde_with/1.11.0/serde_with/) crate
153+
/// is that it can handle nested [`chrono::DateTime`] values (e.g. in [`Option`] or [`Vec`]).
153154
/// ```
154155
/// # #[cfg(all(feature = "chrono-0_4", feature = "serde_with-3"))]
155156
/// # {
156157
/// use serde::{Deserialize, Serialize};
157-
/// use bson::doc;
158+
/// use serde_with::serde_as;
159+
/// use bson::{doc, serde_helpers::datetime};
158160
///
159-
/// #[serde_with::serde_as]
161+
/// #[serde_as]
160162
/// #[derive(Deserialize, Serialize, PartialEq, Debug)]
161163
/// struct Foo {
162-
/// /// Serializes as a BSON datetime rather than using [`chrono::DateTime`]'s serialization
163-
/// #[serde_as(as = "Option<bson::DateTime>")]
164+
/// /// serializes as a BSON datetime rather than using [`chrono::DateTime`]'s serialization
165+
/// #[serde_as(as = "Option<datetime::FromChrono04DateTime>")]
164166
/// as_bson: Option<chrono::DateTime<chrono::Utc>>,
165167
/// }
166168
///

src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@
322322
//! on strings instead, so when using it with BSON, the BSON datetime type is not used. To work
323323
//! around this, the `chrono-0_4` feature flag can be enabled. This flag exposes a number of
324324
//! convenient conversions between [`bson::DateTime`](crate::DateTime) and [`chrono::DateTime`],
325-
//! including the [`serde_helpers::bson_datetime::FromChronoDateTime`]
325+
//! including the [`serde_helpers::datetime::FromChrono04DateTime`]
326326
//! serde helper, which can be used to (de)serialize [`chrono::DateTime`]s to/from BSON datetimes,
327327
//! and the `From<chrono::DateTime>` implementation for [`Bson`], which allows [`chrono::DateTime`]
328328
//! values to be used in the `doc!` and `bson!` macros.
@@ -334,7 +334,7 @@
334334
//! use serde::{Serialize, Deserialize};
335335
//! use serde_with::serde_as;
336336
//! use bson::doc;
337-
//! use bson::serde_helpers::bson_datetime;
337+
//! use bson::serde_helpers::datetime;
338338
//!
339339
//! #[serde_as]
340340
//! #[derive(Serialize, Deserialize)]
@@ -347,7 +347,7 @@
347347
//!
348348
//! // serializes as a BSON datetime.
349349
//! // this requires the "chrono-0_4" feature flag
350-
//! #[serde_as(as = "bson_datetime::FromChronoDateTime")]
350+
//! #[serde_as(as = "datetime::FromChrono04DateTime")]
351351
//! chrono_as_bson: chrono::DateTime<chrono::Utc>,
352352
//! }
353353
//!

0 commit comments

Comments
 (0)