Skip to content

Commit a3d34a4

Browse files
committed
fix(rust/catalyst-types): convert UUID types to cbor::Value
1 parent e182213 commit a3d34a4

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ pub enum CborUuidError {
3232
},
3333
}
3434

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+
)
41+
}
42+
3543
/// Decode `CBOR` encoded `UUID`.
3644
pub(crate) fn decode_cbor_uuid(val: &coset::cbor::Value) -> Result<uuid::Uuid, CborUuidError> {
3745
let Some((UUID_CBOR_TAG, coset::cbor::Value::Bytes(bytes))) = val.as_tag() else {

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

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

4-
use super::{decode_cbor_uuid, INVALID_UUID};
4+
use super::{decode_cbor_uuid, encode_cbor_uuid, INVALID_UUID};
55

66
/// Type representing a `UUIDv4`.
77
#[derive(Copy, Clone, Debug, PartialEq, PartialOrd, serde::Serialize, serde::Deserialize)]
@@ -41,6 +41,12 @@ impl Display for UuidV4 {
4141
}
4242
}
4343

44+
impl From<UuidV4> for coset::cbor::Value {
45+
fn from(val: UuidV4) -> Self {
46+
encode_cbor_uuid(val.into())
47+
}
48+
}
49+
4450
impl TryFrom<&coset::cbor::Value> for UuidV4 {
4551
type Error = super::CborUuidError;
4652

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

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

4-
use super::{decode_cbor_uuid, INVALID_UUID};
4+
use super::{decode_cbor_uuid, encode_cbor_uuid, INVALID_UUID};
55

66
/// Type representing a `UUIDv7`.
77
#[derive(Copy, Clone, Debug, PartialEq, PartialOrd, serde::Serialize, serde::Deserialize)]
@@ -41,6 +41,12 @@ impl Display for UuidV7 {
4141
}
4242
}
4343

44+
impl From<UuidV7> for coset::cbor::Value {
45+
fn from(val: UuidV7) -> Self {
46+
encode_cbor_uuid(val.into())
47+
}
48+
}
49+
4450
impl TryFrom<&coset::cbor::Value> for UuidV7 {
4551
type Error = super::CborUuidError;
4652

0 commit comments

Comments
 (0)