Skip to content

Commit 60933ea

Browse files
committed
fix(rust/catalyst-types): use minicbor and remove coset
1 parent a3d34a4 commit 60933ea

File tree

4 files changed

+39
-133
lines changed

4 files changed

+39
-133
lines changed

rust/catalyst-types/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ name = "catalyst_types"
1717

1818
[dependencies]
1919
blake2b_simd = "1.0.2"
20-
coset = "0.3.8"
2120
displaydoc = "0.2.5"
2221
ed25519-dalek = "2.1.1"
2322
fluent-uri = "0.3.2"
@@ -33,4 +32,4 @@ uuid = { version = "1.11.0", features = ["v4", "v7", "serde"] }
3332

3433
[dev-dependencies]
3534
ed25519-dalek = { version = "2.1.1", features = ["rand_core"] }
36-
rand = "0.8.5"
35+
rand = "0.8.5"
Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! `UUID` types.
22
33
use displaydoc::Display;
4+
use minicbor::Decoder;
45
use thiserror::Error;
56
use uuid::Uuid;
67

@@ -13,9 +14,6 @@ pub use uuid_v7::UuidV7 as V7;
1314
/// Invalid Doc Type UUID
1415
pub const INVALID_UUID: uuid::Uuid = uuid::Uuid::from_bytes([0x00; 16]);
1516

16-
/// CBOR tag for UUID content.
17-
pub const UUID_CBOR_TAG: u64 = 37;
18-
1917
/// Errors that can occur when decoding CBOR-encoded UUIDs.
2018
#[derive(Display, Debug, Error)]
2119
pub enum CborUuidError {
@@ -32,24 +30,21 @@ pub enum CborUuidError {
3230
},
3331
}
3432

35-
/// Encode `UUID` into `CBOR`.
36-
pub(crate) fn encode_cbor_uuid(uuid: uuid::Uuid) -> coset::cbor::Value {
37-
coset::cbor::Value::Tag(
38-
UUID_CBOR_TAG,
39-
coset::cbor::Value::Bytes(uuid.as_bytes().to_vec()).into(),
40-
)
33+
/// Decode from `CBOR` into `UUID`
34+
pub(crate) fn decode_cbor_uuid(
35+
d: &mut Decoder<'_>, (): &mut (),
36+
) -> 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 uuid = uuid::Uuid::from_bytes(decoded);
41+
Ok(uuid)
4142
}
4243

43-
/// Decode `CBOR` encoded `UUID`.
44-
pub(crate) fn decode_cbor_uuid(val: &coset::cbor::Value) -> Result<uuid::Uuid, CborUuidError> {
45-
let Some((UUID_CBOR_TAG, coset::cbor::Value::Bytes(bytes))) = val.as_tag() else {
46-
return Err(CborUuidError::InvalidCborType);
47-
};
48-
let uuid = uuid::Uuid::from_bytes(
49-
bytes
50-
.clone()
51-
.try_into()
52-
.map_err(|_| CborUuidError::InvalidByteSize)?,
53-
);
54-
Ok(uuid)
44+
/// Encode `UUID` into `CBOR`
45+
pub(crate) fn encode_cbor_uuid<W: minicbor::encode::Write>(
46+
uuid: uuid::Uuid, e: &mut minicbor::Encoder<W>, (): &mut (),
47+
) -> Result<(), minicbor::encode::Error<W::Error>> {
48+
e.bytes(uuid.as_bytes())?;
49+
Ok(())
5550
}

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

Lines changed: 11 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
//! `UUIDv4` Type.
22
use std::fmt::{Display, Formatter};
33

4+
use minicbor::{Decode, Decoder, Encode};
5+
46
use super::{decode_cbor_uuid, encode_cbor_uuid, INVALID_UUID};
57

68
/// Type representing a `UUIDv4`.
@@ -41,29 +43,18 @@ impl Display for UuidV4 {
4143
}
4244
}
4345

44-
impl From<UuidV4> for coset::cbor::Value {
45-
fn from(val: UuidV4) -> Self {
46-
encode_cbor_uuid(val.into())
46+
impl Decode<'_, ()> for UuidV4 {
47+
fn decode(d: &mut Decoder<'_>, ctx: &mut ()) -> Result<Self, minicbor::decode::Error> {
48+
let uuid = decode_cbor_uuid(d, ctx)?;
49+
Ok(Self { uuid })
4750
}
4851
}
4952

50-
impl TryFrom<&coset::cbor::Value> for UuidV4 {
51-
type Error = super::CborUuidError;
52-
53-
fn try_from(cbor_value: &coset::cbor::Value) -> Result<Self, Self::Error> {
54-
match decode_cbor_uuid(cbor_value) {
55-
Ok(uuid) => {
56-
if uuid.get_version_num() == Self::UUID_VERSION_NUMBER {
57-
Ok(Self { uuid })
58-
} else {
59-
Err(super::CborUuidError::InvalidVersion {
60-
uuid,
61-
expected_version: Self::UUID_VERSION_NUMBER,
62-
})
63-
}
64-
},
65-
Err(e) => Err(e),
66-
}
53+
impl Encode<()> for UuidV4 {
54+
fn encode<W: minicbor::encode::Write>(
55+
&self, e: &mut minicbor::Encoder<W>, ctx: &mut (),
56+
) -> Result<(), minicbor::encode::Error<W::Error>> {
57+
encode_cbor_uuid(self.uuid(), e, ctx)
6758
}
6859
}
6960

@@ -87,11 +78,9 @@ impl From<UuidV4> for uuid::Uuid {
8778

8879
#[cfg(test)]
8980
mod tests {
90-
use coset::cbor::Value;
9181
use uuid::Uuid;
9282

9383
use super::*;
94-
use crate::uuid::UUID_CBOR_TAG;
9584

9685
#[test]
9786
fn test_invalid_uuid() {
@@ -118,37 +107,4 @@ mod tests {
118107
"Zero UUID should not be valid"
119108
);
120109
}
121-
122-
#[test]
123-
fn test_try_from_cbor_valid_uuid() {
124-
let uuid = Uuid::new_v4();
125-
let cbor_value = Value::Tag(
126-
UUID_CBOR_TAG,
127-
Box::new(Value::Bytes(uuid.as_bytes().to_vec())),
128-
);
129-
let result = UuidV4::try_from(&cbor_value);
130-
131-
assert!(
132-
result.is_ok(),
133-
"Should successfully parse valid UUID from CBOR"
134-
);
135-
let uuid_v4 = result.unwrap();
136-
assert!(uuid_v4.is_valid(), "Parsed UUIDv4 should be valid");
137-
assert_eq!(
138-
uuid_v4.uuid(),
139-
uuid,
140-
"Parsed UUID should match original UUID"
141-
);
142-
}
143-
144-
#[test]
145-
fn test_try_from_cbor_invalid_uuid() {
146-
let cbor_value = Value::Bytes(vec![0; 16]);
147-
let result = UuidV4::try_from(&cbor_value);
148-
149-
assert!(
150-
result.is_err(),
151-
"Should fail to parse invalid UUID from CBOR"
152-
);
153-
}
154110
}

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

Lines changed: 11 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
//! `UUIDv7` Type.
22
use std::fmt::{Display, Formatter};
33

4+
use minicbor::{Decode, Decoder, Encode};
5+
46
use super::{decode_cbor_uuid, encode_cbor_uuid, INVALID_UUID};
57

68
/// Type representing a `UUIDv7`.
@@ -41,29 +43,18 @@ impl Display for UuidV7 {
4143
}
4244
}
4345

44-
impl From<UuidV7> for coset::cbor::Value {
45-
fn from(val: UuidV7) -> Self {
46-
encode_cbor_uuid(val.into())
46+
impl Decode<'_, ()> for UuidV7 {
47+
fn decode(d: &mut Decoder<'_>, ctx: &mut ()) -> Result<Self, minicbor::decode::Error> {
48+
let uuid = decode_cbor_uuid(d, ctx)?;
49+
Ok(Self { uuid })
4750
}
4851
}
4952

50-
impl TryFrom<&coset::cbor::Value> for UuidV7 {
51-
type Error = super::CborUuidError;
52-
53-
fn try_from(cbor_value: &coset::cbor::Value) -> Result<Self, Self::Error> {
54-
match decode_cbor_uuid(cbor_value) {
55-
Ok(uuid) => {
56-
if uuid.get_version_num() == Self::UUID_VERSION_NUMBER {
57-
Ok(Self { uuid })
58-
} else {
59-
Err(super::CborUuidError::InvalidVersion {
60-
uuid,
61-
expected_version: Self::UUID_VERSION_NUMBER,
62-
})
63-
}
64-
},
65-
Err(e) => Err(e),
66-
}
53+
impl Encode<()> for UuidV7 {
54+
fn encode<W: minicbor::encode::Write>(
55+
&self, e: &mut minicbor::Encoder<W>, ctx: &mut (),
56+
) -> Result<(), minicbor::encode::Error<W::Error>> {
57+
encode_cbor_uuid(self.uuid(), e, ctx)
6758
}
6859
}
6960

@@ -87,11 +78,9 @@ impl From<UuidV7> for uuid::Uuid {
8778

8879
#[cfg(test)]
8980
mod tests {
90-
use coset::cbor::Value;
9181
use uuid::Uuid;
9282

9383
use super::*;
94-
use crate::uuid::UUID_CBOR_TAG;
9584

9685
#[test]
9786
fn test_invalid_uuid() {
@@ -119,37 +108,4 @@ mod tests {
119108
"Zero UUID should not be valid"
120109
);
121110
}
122-
123-
#[test]
124-
fn test_try_from_cbor_valid_uuid() {
125-
let uuid = Uuid::try_parse("017f22e3-79b0-7cc7-98cf-e0bbf8a1c5f1").unwrap();
126-
let cbor_value = Value::Tag(
127-
UUID_CBOR_TAG,
128-
Box::new(Value::Bytes(uuid.as_bytes().to_vec())),
129-
);
130-
let result = UuidV7::try_from(&cbor_value);
131-
132-
assert!(
133-
result.is_ok(),
134-
"Should successfully parse valid UUID from CBOR"
135-
);
136-
let uuid_v7 = result.unwrap();
137-
assert!(uuid_v7.is_valid(), "Parsed UUIDv7 should be valid");
138-
assert_eq!(
139-
uuid_v7.uuid(),
140-
uuid,
141-
"Parsed UUID should match original UUID"
142-
);
143-
}
144-
145-
#[test]
146-
fn test_try_from_cbor_invalid_uuid() {
147-
let cbor_value = Value::Bytes(vec![0; 16]);
148-
let result = UuidV7::try_from(&cbor_value);
149-
150-
assert!(
151-
result.is_err(),
152-
"Should fail to parse invalid UUID from CBOR"
153-
);
154-
}
155111
}

0 commit comments

Comments
 (0)