11//! `UUID` types.
22
33use displaydoc:: Display ;
4- use minicbor:: Decoder ;
4+ use minicbor:: { data :: Tag , Decoder } ;
55use thiserror:: Error ;
66use uuid:: Uuid ;
77
@@ -14,6 +14,9 @@ pub use uuid_v7::UuidV7 as V7;
1414/// Invalid Doc Type UUID
1515pub const INVALID_UUID : uuid:: Uuid = uuid:: Uuid :: from_bytes ( [ 0x00 ; 16 ] ) ;
1616
17+ /// UUID CBOR tag <https://www.iana.org/assignments/cbor-tags/cbor-tags.xhtml/>.
18+ pub ( crate ) const UUID_CBOR_TAG : u64 = 37 ;
19+
1720/// Errors that can occur when decoding CBOR-encoded UUIDs.
1821#[ derive( Display , Debug , Error ) ]
1922pub enum CborUuidError {
@@ -34,9 +37,17 @@ pub enum CborUuidError {
3437pub ( crate ) fn decode_cbor_uuid (
3538 d : & mut Decoder < ' _ > , ( ) : & mut ( ) ,
3639) -> Result < uuid:: Uuid , minicbor:: decode:: Error > {
37- let decoded = d. bytes ( ) ?. try_into ( ) . map_err ( |e| {
38- minicbor:: decode:: Error :: message ( format ! ( "Expected UUID to have 16 bytes: {e}" ) )
39- } ) ?;
40+ let tag = d. tag ( ) ?;
41+ if UUID_CBOR_TAG != tag. as_u64 ( ) {
42+ return Err ( minicbor:: decode:: Error :: message ( format ! (
43+ "tag value must be: {UUID_CBOR_TAG}, provided: {}" ,
44+ tag. as_u64( ) ,
45+ ) ) ) ;
46+ }
47+ let decoded = d
48+ . bytes ( ) ?
49+ . try_into ( )
50+ . map_err ( |_| minicbor:: decode:: Error :: message ( "Expected UUID to have 16 bytes" ) ) ?;
4051 let uuid = uuid:: Uuid :: from_bytes ( decoded) ;
4152 Ok ( uuid)
4253}
@@ -45,6 +56,7 @@ pub(crate) fn decode_cbor_uuid(
4556pub ( crate ) fn encode_cbor_uuid < W : minicbor:: encode:: Write > (
4657 uuid : uuid:: Uuid , e : & mut minicbor:: Encoder < W > , ( ) : & mut ( ) ,
4758) -> Result < ( ) , minicbor:: encode:: Error < W :: Error > > {
59+ e. tag ( Tag :: new ( UUID_CBOR_TAG ) ) ?;
4860 e. bytes ( uuid. as_bytes ( ) ) ?;
4961 Ok ( ( ) )
5062}
0 commit comments