bson2/
serde_helpers.rs

1//! Collection of helper functions for serializing to and deserializing from BSON using Serde
2
3use std::{convert::TryFrom, result::Result};
4
5use serde::{ser, Serialize, Serializer};
6
7use crate::oid::ObjectId;
8
9#[doc(inline)]
10pub use bson_datetime_as_rfc3339_string::{
11    deserialize as deserialize_bson_datetime_from_rfc3339_string,
12    serialize as serialize_bson_datetime_as_rfc3339_string,
13};
14#[cfg(feature = "chrono-0_4")]
15#[doc(inline)]
16pub use chrono_datetime_as_bson_datetime::{
17    deserialize as deserialize_chrono_datetime_from_bson_datetime,
18    serialize as serialize_chrono_datetime_as_bson_datetime,
19};
20#[doc(inline)]
21pub use hex_string_as_object_id::{
22    deserialize as deserialize_hex_string_from_object_id,
23    serialize as serialize_hex_string_as_object_id,
24};
25#[doc(inline)]
26pub use rfc3339_string_as_bson_datetime::{
27    deserialize as deserialize_rfc3339_string_from_bson_datetime,
28    serialize as serialize_rfc3339_string_as_bson_datetime,
29};
30#[doc(inline)]
31pub use timestamp_as_u32::{
32    deserialize as deserialize_timestamp_from_u32,
33    serialize as serialize_timestamp_as_u32,
34};
35#[doc(inline)]
36pub use u32_as_f64::{deserialize as deserialize_u32_from_f64, serialize as serialize_u32_as_f64};
37#[doc(inline)]
38pub use u32_as_timestamp::{
39    deserialize as deserialize_u32_from_timestamp,
40    serialize as serialize_u32_as_timestamp,
41};
42#[doc(inline)]
43pub use u64_as_f64::{deserialize as deserialize_u64_from_f64, serialize as serialize_u64_as_f64};
44
45#[cfg(feature = "uuid-0_8")]
46#[doc(inline)]
47pub use uuid_as_binary::{
48    deserialize as deserialize_uuid_from_binary,
49    serialize as serialize_uuid_as_binary,
50};
51#[cfg(feature = "uuid-0_8")]
52#[doc(inline)]
53pub use uuid_as_c_sharp_legacy_binary::{
54    deserialize as deserialize_uuid_from_c_sharp_legacy_binary,
55    serialize as serialize_uuid_as_c_sharp_legacy_binary,
56};
57#[cfg(feature = "uuid-0_8")]
58#[doc(inline)]
59pub use uuid_as_java_legacy_binary::{
60    deserialize as deserialize_uuid_from_java_legacy_binary,
61    serialize as serialize_uuid_as_java_legacy_binary,
62};
63#[cfg(feature = "uuid-0_8")]
64#[doc(inline)]
65pub use uuid_as_python_legacy_binary::{
66    deserialize as deserialize_uuid_from_python_legacy_binary,
67    serialize as serialize_uuid_as_python_legacy_binary,
68};
69
70/// Attempts to serialize a u32 as an i32. Errors if an exact conversion is not possible.
71pub fn serialize_u32_as_i32<S: Serializer>(val: &u32, serializer: S) -> Result<S::Ok, S::Error> {
72    match i32::try_from(*val) {
73        Ok(val) => serializer.serialize_i32(val),
74        Err(_) => Err(ser::Error::custom(format!("cannot convert {} to i32", val))),
75    }
76}
77
78/// Serializes a u32 as an i64.
79pub fn serialize_u32_as_i64<S: Serializer>(val: &u32, serializer: S) -> Result<S::Ok, S::Error> {
80    serializer.serialize_i64(*val as i64)
81}
82
83/// Attempts to serialize a u64 as an i32. Errors if an exact conversion is not possible.
84pub fn serialize_u64_as_i32<S: Serializer>(val: &u64, serializer: S) -> Result<S::Ok, S::Error> {
85    match i32::try_from(*val) {
86        Ok(val) => serializer.serialize_i32(val),
87        Err(_) => Err(ser::Error::custom(format!("cannot convert {} to i32", val))),
88    }
89}
90
91/// Attempts to serialize a u64 as an i64. Errors if an exact conversion is not possible.
92pub fn serialize_u64_as_i64<S: Serializer>(val: &u64, serializer: S) -> Result<S::Ok, S::Error> {
93    match i64::try_from(*val) {
94        Ok(val) => serializer.serialize_i64(val),
95        Err(_) => Err(ser::Error::custom(format!("cannot convert {} to i64", val))),
96    }
97}
98
99/// Serializes an [`ObjectId`] as a hex string.
100pub fn serialize_object_id_as_hex_string<S: Serializer>(
101    val: &ObjectId,
102    serializer: S,
103) -> Result<S::Ok, S::Error> {
104    val.to_hex().serialize(serializer)
105}
106
107/// Contains functions to serialize a u32 as an f64 (BSON double) and deserialize a
108/// u32 from an f64 (BSON double).
109///
110/// ```rust
111/// # use serde::{Serialize, Deserialize};
112/// # use bson::serde_helpers::u32_as_f64;
113/// #[derive(Serialize, Deserialize)]
114/// struct FileInfo {
115///     #[serde(with = "u32_as_f64")]
116///     pub size_bytes: u32,
117/// }
118/// ```
119pub mod u32_as_f64 {
120    use serde::{de, Deserialize, Deserializer, Serializer};
121
122    /// Deserializes a u32 from an f64 (BSON double). Errors if an exact conversion is not possible.
123    pub fn deserialize<'de, D>(deserializer: D) -> Result<u32, D::Error>
124    where
125        D: Deserializer<'de>,
126    {
127        let f = f64::deserialize(deserializer)?;
128        if (f - f as u32 as f64).abs() <= f64::EPSILON {
129            Ok(f as u32)
130        } else {
131            Err(de::Error::custom(format!(
132                "cannot convert f64 (BSON double) {} to u32",
133                f
134            )))
135        }
136    }
137
138    /// Serializes a u32 as an f64 (BSON double).
139    pub fn serialize<S: Serializer>(val: &u32, serializer: S) -> Result<S::Ok, S::Error> {
140        serializer.serialize_f64(*val as f64)
141    }
142}
143
144/// Contains functions to serialize a u64 as an f64 (BSON double) and deserialize a
145/// u64 from an f64 (BSON double).
146///
147/// ```rust
148/// # use serde::{Serialize, Deserialize};
149/// # use bson::serde_helpers::u64_as_f64;
150/// #[derive(Serialize, Deserialize)]
151/// struct FileInfo {
152///     #[serde(with = "u64_as_f64")]
153///     pub size_bytes: u64,
154/// }
155/// ```
156pub mod u64_as_f64 {
157    use serde::{de, ser, Deserialize, Deserializer, Serializer};
158
159    /// Deserializes a u64 from an f64 (BSON double). Errors if an exact conversion is not possible.
160    pub fn deserialize<'de, D>(deserializer: D) -> Result<u64, D::Error>
161    where
162        D: Deserializer<'de>,
163    {
164        let f = f64::deserialize(deserializer)?;
165        if (f - f as u64 as f64).abs() <= f64::EPSILON {
166            Ok(f as u64)
167        } else {
168            Err(de::Error::custom(format!(
169                "cannot convert f64 (BSON double) {} to u64",
170                f
171            )))
172        }
173    }
174
175    /// Serializes a u64 as an f64 (BSON double). Errors if an exact conversion is not possible.
176    pub fn serialize<S: Serializer>(val: &u64, serializer: S) -> Result<S::Ok, S::Error> {
177        if val < &u64::MAX && *val == *val as f64 as u64 {
178            serializer.serialize_f64(*val as f64)
179        } else {
180            Err(ser::Error::custom(format!(
181                "cannot convert u64 {} to f64 (BSON double)",
182                val
183            )))
184        }
185    }
186}
187
188/// Contains functions to serialize a [`chrono::DateTime`] as a [`crate::DateTime`] and deserialize
189/// a [`chrono::DateTime`] from a [`crate::DateTime`].
190///
191/// ```rust
192/// # #[cfg(feature = "chrono-0_4")]
193/// # {
194/// # use serde::{Serialize, Deserialize};
195/// # use bson::serde_helpers::chrono_datetime_as_bson_datetime;
196/// #[derive(Serialize, Deserialize)]
197/// struct Event {
198///     #[serde(with = "chrono_datetime_as_bson_datetime")]
199///     pub date: chrono::DateTime<chrono::Utc>,
200/// }
201/// # }
202/// ```
203#[cfg(feature = "chrono-0_4")]
204#[cfg_attr(docsrs, doc(cfg(feature = "chrono-0_4")))]
205pub mod chrono_datetime_as_bson_datetime {
206    use crate::DateTime;
207    use chrono::Utc;
208    use serde::{Deserialize, Deserializer, Serialize, Serializer};
209    use std::result::Result;
210
211    /// Deserializes a [`chrono::DateTime`] from a [`crate::DateTime`].
212    #[cfg_attr(docsrs, doc(cfg(feature = "chrono-0_4")))]
213    pub fn deserialize<'de, D>(deserializer: D) -> Result<chrono::DateTime<Utc>, D::Error>
214    where
215        D: Deserializer<'de>,
216    {
217        let datetime = DateTime::deserialize(deserializer)?;
218        Ok(datetime.to_chrono())
219    }
220
221    /// Serializes a [`chrono::DateTime`] as a [`crate::DateTime`].
222    #[cfg_attr(docsrs, doc(cfg(feature = "chrono-0_4")))]
223    pub fn serialize<S: Serializer>(
224        val: &chrono::DateTime<Utc>,
225        serializer: S,
226    ) -> Result<S::Ok, S::Error> {
227        let datetime = DateTime::from_chrono(val.to_owned());
228        datetime.serialize(serializer)
229    }
230}
231
232/// Contains functions to serialize an RFC 3339 (ISO 8601) formatted string as a [`crate::DateTime`]
233/// and deserialize an RFC 3339 (ISO 8601) formatted string from a [`crate::DateTime`].
234///
235/// ```rust
236/// # use serde::{Serialize, Deserialize};
237/// # use bson::serde_helpers::rfc3339_string_as_bson_datetime;
238/// #[derive(Serialize, Deserialize)]
239/// struct Event {
240///     #[serde(with = "rfc3339_string_as_bson_datetime")]
241///     pub date: String,
242/// }
243/// ```
244pub mod rfc3339_string_as_bson_datetime {
245    use crate::{Bson, DateTime};
246    use serde::{ser, Deserialize, Deserializer, Serialize, Serializer};
247    use std::result::Result;
248
249    /// Deserializes an ISO string from a DateTime.
250    pub fn deserialize<'de, D>(deserializer: D) -> Result<String, D::Error>
251    where
252        D: Deserializer<'de>,
253    {
254        let date = DateTime::deserialize(deserializer)?;
255        Ok(date.to_rfc3339_string())
256    }
257
258    /// Serializes an ISO string as a DateTime.
259    pub fn serialize<S: Serializer>(val: &str, serializer: S) -> Result<S::Ok, S::Error> {
260        let date =
261            chrono::DateTime::<chrono::FixedOffset>::parse_from_rfc3339(val).map_err(|_| {
262                ser::Error::custom(format!("cannot convert {} to chrono::DateTime", val))
263            })?;
264        Bson::DateTime(crate::DateTime::from_chrono(date)).serialize(serializer)
265    }
266}
267
268/// Contains functions to serialize a [`crate::DateTime`] as an RFC 3339 (ISO 8601) formatted string
269/// and deserialize a [`crate::DateTime`] from an RFC 3339 (ISO 8601) formatted string.
270///
271/// ```rust
272/// # use serde::{Serialize, Deserialize};
273/// # use bson::serde_helpers::bson_datetime_as_rfc3339_string;
274/// #[derive(Serialize, Deserialize)]
275/// struct Event {
276///     #[serde(with = "bson_datetime_as_rfc3339_string")]
277///     pub date: bson::DateTime,
278/// }
279/// ```
280pub mod bson_datetime_as_rfc3339_string {
281    use crate::DateTime;
282    use serde::{de, Deserialize, Deserializer, Serializer};
283    use std::result::Result;
284
285    /// Deserializes a [`crate::DateTime`] from an RFC 3339 formatted string.
286    pub fn deserialize<'de, D>(deserializer: D) -> Result<DateTime, D::Error>
287    where
288        D: Deserializer<'de>,
289    {
290        let iso = String::deserialize(deserializer)?;
291        let date =
292            chrono::DateTime::<chrono::FixedOffset>::parse_from_rfc3339(&iso).map_err(|_| {
293                de::Error::custom(format!("cannot parse RFC 3339 datetime from \"{}\"", iso))
294            })?;
295        Ok(DateTime::from_chrono(date))
296    }
297
298    /// Serializes a [`crate::DateTime`] as an RFC 3339 (ISO 8601) formatted string.
299    pub fn serialize<S: Serializer>(val: &DateTime, serializer: S) -> Result<S::Ok, S::Error> {
300        serializer.serialize_str(&val.to_rfc3339_string())
301    }
302}
303
304/// Contains functions to serialize a hex string as an ObjectId and deserialize a
305/// hex string from an ObjectId
306///
307/// ```rust
308/// # use serde::{Serialize, Deserialize};
309/// # use bson::serde_helpers::hex_string_as_object_id;
310/// #[derive(Serialize, Deserialize)]
311/// struct Item {
312///     #[serde(with = "hex_string_as_object_id")]
313///     pub id: String,
314/// }
315/// ```
316pub mod hex_string_as_object_id {
317    use crate::oid::ObjectId;
318    use serde::{ser, Deserialize, Deserializer, Serialize, Serializer};
319
320    /// Deserializes a hex string from an ObjectId.
321    pub fn deserialize<'de, D>(deserializer: D) -> Result<String, D::Error>
322    where
323        D: Deserializer<'de>,
324    {
325        let object_id = ObjectId::deserialize(deserializer)?;
326        Ok(object_id.to_hex())
327    }
328
329    /// Serializes a hex string as an ObjectId.
330    pub fn serialize<S: Serializer>(val: &str, serializer: S) -> Result<S::Ok, S::Error> {
331        match ObjectId::parse_str(val) {
332            Ok(oid) => oid.serialize(serializer),
333            Err(_) => Err(ser::Error::custom(format!(
334                "cannot convert {} to ObjectId",
335                val
336            ))),
337        }
338    }
339}
340
341/// Contains functions to serialize a [`uuid::Uuid`] as a [`crate::Binary`] and deserialize a
342/// [`uuid::Uuid`] from a [`crate::Binary`]. This only works with version 0.8 of the [`uuid`] crate.
343///
344/// ```rust
345/// use serde::{Serialize, Deserialize};
346/// use uuid::Uuid;
347/// use bson::serde_helpers::uuid_as_binary;
348///
349/// #[derive(Serialize, Deserialize)]
350/// struct Item {
351///     #[serde(with = "uuid_as_binary")]
352///     pub id: Uuid,
353/// }
354/// ```
355#[cfg(feature = "uuid-0_8")]
356#[cfg_attr(docsrs, doc(cfg(feature = "uuid-0_8")))]
357pub mod uuid_as_binary {
358    use serde::{Deserialize, Deserializer, Serialize, Serializer};
359    use std::result::Result;
360    use uuid::Uuid;
361
362    /// Serializes a Uuid as a Binary.
363    #[cfg_attr(docsrs, doc(cfg(feature = "uuid-0_8")))]
364    pub fn serialize<S: Serializer>(val: &Uuid, serializer: S) -> Result<S::Ok, S::Error> {
365        crate::uuid::Uuid::from_external_uuid(*val).serialize(serializer)
366    }
367
368    /// Deserializes a Uuid from a Binary.
369    #[cfg_attr(docsrs, doc(cfg(feature = "uuid-0_8")))]
370    pub fn deserialize<'de, D>(deserializer: D) -> Result<Uuid, D::Error>
371    where
372        D: Deserializer<'de>,
373    {
374        let bson_uuid = crate::uuid::Uuid::deserialize(deserializer)?;
375        Ok(bson_uuid.into())
376    }
377}
378
379/// Contains functions to serialize a [`uuid::Uuid`] to a [`crate::Binary`] in the legacy
380/// Java driver UUID format and deserialize [`uuid::Uuid`] from a [`crate::Binary`] in the legacy
381/// Java driver format.
382///
383/// Note: the `"uuid-0_8"` feature must be enabled to use this helper.
384///
385/// ```rust
386/// use serde::{Serialize, Deserialize};
387/// use uuid::Uuid;
388/// use bson::serde_helpers::uuid_as_java_legacy_binary;
389///
390/// #[derive(Serialize, Deserialize)]
391/// struct Item {
392///     #[serde(with = "uuid_as_java_legacy_binary")]
393///     pub id: Uuid,
394/// }
395/// ```
396#[cfg(feature = "uuid-0_8")]
397#[cfg_attr(docsrs, doc(cfg(feature = "uuid-0_8")))]
398pub mod uuid_as_java_legacy_binary {
399    use crate::{uuid::UuidRepresentation, Binary};
400    use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
401    use std::result::Result;
402    use uuid::Uuid;
403
404    /// Serializes a Uuid as a Binary in a Java Legacy UUID format.
405    #[cfg_attr(docsrs, doc(cfg(feature = "uuid-0_8")))]
406    pub fn serialize<S: Serializer>(val: &Uuid, serializer: S) -> Result<S::Ok, S::Error> {
407        let binary = Binary::from_uuid_with_representation(
408            crate::uuid::Uuid::from_external_uuid(*val),
409            UuidRepresentation::JavaLegacy,
410        );
411        binary.serialize(serializer)
412    }
413
414    /// Deserializes a Uuid from a Binary in a Java Legacy UUID format.
415    #[cfg_attr(docsrs, doc(cfg(feature = "uuid-0_8")))]
416    pub fn deserialize<'de, D>(deserializer: D) -> Result<Uuid, D::Error>
417    where
418        D: Deserializer<'de>,
419    {
420        let binary = Binary::deserialize(deserializer)?;
421        let uuid = binary
422            .to_uuid_with_representation(UuidRepresentation::JavaLegacy)
423            .map_err(de::Error::custom)?;
424        Ok(uuid.to_uuid_0_8())
425    }
426}
427
428/// Contains functions to serialize a [`uuid::Uuid`] to a [`crate::Binary`] in the legacy Python
429/// driver UUID format and deserialize [`uuid::Uuid`] from a [`crate::Binary`] in the legacy Python
430/// driver format.
431///
432/// Note: the `"uuid-0_8"` feature must be enabled to use this helper.
433///
434/// ```rust
435/// use serde::{Serialize, Deserialize};
436/// use uuid::Uuid;
437/// use bson::serde_helpers::uuid_as_python_legacy_binary;
438///
439/// #[derive(Serialize, Deserialize)]
440/// struct Item {
441///     #[serde(with = "uuid_as_python_legacy_binary")]
442///     pub id: Uuid,
443/// }
444/// ```
445#[cfg(feature = "uuid-0_8")]
446#[cfg_attr(docsrs, doc(cfg(feature = "uuid-0_8")))]
447pub mod uuid_as_python_legacy_binary {
448    use crate::{uuid::UuidRepresentation, Binary};
449    use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
450    use std::result::Result;
451    use uuid::Uuid;
452
453    /// Serializes a Uuid as a Binary in a Python Legacy UUID format.
454    #[cfg_attr(docsrs, doc(cfg(feature = "uuid-0_8")))]
455    pub fn serialize<S: Serializer>(val: &Uuid, serializer: S) -> Result<S::Ok, S::Error> {
456        let binary = Binary::from_uuid_with_representation(
457            crate::uuid::Uuid::from_external_uuid(*val),
458            UuidRepresentation::PythonLegacy,
459        );
460        binary.serialize(serializer)
461    }
462
463    /// Deserializes a Uuid from a Binary in a Python Legacy UUID format.
464    #[cfg_attr(docsrs, doc(cfg(feature = "uuid-0_8")))]
465    pub fn deserialize<'de, D>(deserializer: D) -> Result<Uuid, D::Error>
466    where
467        D: Deserializer<'de>,
468    {
469        let binary = Binary::deserialize(deserializer)?;
470        let uuid = binary
471            .to_uuid_with_representation(UuidRepresentation::PythonLegacy)
472            .map_err(de::Error::custom)?;
473        Ok(uuid.into())
474    }
475}
476
477/// Contains functions to serialize a [`uuid::Uuid`] to a [`crate::Binary`] in the legacy C# driver
478/// UUID format and deserialize [`uuid::Uuid`] from a [`crate::Binary`] in the legacy C# driver
479/// format.
480///
481/// Note: the `"uuid-0_8"` feature must be enabled to use this helper.
482///
483/// ```rust
484/// use serde::{Serialize, Deserialize};
485/// use uuid::Uuid;
486/// use bson::serde_helpers::uuid_as_c_sharp_legacy_binary;
487///
488/// #[derive(Serialize, Deserialize)]
489/// struct Item {
490///     #[serde(with = "uuid_as_c_sharp_legacy_binary")]
491///     pub id: Uuid,
492/// }
493/// ```
494#[cfg(feature = "uuid-0_8")]
495#[cfg_attr(docsrs, doc(cfg(feature = "uuid-0_8")))]
496pub mod uuid_as_c_sharp_legacy_binary {
497    use crate::{uuid::UuidRepresentation, Binary};
498    use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
499    use std::result::Result;
500    use uuid::Uuid;
501
502    /// Serializes a Uuid as a Binary in a C# Legacy UUID format.
503    #[cfg_attr(docsrs, doc(cfg(feature = "uuid-0_8")))]
504    pub fn serialize<S: Serializer>(val: &Uuid, serializer: S) -> Result<S::Ok, S::Error> {
505        let binary = Binary::from_uuid_with_representation(
506            crate::uuid::Uuid::from_external_uuid(*val),
507            UuidRepresentation::CSharpLegacy,
508        );
509        binary.serialize(serializer)
510    }
511
512    /// Deserializes a Uuid from a Binary in a C# Legacy UUID format.
513    #[cfg_attr(docsrs, doc(cfg(feature = "uuid-0_8")))]
514    pub fn deserialize<'de, D>(deserializer: D) -> Result<Uuid, D::Error>
515    where
516        D: Deserializer<'de>,
517    {
518        let binary = Binary::deserialize(deserializer)?;
519        let uuid = binary
520            .to_uuid_with_representation(UuidRepresentation::CSharpLegacy)
521            .map_err(de::Error::custom)?;
522        Ok(uuid.into())
523    }
524}
525
526/// Contains functions to serialize a u32 as a bson::Timestamp and deserialize a u32 from a
527/// bson::Timestamp. The u32 should represent seconds since the Unix epoch.
528///
529/// ```rust
530/// # use serde::{Serialize, Deserialize};
531/// # use bson::serde_helpers::u32_as_timestamp;
532/// #[derive(Serialize, Deserialize)]
533/// struct Event {
534///     #[serde(with = "u32_as_timestamp")]
535///     pub time: u32,
536/// }
537/// ```
538pub mod u32_as_timestamp {
539    use crate::{Bson, Timestamp};
540    use serde::{Deserialize, Deserializer, Serialize, Serializer};
541    use std::result::Result;
542
543    /// Serializes a u32 as a bson::Timestamp.
544    pub fn serialize<S: Serializer>(val: &u32, serializer: S) -> Result<S::Ok, S::Error> {
545        let timestamp = Bson::Timestamp(Timestamp {
546            time: *val,
547            increment: 0,
548        });
549        timestamp.serialize(serializer)
550    }
551
552    /// Deserializes a u32 from a bson::Timestamp.
553    pub fn deserialize<'de, D>(deserializer: D) -> Result<u32, D::Error>
554    where
555        D: Deserializer<'de>,
556    {
557        let timestamp = Timestamp::deserialize(deserializer)?;
558        Ok(timestamp.time)
559    }
560}
561
562/// Contains functions to serialize a bson::Timestamp as a u32 and deserialize a bson::Timestamp
563/// from a u32. The u32 should represent seconds since the Unix epoch. Serialization will return an
564/// error if the Timestamp has a non-zero increment.
565///
566/// ```rust
567/// # use serde::{Serialize, Deserialize};
568/// # use bson::{serde_helpers::timestamp_as_u32, Timestamp};
569/// #[derive(Serialize, Deserialize)]
570/// struct Item {
571///     #[serde(with = "timestamp_as_u32")]
572///     pub timestamp: Timestamp,
573/// }
574/// ```
575pub mod timestamp_as_u32 {
576    use crate::Timestamp;
577    use serde::{ser, Deserialize, Deserializer, Serializer};
578    use std::result::Result;
579
580    /// Serializes a bson::Timestamp as a u32. Returns an error if the conversion is lossy (i.e. the
581    /// Timestamp has a non-zero increment).
582    pub fn serialize<S: Serializer>(val: &Timestamp, serializer: S) -> Result<S::Ok, S::Error> {
583        if val.increment != 0 {
584            return Err(ser::Error::custom(
585                "Cannot convert Timestamp with a non-zero increment to u32",
586            ));
587        }
588        serializer.serialize_u32(val.time)
589    }
590
591    /// Deserializes a bson::Timestamp from a u32.
592    pub fn deserialize<'de, D>(deserializer: D) -> Result<Timestamp, D::Error>
593    where
594        D: Deserializer<'de>,
595    {
596        let time = u32::deserialize(deserializer)?;
597        Ok(Timestamp { time, increment: 0 })
598    }
599}