@@ -108,20 +108,25 @@ use crate::error::{Error, Result};
108
108
///
109
109
/// ### `serde_helpers`
110
110
/// 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/)
117
112
/// crate.
118
113
///
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.
119
124
/// ```rust
120
- /// # #[cfg(feature = "chrono-0_4")]
125
+ /// # #[cfg(all( feature = "chrono-0_4", feature = "serde_with-3") )]
121
126
/// # {
122
127
/// use serde::{Serialize, Deserialize};
123
128
/// use serde_with::serde_as;
124
- /// use bson::serde_helpers::bson_datetime ;
129
+ /// use bson::serde_helpers::datetime ;
125
130
///
126
131
/// #[serde_as]
127
132
/// #[derive(Serialize, Deserialize)]
@@ -134,33 +139,30 @@ use crate::error::{Error, Result};
134
139
///
135
140
/// // serializes as a BSON datetime.
136
141
/// // this requires the "chrono-0_4" feature flag
137
- /// #[serde_as(as = "bson_datetime::FromChronoDateTime ")]
142
+ /// #[serde_as(as = "datetime::FromChrono04DateTime ")]
138
143
/// chrono_as_bson: chrono::DateTime<chrono::Utc>,
139
144
///
140
145
/// // 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")]
142
148
/// bson_as_string: bson::DateTime,
143
149
/// }
144
150
/// # }
145
151
/// ```
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`]).
153
154
/// ```
154
155
/// # #[cfg(all(feature = "chrono-0_4", feature = "serde_with-3"))]
155
156
/// # {
156
157
/// use serde::{Deserialize, Serialize};
157
- /// use bson::doc;
158
+ /// use serde_with::serde_as;
159
+ /// use bson::{doc, serde_helpers::datetime};
158
160
///
159
- /// #[serde_with:: serde_as]
161
+ /// #[serde_as]
160
162
/// #[derive(Deserialize, Serialize, PartialEq, Debug)]
161
163
/// 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 >")]
164
166
/// as_bson: Option<chrono::DateTime<chrono::Utc>>,
165
167
/// }
166
168
///
0 commit comments