22use std:: fmt:: { Display , Formatter } ;
33
44use 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
612use 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
1218impl 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 {
96102impl < ' 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) ]
109140mod tests {
110141 use uuid:: Uuid ;
0 commit comments