11//! `UUID` types.
22
3+ #[ allow( clippy:: module_name_repetitions) ]
4+ pub use uuid_v4:: UuidV4 ;
5+ #[ allow( clippy:: module_name_repetitions) ]
6+ pub use uuid_v7:: UuidV7 ;
7+
38mod uuid_v4;
49mod uuid_v7;
510
611use minicbor:: data:: Tag ;
7- pub use uuid_v4:: UuidV4 as V4 ;
8- pub use uuid_v7:: UuidV7 as V7 ;
912
1013/// Invalid Doc Type UUID
1114pub const INVALID_UUID : uuid:: Uuid = uuid:: Uuid :: from_bytes ( [ 0x00 ; 16 ] ) ;
@@ -14,6 +17,19 @@ pub const INVALID_UUID: uuid::Uuid = uuid::Uuid::from_bytes([0x00; 16]);
1417#[ allow( dead_code) ]
1518const UUID_CBOR_TAG : u64 = 37 ;
1619
20+ /// Uuid validation errors, which could occur during decoding or converting to
21+ /// `UuidV4` or `UuidV7` types.
22+ #[ derive( Debug , Clone , thiserror:: Error ) ]
23+ #[ allow( clippy:: module_name_repetitions) ]
24+ pub enum UuidError {
25+ /// `UUIDv4` invalid error
26+ #[ error( "'{0}' is not a valid UUIDv4" ) ]
27+ InvalidUuidV4 ( uuid:: Uuid ) ,
28+ /// `UUIDv7` invalid error
29+ #[ error( "'{0}' is not a valid UUIDv7" ) ]
30+ InvalidUuidV7 ( uuid:: Uuid ) ,
31+ }
32+
1733/// Context for `CBOR` encoding and decoding
1834pub enum CborContext {
1935 /// Untagged bytes
@@ -77,30 +93,52 @@ fn encode_cbor_uuid<W: minicbor::encode::Write>(
7793#[ cfg( test) ]
7894mod tests {
7995
80- use super :: { V4 , V7 } ;
96+ use super :: * ;
8197 use crate :: uuid:: CborContext ;
8298
8399 #[ test]
84100 fn test_cbor_uuid_v4_roundtrip ( ) {
85- let uuid: V4 = uuid :: Uuid :: new_v4 ( ) . into ( ) ;
101+ let uuid = UuidV4 :: new ( ) ;
86102 let mut bytes = Vec :: new ( ) ;
87103 minicbor:: encode_with ( uuid, & mut bytes, & mut CborContext :: Untagged ) . unwrap ( ) ;
88104 let decoded = minicbor:: decode_with ( bytes. as_slice ( ) , & mut CborContext :: Untagged ) . unwrap ( ) ;
89105 assert_eq ! ( uuid, decoded) ;
90106 }
91107
108+ #[ test]
109+ fn test_cbor_uuid_v4_invalid_decoding ( ) {
110+ let uuid_v7 = UuidV7 :: new ( ) ;
111+ let mut bytes = Vec :: new ( ) ;
112+ minicbor:: encode_with ( uuid_v7, & mut bytes, & mut CborContext :: Untagged ) . unwrap ( ) ;
113+ assert ! (
114+ minicbor:: decode_with:: <_, UuidV4 >( bytes. as_slice( ) , & mut CborContext :: Untagged )
115+ . is_err( )
116+ ) ;
117+ }
118+
92119 #[ test]
93120 fn test_cbor_uuid_v7_roundtrip ( ) {
94- let uuid: V7 = uuid :: Uuid :: now_v7 ( ) . into ( ) ;
121+ let uuid = UuidV7 :: new ( ) ;
95122 let mut bytes = Vec :: new ( ) ;
96123 minicbor:: encode_with ( uuid, & mut bytes, & mut CborContext :: Untagged ) . unwrap ( ) ;
97124 let decoded = minicbor:: decode_with ( bytes. as_slice ( ) , & mut CborContext :: Untagged ) . unwrap ( ) ;
98125 assert_eq ! ( uuid, decoded) ;
99126 }
100127
128+ #[ test]
129+ fn test_cbor_uuid_v7_invalid_decoding ( ) {
130+ let uuid_v4 = UuidV4 :: new ( ) ;
131+ let mut bytes = Vec :: new ( ) ;
132+ minicbor:: encode_with ( uuid_v4, & mut bytes, & mut CborContext :: Untagged ) . unwrap ( ) ;
133+ assert ! (
134+ minicbor:: decode_with:: <_, UuidV7 >( bytes. as_slice( ) , & mut CborContext :: Untagged )
135+ . is_err( )
136+ ) ;
137+ }
138+
101139 #[ test]
102140 fn test_tagged_cbor_uuid_v4_roundtrip ( ) {
103- let uuid: V4 = uuid :: Uuid :: new_v4 ( ) . into ( ) ;
141+ let uuid = UuidV4 :: new ( ) ;
104142 let mut bytes = Vec :: new ( ) ;
105143 minicbor:: encode_with ( uuid, & mut bytes, & mut CborContext :: Tagged ) . unwrap ( ) ;
106144 let decoded = minicbor:: decode_with ( bytes. as_slice ( ) , & mut CborContext :: Tagged ) . unwrap ( ) ;
@@ -109,7 +147,7 @@ mod tests {
109147
110148 #[ test]
111149 fn test_tagged_cbor_uuid_v7_roundtrip ( ) {
112- let uuid: V7 = uuid :: Uuid :: now_v7 ( ) . into ( ) ;
150+ let uuid = UuidV7 :: new ( ) ;
113151 let mut bytes = Vec :: new ( ) ;
114152 minicbor:: encode_with ( uuid, & mut bytes, & mut CborContext :: Tagged ) . unwrap ( ) ;
115153 let decoded = minicbor:: decode_with ( bytes. as_slice ( ) , & mut CborContext :: Tagged ) . unwrap ( ) ;
@@ -118,7 +156,7 @@ mod tests {
118156
119157 #[ test]
120158 fn test_optional_cbor_uuid_v4_roundtrip ( ) {
121- let uuid: V4 = uuid :: Uuid :: new_v4 ( ) . into ( ) ;
159+ let uuid = UuidV4 :: new ( ) ;
122160
123161 let mut bytes = Vec :: new ( ) ;
124162 minicbor:: encode_with ( uuid, & mut bytes, & mut CborContext :: Untagged ) . unwrap ( ) ;
@@ -133,7 +171,7 @@ mod tests {
133171
134172 #[ test]
135173 fn test_optional_cbor_uuid_v7_roundtrip ( ) {
136- let uuid: V7 = uuid :: Uuid :: now_v7 ( ) . into ( ) ;
174+ let uuid = UuidV7 :: new ( ) ;
137175
138176 let mut bytes = Vec :: new ( ) ;
139177 minicbor:: encode_with ( uuid, & mut bytes, & mut CborContext :: Untagged ) . unwrap ( ) ;
0 commit comments