Skip to content

Commit 79ab64e

Browse files
authored
minor: improve serde documentation of DateTime and ObjectId (#401)
1 parent 94e6d2a commit 79ab64e

File tree

2 files changed

+135
-9
lines changed

2 files changed

+135
-9
lines changed

src/datetime.rs

Lines changed: 68 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
//! BSON DateTime
1+
//! Module containing functionality related to BSON DateTimes.
2+
//! For more information, see the documentation for the [`DateTime`] type.
23
34
use std::{
45
convert::TryInto,
@@ -57,12 +58,68 @@ use serde_with::{DeserializeAs, SerializeAs};
5758
/// # }
5859
/// ```
5960
///
61+
/// ## Serde integration
62+
///
6063
/// This type differs from [`chrono::DateTime`] in that it serializes to and deserializes from a
61-
/// BSON datetime rather than an RFC 3339 formatted string. Additionally, in non-BSON formats, it
62-
/// will serialize to and deserialize from that format's equivalent of the
63-
/// [extended JSON representation](https://www.mongodb.com/docs/manual/reference/mongodb-extended-json/) of a datetime.
64-
/// To serialize a [`chrono::DateTime`] as a BSON datetime, you can use
65-
/// [`crate::serde_helpers::chrono_datetime_as_bson_datetime`].
64+
/// BSON datetime rather than an RFC 3339 formatted string.
65+
///
66+
/// e.g.
67+
/// ```rust
68+
/// use serde::{Serialize, Deserialize};
69+
///
70+
/// #[derive(Serialize, Deserialize)]
71+
/// struct Foo {
72+
/// // serializes as a BSON datetime.
73+
/// date_time: bson::DateTime,
74+
///
75+
/// // serializes as an RFC 3339 / ISO-8601 string.
76+
/// chrono_datetime: chrono::DateTime<chrono::Utc>,
77+
/// }
78+
///
79+
/// # fn main() -> bson::ser::Result<()> {
80+
/// let f = Foo { date_time: bson::DateTime::now(), chrono_datetime: chrono::Utc::now() };
81+
/// println!("{:?}", bson::to_document(&f)?);
82+
/// # Ok(())
83+
/// # }
84+
/// ```
85+
/// Produces the following output:
86+
/// ```js
87+
/// { "date_time": DateTime("2023-01-23 20:11:47.316 +00:00:00"), "chrono_datetime": "2023-01-23T20:11:47.316114543Z" }
88+
/// ```
89+
///
90+
/// Additionally, in non-BSON formats, it will serialize to and deserialize from that format's
91+
/// equivalent of the [extended JSON representation](https://www.mongodb.com/docs/manual/reference/mongodb-extended-json/) of a datetime.
92+
///
93+
/// e.g.
94+
/// ```rust
95+
/// # use serde::Serialize;
96+
/// # #[derive(Serialize)]
97+
/// # struct Foo {
98+
/// # // serializes as a BSON datetime.
99+
/// # date_time: bson::DateTime,
100+
/// #
101+
/// # // serializes as an RFC 3339 / ISO-8601 string.
102+
/// # chrono_datetime: chrono::DateTime<chrono::Utc>,
103+
/// # }
104+
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
105+
/// let f = Foo { date_time: bson::DateTime::now(), chrono_datetime: chrono::Utc::now() };
106+
/// println!("{}", serde_json::to_string(&f)?);
107+
/// # Ok(())
108+
/// # }
109+
/// ```
110+
/// Produces the following output:
111+
/// ```js
112+
/// {"date_time":{"$date":{"$numberLong":"1674504029491"}},"chrono_datetime":"2023-01-23T20:00:29.491791120Z"}
113+
/// ```
114+
///
115+
/// ### `serde_helpers`
116+
/// The `bson` crate provides a number of useful helpers for serializing and deserializing
117+
/// various datetime types to and from different formats. For example, to serialize a
118+
/// [`chrono::DateTime`] as a BSON datetime, you can use
119+
/// [`crate::serde_helpers::chrono_datetime_as_bson_datetime`]. Similarly, to serialize a BSON
120+
/// [`DateTime`] to a string, you can use [`crate::serde_helpers::bson_datetime_as_rfc3339_string`].
121+
/// Check out the [`crate::serde_helpers`] module documentation for a list of all of the helpers
122+
/// offered by the crate.
66123
///
67124
/// ```rust
68125
/// # #[cfg(feature = "chrono-0_4")]
@@ -81,10 +138,14 @@ use serde_with::{DeserializeAs, SerializeAs};
81138
/// // this requires the "chrono-0_4" feature flag
82139
/// #[serde(with = "bson::serde_helpers::chrono_datetime_as_bson_datetime")]
83140
/// chrono_as_bson: chrono::DateTime<chrono::Utc>,
141+
///
142+
/// // serializes as an RFC 3339 / ISO-8601 string.
143+
/// #[serde(with = "bson::serde_helpers::bson_datetime_as_rfc3339_string")]
144+
/// bson_as_string: bson::DateTime,
84145
/// }
85146
/// # }
86147
/// ```
87-
/// ## The `serde_with` feature flag
148+
/// ### The `serde_with` feature flag
88149
///
89150
/// The `serde_with` feature can be enabled to support more ergonomic serde attributes for
90151
/// (de)serializing [`chrono::DateTime`] from/to BSON via the [`serde_with`](https://docs.rs/serde_with/1.11.0/serde_with/)

src/oid.rs

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
//! ObjectId
1+
//! Module containing functionality related to BSON ObjectIds.
2+
//! For more information, see the documentation for the [`ObjectId`] type.
23
34
use std::{
45
convert::TryInto,
@@ -72,7 +73,71 @@ impl fmt::Display for Error {
7273

7374
impl error::Error for Error {}
7475

75-
/// A wrapper around raw 12-byte ObjectId representations.
76+
/// A wrapper around a raw 12-byte ObjectId.
77+
///
78+
/// ## `serde` integration
79+
/// When serialized to BSON via `serde`, this type produces a BSON ObjectId. In non-BSON formats, it
80+
/// will serialize to and deserialize from that format's equivalent of the [extended JSON representation](https://www.mongodb.com/docs/manual/reference/mongodb-extended-json/) of a BSON ObjectId.
81+
///
82+
/// [`ObjectId`]s can be deserialized from hex strings in all formats.
83+
///
84+
/// e.g.
85+
/// ```rust
86+
/// use serde::{Serialize, Deserialize};
87+
/// use bson::oid::ObjectId;
88+
///
89+
/// #[derive(Serialize, Deserialize)]
90+
/// struct Foo {
91+
/// oid: ObjectId,
92+
/// }
93+
///
94+
/// # fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
95+
/// let f = Foo { oid: ObjectId::new() };
96+
/// println!("bson: {}", bson::to_document(&f)?);
97+
/// println!("json: {}", serde_json::to_string(&f)?);
98+
/// # Ok(())
99+
/// # }
100+
/// ```
101+
/// Produces the following output:
102+
/// ```text
103+
/// bson: { "oid": ObjectId("63ceed18f71dda7d8cf21e8e") }
104+
/// json: {"oid":{"$oid":"63ceed18f71dda7d8cf21e8e"}}
105+
/// ```
106+
///
107+
/// ### `serde_helpers`
108+
/// The `bson` crate provides a number of useful helpers for serializing and deserializing
109+
/// various types to and from different formats. For example, to serialize an
110+
/// [`ObjectId`] as a hex string, you can use
111+
/// [`crate::serde_helpers::serialize_object_id_as_hex_string`].
112+
/// Check out the [`crate::serde_helpers`] module documentation for a list of all of the helpers
113+
/// offered by the crate.
114+
///
115+
/// e.g.
116+
/// ```rust
117+
/// use serde::{Serialize, Deserialize};
118+
/// use bson::oid::ObjectId;
119+
///
120+
/// #[derive(Serialize, Deserialize)]
121+
/// struct Foo {
122+
/// // Serializes as a BSON ObjectId or extJSON in non-BSON formats
123+
/// oid: ObjectId,
124+
///
125+
/// // Serializes as a hex string in all formats
126+
/// #[serde(serialize_with = "bson::serde_helpers::serialize_object_id_as_hex_string")]
127+
/// oid_as_hex: ObjectId,
128+
/// }
129+
/// # fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
130+
/// let f = Foo { oid: ObjectId::new(), oid_as_hex: ObjectId::new() };
131+
/// println!("bson: {}", bson::to_document(&f)?);
132+
/// println!("json: {}", serde_json::to_string(&f)?);
133+
/// # Ok(())
134+
/// # }
135+
/// ```
136+
/// Produces the following output:
137+
/// ```text
138+
/// bson: { "oid": ObjectId("63ceeffd37518221cdc6cda2"), "oid_as_hex": "63ceeffd37518221cdc6cda3" }
139+
/// json: {"oid":{"$oid":"63ceeffd37518221cdc6cda2"},"oid_as_hex":"63ceeffd37518221cdc6cda3"}
140+
/// ```
76141
#[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash)]
77142
pub struct ObjectId {
78143
id: [u8; 12],

0 commit comments

Comments
 (0)