Skip to content

Commit 7e2425d

Browse files
committed
fix(rust/signed_doc): update code and examples with Uuid types
1 parent 875aa31 commit 7e2425d

File tree

3 files changed

+26
-25
lines changed

3 files changed

+26
-25
lines changed

rust/signed_doc/examples/mk_signed_doc.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use ed25519_dalek::{
1414
ed25519::signature::Signer,
1515
pkcs8::{DecodePrivateKey, DecodePublicKey},
1616
};
17-
use signed_doc::{DocumentRef, Metadata};
17+
use signed_doc::{DocumentRef, Metadata, UuidV7};
1818

1919
fn main() {
2020
if let Err(err) = Cli::parse().exec() {
@@ -83,24 +83,27 @@ fn decode_cbor_uuid(val: &coset::cbor::Value) -> anyhow::Result<uuid::Uuid> {
8383

8484
fn encode_cbor_document_ref(doc_ref: &DocumentRef) -> coset::cbor::Value {
8585
match doc_ref {
86-
DocumentRef::Latest { id } => encode_cbor_uuid(id),
86+
DocumentRef::Latest { id } => encode_cbor_uuid(&id.uuid()),
8787
DocumentRef::WithVer(id, ver) => {
88-
coset::cbor::Value::Array(vec![encode_cbor_uuid(id), encode_cbor_uuid(ver)])
88+
coset::cbor::Value::Array(vec![
89+
encode_cbor_uuid(&id.uuid()),
90+
encode_cbor_uuid(&ver.uuid()),
91+
])
8992
},
9093
}
9194
}
9295

9396
#[allow(clippy::indexing_slicing)]
9497
fn decode_cbor_document_ref(val: &coset::cbor::Value) -> anyhow::Result<DocumentRef> {
95-
if let Ok(id) = decode_cbor_uuid(val) {
98+
if let Ok(id) = UuidV7::try_from(val) {
9699
Ok(DocumentRef::Latest { id })
97100
} else {
98101
let Some(array) = val.as_array() else {
99102
anyhow::bail!("Invalid CBOR encoded document `ref` type");
100103
};
101104
anyhow::ensure!(array.len() == 2, "Invalid CBOR encoded document `ref` type");
102-
let id = decode_cbor_uuid(&array[0])?;
103-
let ver = decode_cbor_uuid(&array[1])?;
105+
let id = UuidV7::try_from(&array[0])?;
106+
let ver = UuidV7::try_from(&array[1])?;
104107
Ok(DocumentRef::WithVer(id, ver))
105108
}
106109
}
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
//! Catalyst Signed Document Metadata.
2+
use super::UuidV7;
23

34
/// Reference to a Document.
45
#[derive(Copy, Clone, Debug, serde::Deserialize)]
@@ -7,9 +8,9 @@ pub enum DocumentRef {
78
/// Reference to the latest document
89
Latest {
910
/// Document ID UUID
10-
id: uuid::Uuid,
11+
id: UuidV7,
1112
},
1213
/// Reference to the specific document version
1314
/// Document ID UUID, Document Ver UUID
14-
WithVer(uuid::Uuid, uuid::Uuid),
15+
WithVer(UuidV7, UuidV7),
1516
}

rust/signed_doc/src/metadata/mod.rs

+14-17
Original file line numberDiff line numberDiff line change
@@ -10,42 +10,39 @@ pub use uuid_type::{UuidV4, UuidV7};
1010
/// Document Metadata.
1111
#[derive(Debug, serde::Deserialize)]
1212
pub struct Metadata {
13-
/// Document Type `UUIDv7`.
14-
pub(crate) r#type: uuid::Uuid,
13+
/// Document Type `UUIDv4`.
14+
r#type: UuidV4,
1515
/// Document ID `UUIDv7`.
16-
pub(crate) id: uuid::Uuid,
16+
id: UuidV7,
1717
/// Document Version `UUIDv7`.
18-
pub(crate) ver: uuid::Uuid,
18+
ver: UuidV7,
1919
/// Reference to the latest document.
20-
pub(crate) r#ref: Option<DocumentRef>,
20+
r#ref: Option<DocumentRef>,
2121
/// Reference to the document template.
22-
pub(crate) template: Option<DocumentRef>,
22+
template: Option<DocumentRef>,
2323
/// Reference to the document reply.
24-
pub(crate) reply: Option<DocumentRef>,
24+
reply: Option<DocumentRef>,
2525
/// Reference to the document section.
26-
pub(crate) section: Option<String>,
26+
section: Option<String>,
2727
}
2828

2929
impl Metadata {
30-
/// Invalid Doc Type UUID
31-
const INVALID_UUID: uuid::Uuid = uuid::Uuid::from_bytes([0x00; 16]);
32-
3330
/// Return Document Type `UUIDv4`.
3431
#[must_use]
3532
pub fn doc_type(&self) -> uuid::Uuid {
36-
self.r#type
33+
self.r#type.uuid()
3734
}
3835

3936
/// Return Document ID `UUIDv7`.
4037
#[must_use]
4138
pub fn doc_id(&self) -> uuid::Uuid {
42-
self.id
39+
self.id.uuid()
4340
}
4441

4542
/// Return Document Version `UUIDv7`.
4643
#[must_use]
4744
pub fn doc_ver(&self) -> uuid::Uuid {
48-
self.ver
45+
self.ver.uuid()
4946
}
5047

5148
/// Return Last Document Reference `Option<DocumentRef>`.
@@ -89,9 +86,9 @@ impl Display for Metadata {
8986
impl Default for Metadata {
9087
fn default() -> Self {
9188
Self {
92-
r#type: Self::INVALID_UUID,
93-
id: Self::INVALID_UUID,
94-
ver: Self::INVALID_UUID,
89+
r#type: UuidV4::invalid(),
90+
id: UuidV7::invalid(),
91+
ver: UuidV7::invalid(),
9592
r#ref: None,
9693
template: None,
9794
reply: None,

0 commit comments

Comments
 (0)