From 3b4c144b0ea68f58af5c10205475b7e8e11825cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joaqu=C3=ADn=20Rosales?= Date: Sat, 11 Jan 2025 05:13:19 -0600 Subject: [PATCH] fix(rust/catalyst-types): add serde::Serialize to UUID types (#147) --- rust/catalyst-types/src/uuid/uuid_v4.rs | 12 +++++++++++- rust/catalyst-types/src/uuid/uuid_v7.rs | 12 +++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/rust/catalyst-types/src/uuid/uuid_v4.rs b/rust/catalyst-types/src/uuid/uuid_v4.rs index 64360cf6c..803d53dde 100644 --- a/rust/catalyst-types/src/uuid/uuid_v4.rs +++ b/rust/catalyst-types/src/uuid/uuid_v4.rs @@ -4,8 +4,9 @@ use std::fmt::{Display, Formatter}; use super::{decode_cbor_uuid, INVALID_UUID}; /// Type representing a `UUIDv4`. -#[derive(Copy, Clone, Debug, PartialEq, PartialOrd, serde::Deserialize)] +#[derive(Copy, Clone, Debug, PartialEq, PartialOrd, serde::Serialize, serde::Deserialize)] #[serde(from = "uuid::Uuid")] +#[serde(into = "uuid::Uuid")] pub struct UuidV4 { /// UUID uuid: uuid::Uuid, @@ -69,6 +70,15 @@ impl From for UuidV4 { } } +/// Returns a `uuid::Uuid` from `UUIDv4`. +/// +/// NOTE: This does not guarantee that the `UUID` is valid. +impl From for uuid::Uuid { + fn from(value: UuidV4) -> Self { + value.uuid + } +} + #[cfg(test)] mod tests { use coset::cbor::Value; diff --git a/rust/catalyst-types/src/uuid/uuid_v7.rs b/rust/catalyst-types/src/uuid/uuid_v7.rs index f6aec76cc..ba6d65687 100644 --- a/rust/catalyst-types/src/uuid/uuid_v7.rs +++ b/rust/catalyst-types/src/uuid/uuid_v7.rs @@ -4,8 +4,9 @@ use std::fmt::{Display, Formatter}; use super::{decode_cbor_uuid, INVALID_UUID}; /// Type representing a `UUIDv7`. -#[derive(Copy, Clone, Debug, PartialEq, PartialOrd, serde::Deserialize)] +#[derive(Copy, Clone, Debug, PartialEq, PartialOrd, serde::Serialize, serde::Deserialize)] #[serde(from = "uuid::Uuid")] +#[serde(into = "uuid::Uuid")] pub struct UuidV7 { /// UUID uuid: uuid::Uuid, @@ -69,6 +70,15 @@ impl From for UuidV7 { } } +/// Returns a `uuid::Uuid` from `UUIDv7`. +/// +/// NOTE: This does not guarantee that the `UUID` is valid. +impl From for uuid::Uuid { + fn from(value: UuidV7) -> Self { + value.uuid + } +} + #[cfg(test)] mod tests { use coset::cbor::Value;