Skip to content

Commit 5c20bde

Browse files
RUST-2251 document and tidy up serde helpers (#597)
Co-authored-by: Isabel Atkinson <isabel.atkinson@mongodb.com>
1 parent 46bc541 commit 5c20bde

File tree

7 files changed

+143
-210
lines changed

7 files changed

+143
-210
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ Note that if you are using `bson` through the `mongodb` crate, you do not need t
5050
| `chrono-0_4` | Enable support for v0.4 of the [`chrono`](https://docs.rs/chrono/0.4) crate in the public API. | n/a | no |
5151
| `uuid-1` | Enable support for v1.x of the [`uuid`](https://docs.rs/uuid/1.0) crate in the public API. | n/a | no |
5252
| `time-0_3` | Enable support for v0.3 of the [`time`](https://docs.rs/time/0.3) crate in the public API. | n/a | no |
53-
| `serde_with-3` | Enable [`serde_with`](https://docs.rs/serde_with/3.x) 3.x integrations for `bson::DateTime` and `bson::Uuid`.| `serde_with` | no |
53+
| `serde_with-3` | Enable [`serde_with`](https://docs.rs/serde_with/3.x) type conversion utilities in the public API. | `serde_with` | no |
5454
| `serde_path_to_error` | Enable support for error paths via integration with [`serde_path_to_error`](https://docs.rs/serde_path_to_err/latest). This is an unstable feature and any breaking changes to `serde_path_to_error` may affect usage of it via this feature. | `serde_path_to_error` | no |
5555
| `compat-3-0-0` | Required for future compatibility if default features are disabled. | n/a | no |
5656
| `large_dates` | Increase the supported year range for some `bson::DateTime` utilities from +/-9,999 (inclusive) to +/-999,999 (inclusive). Note that enabling this feature can impact performance and introduce parsing ambiguities. | n/a | no |

serde-tests/test.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,13 +1187,14 @@ fn u2i() {
11871187

11881188
#[test]
11891189
fn serde_with_chrono() {
1190+
use bson::serde_helpers::datetime;
11901191
#[serde_with::serde_as]
11911192
#[derive(Deserialize, Serialize, PartialEq, Debug)]
11921193
struct Foo {
1193-
#[serde_as(as = "Option<bson::DateTime>")]
1194+
#[serde_as(as = "Option<datetime::FromChrono04DateTime>")]
11941195
as_bson: Option<chrono::DateTime<chrono::Utc>>,
11951196

1196-
#[serde_as(as = "Option<bson::DateTime>")]
1197+
#[serde_as(as = "Option<datetime::FromChrono04DateTime>")]
11971198
none_bson: Option<chrono::DateTime<chrono::Utc>>,
11981199
}
11991200

@@ -1211,13 +1212,14 @@ fn serde_with_chrono() {
12111212

12121213
#[test]
12131214
fn serde_with_uuid() {
1215+
use bson::serde_helpers::uuid_1;
12141216
#[serde_with::serde_as]
12151217
#[derive(Deserialize, Serialize, PartialEq, Debug)]
12161218
struct Foo {
1217-
#[serde_as(as = "Option<bson::Uuid>")]
1219+
#[serde_as(as = "Option<uuid_1::AsBinary>")]
12181220
as_bson: Option<uuid::Uuid>,
12191221

1220-
#[serde_as(as = "Option<bson::Uuid>")]
1222+
#[serde_as(as = "Option<uuid_1::AsBinary>")]
12211223
none_bson: Option<uuid::Uuid>,
12221224
}
12231225

src/datetime.rs

Lines changed: 2 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,6 @@ use std::{
1010

1111
#[cfg(feature = "chrono-0_4")]
1212
use chrono::{LocalResult, TimeZone, Utc};
13-
#[cfg(all(
14-
feature = "serde_with-3",
15-
any(feature = "chrono-0_4", feature = "time-0_3", feature = "jiff-0_2")
16-
))]
17-
use serde::{Deserialize, Deserializer, Serialize};
1813
use time::format_description::well_known::Rfc3339;
1914

2015
pub use crate::datetime::builder::DateTimeBuilder;
@@ -108,7 +103,7 @@ use crate::error::{Error, Result};
108103
///
109104
/// ### `serde_helpers`
110105
/// The `bson` crate provides a number of useful helpers for serializing and deserializing
111-
/// various datetime types to and from different formats using the [`serde_with`](https://docs.rs/serde_with/1.11.0/serde_with/)
106+
/// various datetime types to and from different formats using the [`serde_with`](https://docs.rs/serde_with/latest/serde_with/)
112107
/// crate.
113108
///
114109
/// > **Note:** All helpers in this module require use of the [`#[serde_as]`](https://docs.rs/serde_with/latest/serde_with/attr.serde_as.html)
@@ -149,7 +144,7 @@ use crate::error::{Error, Result};
149144
/// }
150145
/// # }
151146
/// ```
152-
/// The main benefit of using the [`serde_with`](https://docs.rs/serde_with/1.11.0/serde_with/) crate
147+
/// The main benefit of using the [`serde_with`](https://docs.rs/serde_with/latest/serde_with/) crate
153148
/// is that it can handle nested [`chrono::DateTime`] values (e.g. in [`Option`] or [`Vec`]).
154149
/// ```
155150
/// # #[cfg(all(feature = "chrono-0_4", feature = "serde_with-3"))]
@@ -496,31 +491,6 @@ impl<T: chrono::TimeZone> From<chrono::DateTime<T>> for crate::DateTime {
496491
}
497492
}
498493

499-
#[cfg(all(feature = "chrono-0_4", feature = "serde_with-3"))]
500-
impl<'de> serde_with::DeserializeAs<'de, chrono::DateTime<Utc>> for crate::DateTime {
501-
fn deserialize_as<D>(deserializer: D) -> std::result::Result<chrono::DateTime<Utc>, D::Error>
502-
where
503-
D: Deserializer<'de>,
504-
{
505-
let dt = DateTime::deserialize(deserializer)?;
506-
Ok(dt.to_chrono())
507-
}
508-
}
509-
510-
#[cfg(all(feature = "chrono-0_4", feature = "serde_with-3"))]
511-
impl serde_with::SerializeAs<chrono::DateTime<Utc>> for crate::DateTime {
512-
fn serialize_as<S>(
513-
source: &chrono::DateTime<Utc>,
514-
serializer: S,
515-
) -> std::result::Result<S::Ok, S::Error>
516-
where
517-
S: serde::Serializer,
518-
{
519-
let dt = DateTime::from_chrono(*source);
520-
dt.serialize(serializer)
521-
}
522-
}
523-
524494
#[cfg(feature = "jiff-0_2")]
525495
impl From<crate::DateTime> for jiff::Timestamp {
526496
fn from(bson_dt: DateTime) -> Self {
@@ -535,31 +505,6 @@ impl From<jiff::Timestamp> for crate::DateTime {
535505
}
536506
}
537507

538-
#[cfg(all(feature = "jiff-0_2", feature = "serde_with-3"))]
539-
impl<'de> serde_with::DeserializeAs<'de, jiff::Timestamp> for crate::DateTime {
540-
fn deserialize_as<D>(deserializer: D) -> std::result::Result<jiff::Timestamp, D::Error>
541-
where
542-
D: Deserializer<'de>,
543-
{
544-
let dt = DateTime::deserialize(deserializer)?;
545-
Ok(dt.to_jiff())
546-
}
547-
}
548-
549-
#[cfg(all(feature = "jiff-0_2", feature = "serde_with-3"))]
550-
impl serde_with::SerializeAs<jiff::Timestamp> for crate::DateTime {
551-
fn serialize_as<S>(
552-
source: &jiff::Timestamp,
553-
serializer: S,
554-
) -> std::result::Result<S::Ok, S::Error>
555-
where
556-
S: serde::Serializer,
557-
{
558-
let dt = DateTime::from_jiff(*source);
559-
dt.serialize(serializer)
560-
}
561-
}
562-
563508
#[cfg(feature = "time-0_3")]
564509
impl From<crate::DateTime> for time::OffsetDateTime {
565510
fn from(bson_dt: DateTime) -> Self {
@@ -573,28 +518,3 @@ impl From<time::OffsetDateTime> for crate::DateTime {
573518
Self::from_time_0_3(x)
574519
}
575520
}
576-
577-
#[cfg(all(feature = "time-0_3", feature = "serde_with-3"))]
578-
impl<'de> serde_with::DeserializeAs<'de, time::OffsetDateTime> for crate::DateTime {
579-
fn deserialize_as<D>(deserializer: D) -> std::result::Result<time::OffsetDateTime, D::Error>
580-
where
581-
D: Deserializer<'de>,
582-
{
583-
let dt = DateTime::deserialize(deserializer)?;
584-
Ok(dt.to_time_0_3())
585-
}
586-
}
587-
588-
#[cfg(all(feature = "time-0_3", feature = "serde_with-3"))]
589-
impl serde_with::SerializeAs<time::OffsetDateTime> for crate::DateTime {
590-
fn serialize_as<S>(
591-
source: &time::OffsetDateTime,
592-
serializer: S,
593-
) -> std::result::Result<S::Ok, S::Error>
594-
where
595-
S: serde::Serializer,
596-
{
597-
let dt = DateTime::from_time_0_3(*source);
598-
dt.serialize(serializer)
599-
}
600-
}

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
//! | `uuid-1` | Enable support for v1.x of the [`uuid`](https://docs.rs/uuid/1.x) crate in the public API. | no |
6767
//! | `time-0_3` | Enable support for v0.3 of the [`time`](https://docs.rs/time/0.3) crate in the public API. | no |
6868
//! | `serde` | Enable integration with the [`serde`](https://docs.rs/serde/) serialization/deserialization framework. | no |
69-
//! | `serde_with-3` | Enable [`serde_with`](https://docs.rs/serde_with/3.x) 3.x integrations for [`DateTime`] and [`Uuid`]. | no |
69+
//! | `serde_with-3` | Enable [`serde_with`](https://docs.rs/serde_with/3.x) type conversion utilities in the public API. | no |
7070
//! | `serde_path_to_error` | Enable support for error paths via integration with [`serde_path_to_error`](https://docs.rs/serde_path_to_err/latest). This is an unstable feature and any breaking changes to `serde_path_to_error` may affect usage of it via this feature. | no |
7171
//! | `compat-3-0-0` | Required for future compatibility if default features are disabled. | yes |
7272
//! | `large_dates` | Increase the supported year range for some `bson::DateTime` utilities from +/-9,999 (inclusive) to +/-999,999 (inclusive). Note that enabling this feature can impact performance and introduce parsing ambiguities. | no |

src/macros.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ macro_rules! rawdoc {
439439
///
440440
/// This macro generates a `SerializeAs`/`DeserializeAs` implementation for a given type,
441441
/// with optional struct-level attributes like `#[derive(...)]` or `/// doc comments`.
442-
#[allow(unused_macros)]
442+
#[cfg(feature = "serde")]
443443
macro_rules! serde_conv_doc {
444444
($(#[$meta:meta])* $vis:vis $m:ident, $t:ty, $ser:expr, $de:expr) => {
445445
#[allow(non_camel_case_types)]
@@ -470,7 +470,8 @@ macro_rules! serde_conv_doc {
470470
}
471471
}
472472

473-
impl SerializeAs<$t> for $m {
473+
#[cfg(feature = "serde_with-3")]
474+
impl serde_with::SerializeAs<$t> for $m {
474475
fn serialize_as<S>(x: &$t, serializer: S) -> Result<S::Ok, S::Error>
475476
where
476477
S: Serializer,
@@ -479,7 +480,8 @@ macro_rules! serde_conv_doc {
479480
}
480481
}
481482

482-
impl<'de> DeserializeAs<'de, $t> for $m {
483+
#[cfg(feature = "serde_with-3")]
484+
impl<'de> serde_with::DeserializeAs<'de, $t> for $m {
483485
fn deserialize_as<D>(deserializer: D) -> Result<$t, D::Error>
484486
where
485487
D: Deserializer<'de>,
@@ -491,5 +493,5 @@ macro_rules! serde_conv_doc {
491493
};
492494
}
493495

494-
#[allow(unused_imports)]
496+
#[cfg(feature = "serde")]
495497
pub(crate) use serde_conv_doc;

0 commit comments

Comments
 (0)