Skip to content

RUST-1748 Convert UUID serde helpers to use serde_conv #579

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 70 commits into from
Jul 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
70 commits
Select commit Hold shift + click to select a range
a54433e
Refactor BSON DateTime serde converters using serde_conv_doc macro
JamieTsai1024 Jul 8, 2025
a51f29e
Refactor OffsetDateTime and i64 to DateTime helpers to use serde_conv
JamieTsai1024 Jul 8, 2025
03e9a51
Refactor type converters betweem timestamp and u32 with serde_conv
JamieTsai1024 Jul 9, 2025
f1577fd
Refactor u32 and u64 to f64 converters to use serde_conv
JamieTsai1024 Jul 9, 2025
b8ad904
Rename u32 module to timestamp
JamieTsai1024 Jul 9, 2025
a1ad2de
Clean up remaining u32_as_timestamp converter
JamieTsai1024 Jul 9, 2025
5529ec8
Fix struct capitalization
JamieTsai1024 Jul 10, 2025
a7c182a
Refactor unsigned (u32, u64) to signed (i32, i64) converters using se…
JamieTsai1024 Jul 10, 2025
693faa8
Consolidate module names under u32 and u64
JamieTsai1024 Jul 10, 2025
a9873d3
Consolidate module names under u32 and u64
JamieTsai1024 Jul 10, 2025
b9b08cf
Refactor uuid to binary converter to use serde_conv
JamieTsai1024 Jul 11, 2025
1ccea32
Reorder tests based on serde_helpers ordering
JamieTsai1024 Jul 11, 2025
1db7343
Refactor uuid to legacy binary converters using serde_conv
JamieTsai1024 Jul 11, 2025
4cdaf4b
Shorten vector field assertions
JamieTsai1024 Jul 11, 2025
61e2b05
Remove unused imports
JamieTsai1024 Jul 11, 2025
b566c08
Shorten deserialization assertions
JamieTsai1024 Jul 11, 2025
181bcd2
Add documentation for modules
JamieTsai1024 Jul 11, 2025
ed526cb
Reorder imports in documentation
JamieTsai1024 Jul 11, 2025
a63f82e
Shorten *_optional_some field assertions
JamieTsai1024 Jul 11, 2025
2298c43
Remove bson from datetime helpers module name
JamieTsai1024 Jul 14, 2025
7cdc3d9
Add RFC 3339 format to error messages
JamieTsai1024 Jul 14, 2025
76349a0
Fix typo
JamieTsai1024 Jul 14, 2025
4e4291e
Fix typo 'DatetTime'
JamieTsai1024 Jul 14, 2025
e9b14b8
Refactor DateTime FromI64 and FromTime03OffsetDateTime using serde_co…
JamieTsai1024 Jul 14, 2025
212ea0b
Add rustdoc comments to module and structs
JamieTsai1024 Jul 14, 2025
03027d1
Switch from clone through to_owned() to dereference
JamieTsai1024 Jul 14, 2025
7077c0b
Rename bson_datetime module to datetime
JamieTsai1024 Jul 14, 2025
ebe3a99
Remove extra rustdocs ///
JamieTsai1024 Jul 14, 2025
9f41ee6
Add DateTime import to fix lint on rustdocs
JamieTsai1024 Jul 14, 2025
e6b69c5
Merge branch 'RUST-1748-serde_conv-2' into RUST-1748-serde_conv-3
JamieTsai1024 Jul 14, 2025
2d26b59
Remove DateTime import for rustdocs to avoid clippy warning
JamieTsai1024 Jul 14, 2025
d3d9a95
Add missing conditional compilation to OffsetDateTime struct
JamieTsai1024 Jul 14, 2025
e1e4dae
Merge branch 2 into branch 3
JamieTsai1024 Jul 14, 2025
ae2b0a4
Fix rustdoc imports
JamieTsai1024 Jul 14, 2025
3a0e345
Update rustdocs phrasing and improve struct naming for Chrono04DateTime
JamieTsai1024 Jul 14, 2025
f527aff
Split tests for DateTime
JamieTsai1024 Jul 14, 2025
c14f554
Merge branch 'RUST-1748-serde_conv-2' into RUST-1748-serde_conv-3
JamieTsai1024 Jul 14, 2025
f834214
Add back deleted u32 tests from merge
JamieTsai1024 Jul 14, 2025
ec82af4
Fix datetime rustdoc comments from merge
JamieTsai1024 Jul 14, 2025
47cbddd
Update rustdoc comments
JamieTsai1024 Jul 14, 2025
fe5b5e4
clippy test: revert FromTime03OffsetDateTime to FromTimeOffsetDateTime
JamieTsai1024 Jul 14, 2025
ca6f68f
Rename FromTime03OffsetDateTime
JamieTsai1024 Jul 14, 2025
b85d7d0
Merge branch 'RUST-1748-serde_conv-2' into RUST-1748-serde_conv-3
JamieTsai1024 Jul 14, 2025
d4900fd
Merge branch 'RUST-1748-serde_conv-3' into RUST-1748-serde_conv-4
JamieTsai1024 Jul 14, 2025
53090e9
Revert all UUID changes (separate to another PR)
JamieTsai1024 Jul 14, 2025
f0c3833
Fix rustdoc import
JamieTsai1024 Jul 14, 2025
00a4f9c
Copy documentation and test changes for ObjectId
JamieTsai1024 Jul 14, 2025
3103c27
Try uncommenting conditional compilation on rustdoc comments
JamieTsai1024 Jul 15, 2025
6f74ead
Undo uncommenting rustdocs
JamieTsai1024 Jul 15, 2025
a3dd1b8
Fix clippy attempt - move conditional compilation attribute
JamieTsai1024 Jul 15, 2025
f81dfd2
Revert all ObjectId changes (separate to another PR)
JamieTsai1024 Jul 15, 2025
d3eb97e
Revert "Revert all ObjectId changes (separate to another PR)"
JamieTsai1024 Jul 15, 2025
60b6c4f
Move #[cfg(feature = serde_with-3)] attribute to test function
JamieTsai1024 Jul 15, 2025
d91ba76
Break tests into smaller functions
JamieTsai1024 Jul 15, 2025
de0b400
Remove whitespace diffs
JamieTsai1024 Jul 15, 2025
844b50c
Merge branch 'RUST-1748-serde_conv-2' into RUST-1748-serde_conv-3
JamieTsai1024 Jul 15, 2025
e9334c5
Merge branch 'RUST-1748-serde_conv-3' into RUST-1748-serde_conv-3.5
JamieTsai1024 Jul 15, 2025
93d8efe
Merge branch 'RUST-1748-serde_conv-3.5' into RUST-1748-serde_conv-4
JamieTsai1024 Jul 15, 2025
6d4951c
Fix whitespace
JamieTsai1024 Jul 15, 2025
26419ad
Merge branch 'RUST-1748-serde_conv-3.5' into RUST-1748-serde_conv-4
JamieTsai1024 Jul 15, 2025
5ca198f
Fix rustdocs whitespace
JamieTsai1024 Jul 15, 2025
29b289a
Fix rustdocs whitespace and imports
JamieTsai1024 Jul 15, 2025
57a763b
Merge branch 'RUST-1748-serde_conv-3.5' into RUST-1748-serde_conv-4
JamieTsai1024 Jul 15, 2025
25324ca
Update rustdocs
JamieTsai1024 Jul 15, 2025
fbe3bfc
Fix rustdoc imports
JamieTsai1024 Jul 15, 2025
9087456
Merge branch 'main' into RUST-1748-serde_conv-4
JamieTsai1024 Jul 21, 2025
b0d6844
Add back missing uuid tests lost in PR split
JamieTsai1024 Jul 22, 2025
43f94a1
Remove Result imports in tests
JamieTsai1024 Jul 22, 2025
b892c34
Fix lint with Binary unused import
JamieTsai1024 Jul 22, 2025
8b6ec90
Fix rustdoc typo
JamieTsai1024 Jul 23, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
288 changes: 152 additions & 136 deletions src/serde_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,6 @@ use std::{
result::Result,
};

#[cfg(feature = "uuid-1")]
#[doc(inline)]
pub use uuid_1_as_binary::{
deserialize as deserialize_uuid_1_from_binary,
serialize as serialize_uuid_1_as_binary,
};
#[cfg(feature = "uuid-1")]
#[doc(inline)]
pub use uuid_1_as_c_sharp_legacy_binary::{
deserialize as deserialize_uuid_1_from_c_sharp_legacy_binary,
serialize as serialize_uuid_1_as_c_sharp_legacy_binary,
};
#[cfg(feature = "uuid-1")]
#[doc(inline)]
pub use uuid_1_as_java_legacy_binary::{
deserialize as deserialize_uuid_1_from_java_legacy_binary,
serialize as serialize_uuid_1_as_java_legacy_binary,
};
#[cfg(feature = "uuid-1")]
#[doc(inline)]
pub use uuid_1_as_python_legacy_binary::{
deserialize as deserialize_uuid_1_from_python_legacy_binary,
serialize as serialize_uuid_1_as_python_legacy_binary,
};

/// Type converters for serializing and deserializing [`crate::oid::ObjectId`] using
/// [`serde_with::serde_as`].
///
Expand Down Expand Up @@ -116,7 +91,6 @@ pub mod datetime {
use chrono::Utc;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serde_with::{DeserializeAs, SerializeAs};
use std::result::Result;

serde_conv_doc!(
/// Converts a [`DateTime`] to and from an RFC 3339 (ISO 8601) formatted string.
Expand Down Expand Up @@ -270,7 +244,6 @@ pub mod timestamp {
use crate::{macros::serde_conv_doc, Timestamp};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serde_with::{DeserializeAs, SerializeAs};
use std::result::Result;

serde_conv_doc!(
/// Converts a [`Timestamp`] to and from a `u32`.
Expand Down Expand Up @@ -351,7 +324,6 @@ pub mod u32 {
use crate::macros::serde_conv_doc;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serde_with::{DeserializeAs, SerializeAs};
use std::result::Result;

serde_conv_doc!(
/// Converts a `u32` to and from an `f64`.
Expand Down Expand Up @@ -454,7 +426,6 @@ pub mod u64 {
use crate::macros::serde_conv_doc;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serde_with::{DeserializeAs, SerializeAs};
use std::result::Result;

serde_conv_doc!(
/// Converts a `u64` to and from an `f64`.
Expand Down Expand Up @@ -550,6 +521,158 @@ pub mod u64 {
);
}

/// Type converters for serializing and deserializing [`uuid::Uuid`] using
/// [`serde_with::serde_as`].
///
/// ## Available converters
/// - [`uuid_1::AsBinary`] — serializes a [`uuid::Uuid`] as a [`crate::Binary`].
/// - [`uuid_1::AsCSharpLegacyBinary`] — serializes a [`uuid::Uuid`] as a [`crate::Binary`] in the
/// legacy C# driver UUID format.
/// - [`uuid_1::AsJavaLegacyBinary`] — serializes a [`uuid::Uuid`] as a [`crate::Binary`] in the
/// legacy Java driver UUID format.
/// - [`uuid_1::AsPythonLegacyBinary`] — serializes a [`uuid::Uuid`] as a [`crate::Binary`] in the
/// legacy Python driver UUID format.
#[cfg(all(feature = "serde_with-3", feature = "uuid-1"))]
#[cfg_attr(docsrs, doc(cfg(all(feature = "serde_with-3", feature = "uuid-1"))))]
pub mod uuid_1 {
use crate::macros::serde_conv_doc;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serde_with::{DeserializeAs, SerializeAs};
use uuid::Uuid;

serde_conv_doc!(
/// Serializes a [`Uuid`] as a [`crate::Binary`] and deserializes a [`Uuid`] from a [`crate::Binary`].
/// ```rust
/// # #[cfg(all(feature = "uuid-1", feature = "serde_with-3"))]
/// # {
/// use bson::serde_helpers::uuid_1;
/// use serde::{Serialize, Deserialize};
/// use serde_with::serde_as;
/// use uuid::Uuid;
/// #[serde_as]
/// #[derive(Serialize, Deserialize)]
/// struct Item {
/// #[serde_as(as = "uuid_1::AsBinary")]
/// pub id: Uuid,
/// }
/// # }
/// ```
pub AsBinary,
Uuid,
|uuid: &Uuid| -> Result<crate::uuid::Uuid, String> {
Ok(crate::uuid::Uuid::from(*uuid))
},
|bson_uuid: crate::uuid::Uuid| -> Result<Uuid, String> {
Ok(bson_uuid.into())
}
);

serde_conv_doc!(
/// Serializes a [`Uuid`] to a [`crate::Binary`] in the legacy C# driver UUID format and
/// deserializes [`Uuid`] from a [`crate::Binary`] in the legacy C# driver format.
/// ```rust
/// # #[cfg(all(feature = "uuid-1", feature = "serde_with-3"))]
/// # {
/// use bson::serde_helpers::uuid_1;
/// use serde::{Serialize, Deserialize};
/// use serde_with::serde_as;
/// use uuid::Uuid;
/// #[serde_as]
/// #[derive(Serialize, Deserialize)]
/// struct Item {
/// #[serde_as(as = "uuid_1::AsCSharpLegacyBinary")]
/// pub id: Uuid,
/// }
/// # }
/// ```
pub AsCSharpLegacyBinary,
Uuid,
|uuid: &Uuid| -> Result<crate::Binary, String> {
let inner = crate::uuid::Uuid::from(*uuid);
Ok(crate::Binary::from_uuid_with_representation(
inner,
crate::uuid::UuidRepresentation::CSharpLegacy,
))
},
|binary: crate::Binary| -> Result<Uuid, String> {
let inner = binary
.to_uuid_with_representation(crate::uuid::UuidRepresentation::CSharpLegacy)
.map_err(|e| e.to_string())?;
Ok(inner.into())
}
);

serde_conv_doc!(
/// Serializes a [`Uuid`] to a [`crate::Binary`] in the legacy Java driver UUID format and
/// deserializes [`Uuid`] from a [`crate::Binary`] in the legacy Java driver format.
/// ```rust
/// # #[cfg(all(feature = "uuid-1", feature = "serde_with-3"))]
/// # {
/// use bson::serde_helpers::uuid_1;
/// use serde::{Serialize, Deserialize};
/// use serde_with::serde_as;
/// use uuid::Uuid;
/// #[serde_as]
/// #[derive(Serialize, Deserialize)]
/// struct Item {
/// #[serde_as(as = "uuid_1::AsJavaLegacyBinary")]
/// pub id: Uuid,
/// }
/// # }
/// ```
pub AsJavaLegacyBinary,
Uuid,
|uuid: &Uuid| -> Result<crate::Binary, String> {
let inner = crate::uuid::Uuid::from(*uuid);
Ok(crate::Binary::from_uuid_with_representation(
inner,
crate::uuid::UuidRepresentation::JavaLegacy,
))
},
|binary: crate::Binary| -> Result<Uuid, String> {
let inner = binary
.to_uuid_with_representation(crate::uuid::UuidRepresentation::JavaLegacy)
.map_err(|e| e.to_string())?;
Ok(inner.into())
}
);

serde_conv_doc!(
/// Serializes a [`Uuid`] to a [`crate::Binary`] in the legacy Python driver UUID format and
/// deserializes [`Uuid`] from a [`crate::Binary`] in the legacy Python driver format.
/// ```rust
/// # #[cfg(all(feature = "uuid-1", feature = "serde_with-3"))]
/// # {
/// use bson::serde_helpers::uuid_1;
/// use serde::{Serialize, Deserialize};
/// use serde_with::serde_as;
/// use uuid::Uuid;
/// #[serde_as]
/// #[derive(Serialize, Deserialize)]
/// struct Item {
/// #[serde_as(as = "uuid_1::AsPythonLegacyBinary")]
/// pub id: Uuid,
/// }
/// # }
/// ```
pub AsPythonLegacyBinary,
Uuid,
|uuid: &Uuid| -> Result<crate::Binary, String> {
let inner = crate::uuid::Uuid::from(*uuid);
Ok(crate::Binary::from_uuid_with_representation(
inner,
crate::uuid::UuidRepresentation::PythonLegacy,
))
},
|binary: crate::Binary| -> Result<Uuid, String> {
let inner = binary
.to_uuid_with_representation(crate::uuid::UuidRepresentation::PythonLegacy)
.map_err(|e| e.to_string())?;
Ok(inner.into())
}
);
}

#[allow(unused_macros)]
macro_rules! as_binary_mod {
($feat:meta, $uu:path) => {
Expand All @@ -575,29 +698,6 @@ macro_rules! as_binary_mod {
};
}

/// Contains functions to serialize a [`uuid::Uuid`] as a [`crate::Binary`] and deserialize a
/// [`uuid::Uuid`] from a [`crate::Binary`].
///
/// ```rust
/// # #[cfg(feature = "uuid-1")]
/// # {
/// use serde::{Serialize, Deserialize};
/// use uuid::Uuid;
/// use bson::serde_helpers::uuid_1_as_binary;
///
/// #[derive(Serialize, Deserialize)]
/// struct Item {
/// #[serde(with = "uuid_1_as_binary")]
/// pub id: Uuid,
/// }
/// # }
/// ```
#[cfg(feature = "uuid-1")]
#[cfg_attr(docsrs, doc(cfg(feature = "uuid-1")))]
pub mod uuid_1_as_binary {
as_binary_mod!(cfg(feature = "uuid-1"), uuid::Uuid);
}

#[allow(unused_macros)]
macro_rules! as_legacy_binary_mod {
($feat:meta, $uu:path, $rep:path) => {
Expand Down Expand Up @@ -628,90 +728,6 @@ macro_rules! as_legacy_binary_mod {
};
}

/// Contains functions to serialize a [`uuid::Uuid`] to a [`crate::Binary`] in the legacy
/// Java driver UUID format and deserialize [`uuid::Uuid`] from a [`crate::Binary`] in the legacy
/// Java driver format.
///
/// ```rust
/// #[cfg(feature = "uuid-1")]
/// # {
/// use serde::{Serialize, Deserialize};
/// use uuid::Uuid;
/// use bson::serde_helpers::uuid_1_as_java_legacy_binary;
///
/// #[derive(Serialize, Deserialize)]
/// struct Item {
/// #[serde(with = "uuid_1_as_java_legacy_binary")]
/// pub id: Uuid,
/// }
/// # }
/// ```
#[cfg(feature = "uuid-1")]
#[cfg_attr(docsrs, doc(cfg(feature = "uuid-1")))]
pub mod uuid_1_as_java_legacy_binary {
as_legacy_binary_mod!(
cfg(feature = "uuid-1"),
uuid::Uuid,
UuidRepresentation::JavaLegacy
);
}

/// Contains functions to serialize a [`uuid::Uuid`] to a [`crate::Binary`] in the legacy Python
/// driver UUID format and deserialize [`uuid::Uuid`] from a [`crate::Binary`] in the legacy Python
/// driver format.
///
/// ```rust
/// # #[cfg(feature = "uuid-1")]
/// # {
/// use serde::{Serialize, Deserialize};
/// use uuid::Uuid;
/// use bson::serde_helpers::uuid_1_as_python_legacy_binary;
///
/// #[derive(Serialize, Deserialize)]
/// struct Item {
/// #[serde(with = "uuid_1_as_python_legacy_binary")]
/// pub id: Uuid,
/// }
/// # }
/// ```
#[cfg(feature = "uuid-1")]
#[cfg_attr(docsrs, doc(cfg(feature = "uuid-1")))]
pub mod uuid_1_as_python_legacy_binary {
as_legacy_binary_mod!(
cfg(feature = "uuid-1"),
uuid::Uuid,
UuidRepresentation::PythonLegacy
);
}

/// Contains functions to serialize a [`uuid::Uuid`] to a [`crate::Binary`] in the legacy C# driver
/// UUID format and deserialize [`uuid::Uuid`] from a [`crate::Binary`] in the legacy C# driver
/// format.
///
/// ```rust
/// # #[cfg(feature = "uuid-1")]
/// # {
/// use serde::{Serialize, Deserialize};
/// use uuid::Uuid;
/// use bson::serde_helpers::uuid_1_as_c_sharp_legacy_binary;
///
/// #[derive(Serialize, Deserialize)]
/// struct Item {
/// #[serde(with = "uuid_1_as_c_sharp_legacy_binary")]
/// pub id: Uuid,
/// }
/// # }
/// ```
#[cfg(feature = "uuid-1")]
#[cfg_attr(docsrs, doc(cfg(feature = "uuid-1")))]
pub mod uuid_1_as_c_sharp_legacy_binary {
as_legacy_binary_mod!(
cfg(feature = "uuid-1"),
uuid::Uuid,
UuidRepresentation::CSharpLegacy
);
}

/// Wrapping a type in `HumanReadable` signals to the BSON serde integration that it and all
/// recursively contained types should be serialized to and deserialized from their human-readable
/// formats.
Expand Down
Loading