@@ -13,11 +13,46 @@ pub const INVALID_UUID: uuid::Uuid = uuid::Uuid::from_bytes([0x00; 16]);
1313#[ allow( dead_code) ]
1414const UUID_CBOR_TAG : u64 = 37 ;
1515
16+ /// Context for `CBOR` encoding and decoding
17+ pub enum CborContext {
18+ /// Untagged bytes
19+ Untagged ,
20+ /// IANA CBOR tag and bytes
21+ Tagged ,
22+ /// Optional tag
23+ Optional ,
24+ }
25+
1626/// Decode from `CBOR` into `UUID`
17- fn decode_cbor_uuid < C > (
18- d : & mut minicbor:: Decoder < ' _ > , _ctx : & mut C ,
27+ fn decode_cbor_uuid (
28+ d : & mut minicbor:: Decoder < ' _ > , ctx : & mut CborContext ,
1929) -> Result < uuid:: Uuid , minicbor:: decode:: Error > {
20- let decoded: [ u8 ; 16 ] = d. bytes ( ) ?. try_into ( ) . map_err ( |_| {
30+ let bytes = match ctx {
31+ CborContext :: Untagged => d. bytes ( ) ?,
32+ CborContext :: Tagged => {
33+ let tag = d. tag ( ) ?. as_u64 ( ) ;
34+ if UUID_CBOR_TAG == tag {
35+ return Err ( minicbor:: decode:: Error :: message ( format ! (
36+ "tag value must be: {UUID_CBOR_TAG}, provided: {tag}"
37+ ) ) ) ;
38+ }
39+ d. bytes ( ) ?
40+ } ,
41+ CborContext :: Optional => {
42+ if let Ok ( tag) = d. tag ( ) {
43+ let tag = tag. as_u64 ( ) ;
44+ if UUID_CBOR_TAG == tag {
45+ return Err ( minicbor:: decode:: Error :: message ( format ! (
46+ "tag value must be: {UUID_CBOR_TAG}, provided: {tag}"
47+ ) ) ) ;
48+ }
49+ d. bytes ( ) ?
50+ } else {
51+ d. bytes ( ) ?
52+ }
53+ } ,
54+ } ;
55+ let decoded: [ u8 ; 16 ] = bytes. try_into ( ) . map_err ( |_| {
2156 minicbor:: decode:: Error :: message ( "Invalid CBOR encoded UUID type: invalid bytes size" )
2257 } ) ?;
2358 let uuid = uuid:: Uuid :: from_bytes ( decoded) ;
@@ -34,9 +69,10 @@ fn encode_cbor_uuid<C, W: minicbor::encode::Write>(
3469
3570#[ cfg( test) ]
3671mod tests {
37- use minicbor:: data:: Tagged ;
72+ use minicbor:: data:: { Tag , Tagged } ;
3873
3974 use super :: { V4 , V7 } ;
75+ use crate :: uuid:: CborContext ;
4076
4177 const UUID_CBOR_TAG : u64 = 37 ;
4278
@@ -45,7 +81,7 @@ mod tests {
4581 let uuid: V4 = uuid:: Uuid :: new_v4 ( ) . into ( ) ;
4682 let mut bytes = Vec :: new ( ) ;
4783 minicbor:: encode ( uuid, & mut bytes) . unwrap ( ) ;
48- let decoded = minicbor:: decode ( bytes. as_slice ( ) ) . unwrap ( ) ;
84+ let decoded = minicbor:: decode_with ( bytes. as_slice ( ) , & mut CborContext :: Untagged ) . unwrap ( ) ;
4985 assert_eq ! ( uuid, decoded) ;
5086 }
5187
@@ -54,7 +90,7 @@ mod tests {
5490 let uuid: V7 = uuid:: Uuid :: now_v7 ( ) . into ( ) ;
5591 let mut bytes = Vec :: new ( ) ;
5692 minicbor:: encode ( uuid, & mut bytes) . unwrap ( ) ;
57- let decoded = minicbor:: decode ( bytes. as_slice ( ) ) . unwrap ( ) ;
93+ let decoded = minicbor:: decode_with ( bytes. as_slice ( ) , & mut CborContext :: Untagged ) . unwrap ( ) ;
5894 assert_eq ! ( uuid, decoded) ;
5995 }
6096
@@ -64,7 +100,7 @@ mod tests {
64100 let tagged: Tagged < UUID_CBOR_TAG , V4 > = uuid. into ( ) ;
65101 let mut bytes = Vec :: new ( ) ;
66102 minicbor:: encode ( tagged, & mut bytes) . unwrap ( ) ;
67- let decoded = minicbor:: decode ( bytes. as_slice ( ) ) . unwrap ( ) ;
103+ let decoded = minicbor:: decode_with ( bytes. as_slice ( ) , & mut CborContext :: Untagged ) . unwrap ( ) ;
68104 assert_eq ! ( tagged, decoded) ;
69105 }
70106
@@ -74,7 +110,8 @@ mod tests {
74110 let tagged: Tagged < UUID_CBOR_TAG , V7 > = uuid. into ( ) ;
75111 let mut bytes = Vec :: new ( ) ;
76112 minicbor:: encode ( tagged, & mut bytes) . unwrap ( ) ;
77- let decoded = minicbor:: decode ( bytes. as_slice ( ) ) . unwrap ( ) ;
113+ let decoded = minicbor:: decode_with ( bytes. as_slice ( ) , & mut CborContext :: Untagged ) . unwrap ( ) ;
78114 assert_eq ! ( tagged, decoded) ;
115+ assert_eq ! ( tagged. tag( ) , Tag :: new( UUID_CBOR_TAG ) ) ;
79116 }
80117}
0 commit comments