Skip to content

Commit 257c0ff

Browse files
committed
fix(rust/catalyst-types): fix uuid cbor and add tests
1 parent f47590e commit 257c0ff

File tree

2 files changed

+60
-22
lines changed

2 files changed

+60
-22
lines changed
Lines changed: 56 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! `UUID` types.
22
33
use displaydoc::Display;
4-
use minicbor::{data::Tag, Decoder};
4+
use minicbor::Decoder;
55
use thiserror::Error;
66
use uuid::Uuid;
77

@@ -14,8 +14,8 @@ pub use uuid_v7::UuidV7 as V7;
1414
/// Invalid Doc Type UUID
1515
pub 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;
17+
// UUID CBOR tag <https://www.iana.org/assignments/cbor-tags/cbor-tags.xhtml/>.
18+
// pub(crate) const UUID_CBOR_TAG: u64 = 37;
1919

2020
/// Errors that can occur when decoding CBOR-encoded UUIDs.
2121
#[derive(Display, Debug, Error)]
@@ -34,29 +34,67 @@ pub enum CborUuidError {
3434
}
3535

3636
/// Decode from `CBOR` into `UUID`
37-
pub(crate) fn decode_cbor_uuid(
38-
d: &mut Decoder<'_>, (): &mut (),
37+
pub(crate) fn decode_cbor_uuid<C>(
38+
d: &mut Decoder<'_>, ctx: &mut C,
3939
) -> Result<uuid::Uuid, minicbor::decode::Error> {
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-
}
4740
let decoded = d
48-
.bytes()?
49-
.try_into()
41+
.decode_with(ctx)
5042
.map_err(|_| minicbor::decode::Error::message("Expected UUID to have 16 bytes"))?;
5143
let uuid = uuid::Uuid::from_bytes(decoded);
5244
Ok(uuid)
5345
}
5446

5547
/// Encode `UUID` into `CBOR`
56-
pub(crate) fn encode_cbor_uuid<W: minicbor::encode::Write>(
57-
uuid: uuid::Uuid, e: &mut minicbor::Encoder<W>, (): &mut (),
48+
pub(crate) fn encode_cbor_uuid<C, W: minicbor::encode::Write>(
49+
uuid: uuid::Uuid, e: &mut minicbor::Encoder<W>, ctx: &mut C,
5850
) -> Result<(), minicbor::encode::Error<W::Error>> {
59-
e.tag(Tag::new(UUID_CBOR_TAG))?;
60-
e.bytes(uuid.as_bytes())?;
51+
e.encode_with(uuid.as_bytes(), ctx)?;
6152
Ok(())
6253
}
54+
55+
#[cfg(test)]
56+
mod tests {
57+
use minicbor::data::Tagged;
58+
59+
use super::{V4, V7};
60+
61+
const UUID_CBOR_TAG: u64 = 37;
62+
63+
#[test]
64+
fn test_cbor_uuid_v4_roundtrip() {
65+
let uuid: V4 = uuid::Uuid::new_v4().into();
66+
let mut bytes = Vec::new();
67+
minicbor::encode(uuid, &mut bytes).unwrap();
68+
let decoded = minicbor::decode(bytes.as_slice()).unwrap();
69+
assert_eq!(uuid, decoded);
70+
}
71+
72+
#[test]
73+
fn test_cbor_uuid_v7_roundtrip() {
74+
let uuid: V7 = uuid::Uuid::now_v7().into();
75+
let mut bytes = Vec::new();
76+
minicbor::encode(uuid, &mut bytes).unwrap();
77+
let decoded = minicbor::decode(bytes.as_slice()).unwrap();
78+
assert_eq!(uuid, decoded);
79+
}
80+
81+
#[test]
82+
fn test_cbor_tagged_uuid_v4_roundtrip() {
83+
let uuid: V4 = uuid::Uuid::new_v4().into();
84+
let tagged: Tagged<UUID_CBOR_TAG, V4> = uuid.into();
85+
let mut bytes = Vec::new();
86+
minicbor::encode(tagged, &mut bytes).unwrap();
87+
let decoded = minicbor::decode(bytes.as_slice()).unwrap();
88+
assert_eq!(tagged, decoded);
89+
}
90+
91+
#[test]
92+
fn test_cbor_tagged_uuid_v7_roundtrip() {
93+
let uuid: V7 = uuid::Uuid::now_v7().into();
94+
let tagged: Tagged<UUID_CBOR_TAG, V7> = uuid.into();
95+
let mut bytes = Vec::new();
96+
minicbor::encode(tagged, &mut bytes).unwrap();
97+
let decoded = minicbor::decode(bytes.as_slice()).unwrap();
98+
assert_eq!(tagged, decoded);
99+
}
100+
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,16 @@ impl Display for UuidV7 {
4343
}
4444
}
4545

46-
impl Decode<'_, ()> for UuidV7 {
47-
fn decode(d: &mut Decoder<'_>, ctx: &mut ()) -> Result<Self, minicbor::decode::Error> {
46+
impl<C> Decode<'_, C> for UuidV7 {
47+
fn decode(d: &mut Decoder<'_>, ctx: &mut C) -> Result<Self, minicbor::decode::Error> {
4848
let uuid = decode_cbor_uuid(d, ctx)?;
4949
Ok(Self { uuid })
5050
}
5151
}
5252

53-
impl Encode<()> for UuidV7 {
53+
impl<C> Encode<C> for UuidV7 {
5454
fn encode<W: minicbor::encode::Write>(
55-
&self, e: &mut minicbor::Encoder<W>, ctx: &mut (),
55+
&self, e: &mut minicbor::Encoder<W>, ctx: &mut C,
5656
) -> Result<(), minicbor::encode::Error<W::Error>> {
5757
encode_cbor_uuid(self.uuid(), e, ctx)
5858
}

0 commit comments

Comments
 (0)