Skip to content

Commit b477fd5

Browse files
Implement SerializeValue and DeserializeValue for UUIDs
1 parent 6eb47b4 commit b477fd5

File tree

3 files changed

+74
-9
lines changed

3 files changed

+74
-9
lines changed

rust/catalyst-types/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ chrono = "0.4.39"
3333
fmmap = { version = "0.3.3", features = ["sync", "tokio-async"] }
3434
once_cell = "1.20.2"
3535
tracing = "0.1.41"
36+
scylla = "0.15.1"
3637

3738
[dev-dependencies]
3839
ed25519-dalek = { version = "2.1.1", features = ["rand_core"] }

rust/catalyst-types/src/uuid/uuid_v4.rs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22
use std::fmt::{Display, Formatter};
33

44
use minicbor::{Decode, Decoder, Encode};
5+
use scylla::{
6+
_macro_internal::{
7+
CellWriter, ColumnType, DeserializationError, FrameSlice, SerializationError,
8+
SerializeValue, TypeCheckError, WrittenCellProof,
9+
},
10+
deserialize::DeserializeValue,
11+
};
12+
use serde::Deserialize;
513
use uuid::Uuid;
614

715
use super::{decode_cbor_uuid, encode_cbor_uuid, CborContext, UuidError, INVALID_UUID};
@@ -97,7 +105,7 @@ impl From<UuidV4> for Uuid {
97105
impl<'de> serde::Deserialize<'de> for UuidV4 {
98106
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
99107
where D: serde::Deserializer<'de> {
100-
let uuid = Uuid::deserialize(deserializer)?;
108+
let uuid = <Uuid as Deserialize>::deserialize(deserializer)?;
101109
if is_valid(&uuid) {
102110
Ok(Self(uuid))
103111
} else {
@@ -106,6 +114,31 @@ impl<'de> serde::Deserialize<'de> for UuidV4 {
106114
}
107115
}
108116

117+
impl SerializeValue for UuidV4 {
118+
fn serialize<'b>(
119+
&self, typ: &ColumnType, writer: CellWriter<'b>,
120+
) -> Result<WrittenCellProof<'b>, SerializationError> {
121+
self.0.serialize(typ, writer)
122+
}
123+
}
124+
125+
impl<'frame, 'metadata> DeserializeValue<'frame, 'metadata> for UuidV4 {
126+
fn type_check(typ: &ColumnType) -> Result<(), TypeCheckError> {
127+
Uuid::type_check(typ)
128+
}
129+
130+
fn deserialize(
131+
typ: &'metadata ColumnType<'metadata>, v: Option<FrameSlice<'frame>>,
132+
) -> Result<Self, DeserializationError> {
133+
let uuid = <Uuid as DeserializeValue>::deserialize(typ, v)?;
134+
if is_valid(&uuid) {
135+
Ok(Self(uuid))
136+
} else {
137+
Err(DeserializationError::new(UuidError::InvalidUuidV4(uuid)))
138+
}
139+
}
140+
}
141+
109142
#[cfg(test)]
110143
mod tests {
111144
use super::*;

rust/catalyst-types/src/uuid/uuid_v7.rs

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,18 @@
22
use std::fmt::{Display, Formatter};
33

44
use minicbor::{Decode, Decoder, Encode};
5+
use scylla::_macro_internal::{
6+
CellWriter, ColumnType, DeserializationError, DeserializeValue, FrameSlice, SerializationError,
7+
SerializeValue, TypeCheckError, WrittenCellProof,
8+
};
9+
use serde::Deserialize;
10+
use uuid::Uuid;
511

612
use super::{decode_cbor_uuid, encode_cbor_uuid, CborContext, UuidError, INVALID_UUID};
713

814
/// Type representing a `UUIDv7`.
915
#[derive(Copy, Clone, Debug, PartialEq, PartialOrd, serde::Serialize)]
10-
pub struct UuidV7(uuid::Uuid);
16+
pub struct UuidV7(Uuid);
1117

1218
impl UuidV7 {
1319
/// Version for `UUIDv7`.
@@ -17,7 +23,7 @@ impl UuidV7 {
1723
#[must_use]
1824
#[allow(clippy::new_without_default)]
1925
pub fn new() -> Self {
20-
Self(uuid::Uuid::now_v7())
26+
Self(Uuid::now_v7())
2127
}
2228

2329
/// Generates a zeroed out `UUIDv7` that can never be valid.
@@ -34,13 +40,13 @@ impl UuidV7 {
3440

3541
/// Returns the `uuid::Uuid` type.
3642
#[must_use]
37-
pub fn uuid(&self) -> uuid::Uuid {
43+
pub fn uuid(&self) -> Uuid {
3844
self.0
3945
}
4046
}
4147

4248
/// Check if this is a valid `UUIDv7`.
43-
fn is_valid(uuid: &uuid::Uuid) -> bool {
49+
fn is_valid(uuid: &Uuid) -> bool {
4450
uuid != &INVALID_UUID && uuid.get_version_num() == UuidV7::UUID_VERSION_NUMBER
4551
}
4652

@@ -72,10 +78,10 @@ impl Encode<CborContext> for UuidV7 {
7278
}
7379

7480
/// Returns a `UUIDv7` from `uuid::Uuid`.
75-
impl TryFrom<uuid::Uuid> for UuidV7 {
81+
impl TryFrom<Uuid> for UuidV7 {
7682
type Error = UuidError;
7783

78-
fn try_from(uuid: uuid::Uuid) -> Result<Self, Self::Error> {
84+
fn try_from(uuid: Uuid) -> Result<Self, Self::Error> {
7985
if is_valid(&uuid) {
8086
Ok(Self(uuid))
8187
} else {
@@ -87,7 +93,7 @@ impl TryFrom<uuid::Uuid> for UuidV7 {
8793
/// Returns a `uuid::Uuid` from `UUIDv7`.
8894
///
8995
/// NOTE: This does not guarantee that the `UUID` is valid.
90-
impl From<UuidV7> for uuid::Uuid {
96+
impl From<UuidV7> for Uuid {
9197
fn from(value: UuidV7) -> Self {
9298
value.0
9399
}
@@ -96,7 +102,7 @@ impl From<UuidV7> for uuid::Uuid {
96102
impl<'de> serde::Deserialize<'de> for UuidV7 {
97103
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
98104
where D: serde::Deserializer<'de> {
99-
let uuid = uuid::Uuid::deserialize(deserializer)?;
105+
let uuid = <Uuid as Deserialize>::deserialize(deserializer)?;
100106
if is_valid(&uuid) {
101107
Ok(Self(uuid))
102108
} else {
@@ -105,6 +111,31 @@ impl<'de> serde::Deserialize<'de> for UuidV7 {
105111
}
106112
}
107113

114+
impl SerializeValue for UuidV7 {
115+
fn serialize<'b>(
116+
&self, typ: &ColumnType, writer: CellWriter<'b>,
117+
) -> Result<WrittenCellProof<'b>, SerializationError> {
118+
self.0.serialize(typ, writer)
119+
}
120+
}
121+
122+
impl<'frame, 'metadata> DeserializeValue<'frame, 'metadata> for UuidV7 {
123+
fn type_check(typ: &ColumnType) -> Result<(), TypeCheckError> {
124+
Uuid::type_check(typ)
125+
}
126+
127+
fn deserialize(
128+
typ: &'metadata ColumnType<'metadata>, v: Option<FrameSlice<'frame>>,
129+
) -> Result<Self, DeserializationError> {
130+
let uuid = <Uuid as DeserializeValue>::deserialize(typ, v)?;
131+
if is_valid(&uuid) {
132+
Ok(Self(uuid))
133+
} else {
134+
Err(DeserializationError::new(UuidError::InvalidUuidV4(uuid)))
135+
}
136+
}
137+
}
138+
108139
#[cfg(test)]
109140
mod tests {
110141
use uuid::Uuid;

0 commit comments

Comments
 (0)