Skip to content

Commit 266aa30

Browse files
RUST-1748 Convert UUID serde helpers to use serde_conv (#579)
1 parent e460117 commit 266aa30

File tree

3 files changed

+445
-202
lines changed

3 files changed

+445
-202
lines changed

src/serde_helpers.rs

Lines changed: 152 additions & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,6 @@ use std::{
66
result::Result,
77
};
88

9-
#[cfg(feature = "uuid-1")]
10-
#[doc(inline)]
11-
pub use uuid_1_as_binary::{
12-
deserialize as deserialize_uuid_1_from_binary,
13-
serialize as serialize_uuid_1_as_binary,
14-
};
15-
#[cfg(feature = "uuid-1")]
16-
#[doc(inline)]
17-
pub use uuid_1_as_c_sharp_legacy_binary::{
18-
deserialize as deserialize_uuid_1_from_c_sharp_legacy_binary,
19-
serialize as serialize_uuid_1_as_c_sharp_legacy_binary,
20-
};
21-
#[cfg(feature = "uuid-1")]
22-
#[doc(inline)]
23-
pub use uuid_1_as_java_legacy_binary::{
24-
deserialize as deserialize_uuid_1_from_java_legacy_binary,
25-
serialize as serialize_uuid_1_as_java_legacy_binary,
26-
};
27-
#[cfg(feature = "uuid-1")]
28-
#[doc(inline)]
29-
pub use uuid_1_as_python_legacy_binary::{
30-
deserialize as deserialize_uuid_1_from_python_legacy_binary,
31-
serialize as serialize_uuid_1_as_python_legacy_binary,
32-
};
33-
349
/// Type converters for serializing and deserializing [`crate::oid::ObjectId`] using
3510
/// [`serde_with::serde_as`].
3611
///
@@ -116,7 +91,6 @@ pub mod datetime {
11691
use chrono::Utc;
11792
use serde::{Deserialize, Deserializer, Serialize, Serializer};
11893
use serde_with::{DeserializeAs, SerializeAs};
119-
use std::result::Result;
12094

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

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

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

459430
serde_conv_doc!(
460431
/// Converts a `u64` to and from an `f64`.
@@ -550,6 +521,158 @@ pub mod u64 {
550521
);
551522
}
552523

524+
/// Type converters for serializing and deserializing [`uuid::Uuid`] using
525+
/// [`serde_with::serde_as`].
526+
///
527+
/// ## Available converters
528+
/// - [`uuid_1::AsBinary`] — serializes a [`uuid::Uuid`] as a [`crate::Binary`].
529+
/// - [`uuid_1::AsCSharpLegacyBinary`] — serializes a [`uuid::Uuid`] as a [`crate::Binary`] in the
530+
/// legacy C# driver UUID format.
531+
/// - [`uuid_1::AsJavaLegacyBinary`] — serializes a [`uuid::Uuid`] as a [`crate::Binary`] in the
532+
/// legacy Java driver UUID format.
533+
/// - [`uuid_1::AsPythonLegacyBinary`] — serializes a [`uuid::Uuid`] as a [`crate::Binary`] in the
534+
/// legacy Python driver UUID format.
535+
#[cfg(all(feature = "serde_with-3", feature = "uuid-1"))]
536+
#[cfg_attr(docsrs, doc(cfg(all(feature = "serde_with-3", feature = "uuid-1"))))]
537+
pub mod uuid_1 {
538+
use crate::macros::serde_conv_doc;
539+
use serde::{Deserialize, Deserializer, Serialize, Serializer};
540+
use serde_with::{DeserializeAs, SerializeAs};
541+
use uuid::Uuid;
542+
543+
serde_conv_doc!(
544+
/// Serializes a [`Uuid`] as a [`crate::Binary`] and deserializes a [`Uuid`] from a [`crate::Binary`].
545+
/// ```rust
546+
/// # #[cfg(all(feature = "uuid-1", feature = "serde_with-3"))]
547+
/// # {
548+
/// use bson::serde_helpers::uuid_1;
549+
/// use serde::{Serialize, Deserialize};
550+
/// use serde_with::serde_as;
551+
/// use uuid::Uuid;
552+
/// #[serde_as]
553+
/// #[derive(Serialize, Deserialize)]
554+
/// struct Item {
555+
/// #[serde_as(as = "uuid_1::AsBinary")]
556+
/// pub id: Uuid,
557+
/// }
558+
/// # }
559+
/// ```
560+
pub AsBinary,
561+
Uuid,
562+
|uuid: &Uuid| -> Result<crate::uuid::Uuid, String> {
563+
Ok(crate::uuid::Uuid::from(*uuid))
564+
},
565+
|bson_uuid: crate::uuid::Uuid| -> Result<Uuid, String> {
566+
Ok(bson_uuid.into())
567+
}
568+
);
569+
570+
serde_conv_doc!(
571+
/// Serializes a [`Uuid`] to a [`crate::Binary`] in the legacy C# driver UUID format and
572+
/// deserializes [`Uuid`] from a [`crate::Binary`] in the legacy C# driver format.
573+
/// ```rust
574+
/// # #[cfg(all(feature = "uuid-1", feature = "serde_with-3"))]
575+
/// # {
576+
/// use bson::serde_helpers::uuid_1;
577+
/// use serde::{Serialize, Deserialize};
578+
/// use serde_with::serde_as;
579+
/// use uuid::Uuid;
580+
/// #[serde_as]
581+
/// #[derive(Serialize, Deserialize)]
582+
/// struct Item {
583+
/// #[serde_as(as = "uuid_1::AsCSharpLegacyBinary")]
584+
/// pub id: Uuid,
585+
/// }
586+
/// # }
587+
/// ```
588+
pub AsCSharpLegacyBinary,
589+
Uuid,
590+
|uuid: &Uuid| -> Result<crate::Binary, String> {
591+
let inner = crate::uuid::Uuid::from(*uuid);
592+
Ok(crate::Binary::from_uuid_with_representation(
593+
inner,
594+
crate::uuid::UuidRepresentation::CSharpLegacy,
595+
))
596+
},
597+
|binary: crate::Binary| -> Result<Uuid, String> {
598+
let inner = binary
599+
.to_uuid_with_representation(crate::uuid::UuidRepresentation::CSharpLegacy)
600+
.map_err(|e| e.to_string())?;
601+
Ok(inner.into())
602+
}
603+
);
604+
605+
serde_conv_doc!(
606+
/// Serializes a [`Uuid`] to a [`crate::Binary`] in the legacy Java driver UUID format and
607+
/// deserializes [`Uuid`] from a [`crate::Binary`] in the legacy Java driver format.
608+
/// ```rust
609+
/// # #[cfg(all(feature = "uuid-1", feature = "serde_with-3"))]
610+
/// # {
611+
/// use bson::serde_helpers::uuid_1;
612+
/// use serde::{Serialize, Deserialize};
613+
/// use serde_with::serde_as;
614+
/// use uuid::Uuid;
615+
/// #[serde_as]
616+
/// #[derive(Serialize, Deserialize)]
617+
/// struct Item {
618+
/// #[serde_as(as = "uuid_1::AsJavaLegacyBinary")]
619+
/// pub id: Uuid,
620+
/// }
621+
/// # }
622+
/// ```
623+
pub AsJavaLegacyBinary,
624+
Uuid,
625+
|uuid: &Uuid| -> Result<crate::Binary, String> {
626+
let inner = crate::uuid::Uuid::from(*uuid);
627+
Ok(crate::Binary::from_uuid_with_representation(
628+
inner,
629+
crate::uuid::UuidRepresentation::JavaLegacy,
630+
))
631+
},
632+
|binary: crate::Binary| -> Result<Uuid, String> {
633+
let inner = binary
634+
.to_uuid_with_representation(crate::uuid::UuidRepresentation::JavaLegacy)
635+
.map_err(|e| e.to_string())?;
636+
Ok(inner.into())
637+
}
638+
);
639+
640+
serde_conv_doc!(
641+
/// Serializes a [`Uuid`] to a [`crate::Binary`] in the legacy Python driver UUID format and
642+
/// deserializes [`Uuid`] from a [`crate::Binary`] in the legacy Python driver format.
643+
/// ```rust
644+
/// # #[cfg(all(feature = "uuid-1", feature = "serde_with-3"))]
645+
/// # {
646+
/// use bson::serde_helpers::uuid_1;
647+
/// use serde::{Serialize, Deserialize};
648+
/// use serde_with::serde_as;
649+
/// use uuid::Uuid;
650+
/// #[serde_as]
651+
/// #[derive(Serialize, Deserialize)]
652+
/// struct Item {
653+
/// #[serde_as(as = "uuid_1::AsPythonLegacyBinary")]
654+
/// pub id: Uuid,
655+
/// }
656+
/// # }
657+
/// ```
658+
pub AsPythonLegacyBinary,
659+
Uuid,
660+
|uuid: &Uuid| -> Result<crate::Binary, String> {
661+
let inner = crate::uuid::Uuid::from(*uuid);
662+
Ok(crate::Binary::from_uuid_with_representation(
663+
inner,
664+
crate::uuid::UuidRepresentation::PythonLegacy,
665+
))
666+
},
667+
|binary: crate::Binary| -> Result<Uuid, String> {
668+
let inner = binary
669+
.to_uuid_with_representation(crate::uuid::UuidRepresentation::PythonLegacy)
670+
.map_err(|e| e.to_string())?;
671+
Ok(inner.into())
672+
}
673+
);
674+
}
675+
553676
#[allow(unused_macros)]
554677
macro_rules! as_binary_mod {
555678
($feat:meta, $uu:path) => {
@@ -575,29 +698,6 @@ macro_rules! as_binary_mod {
575698
};
576699
}
577700

578-
/// Contains functions to serialize a [`uuid::Uuid`] as a [`crate::Binary`] and deserialize a
579-
/// [`uuid::Uuid`] from a [`crate::Binary`].
580-
///
581-
/// ```rust
582-
/// # #[cfg(feature = "uuid-1")]
583-
/// # {
584-
/// use serde::{Serialize, Deserialize};
585-
/// use uuid::Uuid;
586-
/// use bson::serde_helpers::uuid_1_as_binary;
587-
///
588-
/// #[derive(Serialize, Deserialize)]
589-
/// struct Item {
590-
/// #[serde(with = "uuid_1_as_binary")]
591-
/// pub id: Uuid,
592-
/// }
593-
/// # }
594-
/// ```
595-
#[cfg(feature = "uuid-1")]
596-
#[cfg_attr(docsrs, doc(cfg(feature = "uuid-1")))]
597-
pub mod uuid_1_as_binary {
598-
as_binary_mod!(cfg(feature = "uuid-1"), uuid::Uuid);
599-
}
600-
601701
#[allow(unused_macros)]
602702
macro_rules! as_legacy_binary_mod {
603703
($feat:meta, $uu:path, $rep:path) => {
@@ -628,90 +728,6 @@ macro_rules! as_legacy_binary_mod {
628728
};
629729
}
630730

631-
/// Contains functions to serialize a [`uuid::Uuid`] to a [`crate::Binary`] in the legacy
632-
/// Java driver UUID format and deserialize [`uuid::Uuid`] from a [`crate::Binary`] in the legacy
633-
/// Java driver format.
634-
///
635-
/// ```rust
636-
/// #[cfg(feature = "uuid-1")]
637-
/// # {
638-
/// use serde::{Serialize, Deserialize};
639-
/// use uuid::Uuid;
640-
/// use bson::serde_helpers::uuid_1_as_java_legacy_binary;
641-
///
642-
/// #[derive(Serialize, Deserialize)]
643-
/// struct Item {
644-
/// #[serde(with = "uuid_1_as_java_legacy_binary")]
645-
/// pub id: Uuid,
646-
/// }
647-
/// # }
648-
/// ```
649-
#[cfg(feature = "uuid-1")]
650-
#[cfg_attr(docsrs, doc(cfg(feature = "uuid-1")))]
651-
pub mod uuid_1_as_java_legacy_binary {
652-
as_legacy_binary_mod!(
653-
cfg(feature = "uuid-1"),
654-
uuid::Uuid,
655-
UuidRepresentation::JavaLegacy
656-
);
657-
}
658-
659-
/// Contains functions to serialize a [`uuid::Uuid`] to a [`crate::Binary`] in the legacy Python
660-
/// driver UUID format and deserialize [`uuid::Uuid`] from a [`crate::Binary`] in the legacy Python
661-
/// driver format.
662-
///
663-
/// ```rust
664-
/// # #[cfg(feature = "uuid-1")]
665-
/// # {
666-
/// use serde::{Serialize, Deserialize};
667-
/// use uuid::Uuid;
668-
/// use bson::serde_helpers::uuid_1_as_python_legacy_binary;
669-
///
670-
/// #[derive(Serialize, Deserialize)]
671-
/// struct Item {
672-
/// #[serde(with = "uuid_1_as_python_legacy_binary")]
673-
/// pub id: Uuid,
674-
/// }
675-
/// # }
676-
/// ```
677-
#[cfg(feature = "uuid-1")]
678-
#[cfg_attr(docsrs, doc(cfg(feature = "uuid-1")))]
679-
pub mod uuid_1_as_python_legacy_binary {
680-
as_legacy_binary_mod!(
681-
cfg(feature = "uuid-1"),
682-
uuid::Uuid,
683-
UuidRepresentation::PythonLegacy
684-
);
685-
}
686-
687-
/// Contains functions to serialize a [`uuid::Uuid`] to a [`crate::Binary`] in the legacy C# driver
688-
/// UUID format and deserialize [`uuid::Uuid`] from a [`crate::Binary`] in the legacy C# driver
689-
/// format.
690-
///
691-
/// ```rust
692-
/// # #[cfg(feature = "uuid-1")]
693-
/// # {
694-
/// use serde::{Serialize, Deserialize};
695-
/// use uuid::Uuid;
696-
/// use bson::serde_helpers::uuid_1_as_c_sharp_legacy_binary;
697-
///
698-
/// #[derive(Serialize, Deserialize)]
699-
/// struct Item {
700-
/// #[serde(with = "uuid_1_as_c_sharp_legacy_binary")]
701-
/// pub id: Uuid,
702-
/// }
703-
/// # }
704-
/// ```
705-
#[cfg(feature = "uuid-1")]
706-
#[cfg_attr(docsrs, doc(cfg(feature = "uuid-1")))]
707-
pub mod uuid_1_as_c_sharp_legacy_binary {
708-
as_legacy_binary_mod!(
709-
cfg(feature = "uuid-1"),
710-
uuid::Uuid,
711-
UuidRepresentation::CSharpLegacy
712-
);
713-
}
714-
715731
/// Wrapping a type in `HumanReadable` signals to the BSON serde integration that it and all
716732
/// recursively contained types should be serialized to and deserialized from their human-readable
717733
/// formats.

0 commit comments

Comments
 (0)