Skip to content

Commit

Permalink
fix(rust/catalyst-types): add serde::Serialize to UUID types (#147)
Browse files Browse the repository at this point in the history
  • Loading branch information
saibatizoku authored Jan 11, 2025
1 parent cb475a6 commit 3b4c144
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
12 changes: 11 additions & 1 deletion rust/catalyst-types/src/uuid/uuid_v4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -69,6 +70,15 @@ impl From<uuid::Uuid> for UuidV4 {
}
}

/// Returns a `uuid::Uuid` from `UUIDv4`.
///
/// NOTE: This does not guarantee that the `UUID` is valid.
impl From<UuidV4> for uuid::Uuid {
fn from(value: UuidV4) -> Self {
value.uuid
}
}

#[cfg(test)]
mod tests {
use coset::cbor::Value;
Expand Down
12 changes: 11 additions & 1 deletion rust/catalyst-types/src/uuid/uuid_v7.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -69,6 +70,15 @@ impl From<uuid::Uuid> for UuidV7 {
}
}

/// Returns a `uuid::Uuid` from `UUIDv7`.
///
/// NOTE: This does not guarantee that the `UUID` is valid.
impl From<UuidV7> for uuid::Uuid {
fn from(value: UuidV7) -> Self {
value.uuid
}
}

#[cfg(test)]
mod tests {
use coset::cbor::Value;
Expand Down

0 comments on commit 3b4c144

Please sign in to comment.