Skip to content

Commit d942e1e

Browse files
committed
feat(rust/signed-doc): refactor metadata into a module
1 parent ea0bebd commit d942e1e

File tree

4 files changed

+131
-71
lines changed

4 files changed

+131
-71
lines changed

rust/signed_doc/examples/mk_signed_doc.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -199,35 +199,35 @@ fn build_empty_cose_doc(doc_bytes: Vec<u8>, meta: &Metadata) -> coset::CoseSign
199199

200200
protected_header.rest.push((
201201
coset::Label::Text("type".to_string()),
202-
encode_cbor_uuid(&meta.r#type),
202+
encode_cbor_uuid(&meta.doc_type()),
203203
));
204204
protected_header.rest.push((
205205
coset::Label::Text("id".to_string()),
206-
encode_cbor_uuid(&meta.id),
206+
encode_cbor_uuid(&meta.doc_id()),
207207
));
208208
protected_header.rest.push((
209209
coset::Label::Text("ver".to_string()),
210-
encode_cbor_uuid(&meta.ver),
210+
encode_cbor_uuid(&meta.doc_ver()),
211211
));
212-
if let Some(r#ref) = &meta.r#ref {
212+
if let Some(r#ref) = &meta.doc_ref() {
213213
protected_header.rest.push((
214214
coset::Label::Text("ref".to_string()),
215215
encode_cbor_document_ref(r#ref),
216216
));
217217
}
218-
if let Some(template) = &meta.template {
218+
if let Some(template) = &meta.doc_template() {
219219
protected_header.rest.push((
220220
coset::Label::Text("template".to_string()),
221221
encode_cbor_document_ref(template),
222222
));
223223
}
224-
if let Some(reply) = &meta.reply {
224+
if let Some(reply) = &meta.doc_reply() {
225225
protected_header.rest.push((
226226
coset::Label::Text("reply".to_string()),
227227
encode_cbor_document_ref(reply),
228228
));
229229
}
230-
if let Some(section) = &meta.section {
230+
if let Some(section) = &meta.doc_section() {
231231
protected_header.rest.push((
232232
coset::Label::Text("section".to_string()),
233233
coset::cbor::Value::Text(section.clone()),

rust/signed_doc/src/lib.rs

Lines changed: 10 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ use std::{
88

99
use coset::{CborSerializable, TaggedCborSerializable};
1010

11+
mod metadata;
12+
13+
pub use metadata::{DocumentRef, Metadata};
14+
1115
/// Catalyst Signed Document Content Encoding Key.
1216
const CONTENT_ENCODING_KEY: &str = "content encoding";
1317
/// Catalyst Signed Document Content Encoding Value.
@@ -59,67 +63,6 @@ struct InnerCatalystSignedDocument {
5963
content_errors: Vec<String>,
6064
}
6165

62-
/// Document Metadata.
63-
#[derive(Debug, serde::Deserialize)]
64-
pub struct Metadata {
65-
/// Document Type `UUIDv7`.
66-
pub r#type: uuid::Uuid,
67-
/// Document ID `UUIDv7`.
68-
pub id: uuid::Uuid,
69-
/// Document Version `UUIDv7`.
70-
pub ver: uuid::Uuid,
71-
/// Reference to the latest document.
72-
pub r#ref: Option<DocumentRef>,
73-
/// Reference to the document template.
74-
pub template: Option<DocumentRef>,
75-
/// Reference to the document reply.
76-
pub reply: Option<DocumentRef>,
77-
/// Reference to the document section.
78-
pub section: Option<String>,
79-
}
80-
81-
impl Display for Metadata {
82-
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
83-
writeln!(f, "Metadata {{")?;
84-
writeln!(f, " doc_type: {},", self.r#type)?;
85-
writeln!(f, " doc_id: {},", self.id)?;
86-
writeln!(f, " doc_ver: {},", self.ver)?;
87-
writeln!(f, " doc_ref: {:?},", self.r#ref)?;
88-
writeln!(f, " doc_template: {:?},", self.template)?;
89-
writeln!(f, " doc_reply: {:?},", self.reply)?;
90-
writeln!(f, " doc_section: {:?}", self.section)?;
91-
writeln!(f, "}}")
92-
}
93-
}
94-
95-
impl Default for Metadata {
96-
fn default() -> Self {
97-
Self {
98-
r#type: CatalystSignedDocument::INVALID_UUID,
99-
id: CatalystSignedDocument::INVALID_UUID,
100-
ver: CatalystSignedDocument::INVALID_UUID,
101-
r#ref: None,
102-
template: None,
103-
reply: None,
104-
section: None,
105-
}
106-
}
107-
}
108-
109-
/// Reference to a Document.
110-
#[derive(Copy, Clone, Debug, serde::Deserialize)]
111-
#[serde(untagged)]
112-
pub enum DocumentRef {
113-
/// Reference to the latest document
114-
Latest {
115-
/// Document ID UUID
116-
id: uuid::Uuid,
117-
},
118-
/// Reference to the specific document version
119-
/// Document ID UUID, Document Ver UUID
120-
WithVer(uuid::Uuid, uuid::Uuid),
121-
}
122-
12366
// Do this instead of `new` if we are converting a single parameter into a struct/type we
12467
// should use either `From` or `TryFrom` and reserve `new` for cases where we need
12568
// multiple parameters to actually create the type. This is much more elegant to use this
@@ -161,9 +104,6 @@ impl TryFrom<Vec<u8>> for CatalystSignedDocument {
161104
}
162105

163106
impl CatalystSignedDocument {
164-
/// Invalid Doc Type UUID
165-
const INVALID_UUID: uuid::Uuid = uuid::Uuid::from_bytes([0x00; 16]);
166-
167107
// A bunch of getters to access the contents, or reason through the document, such as.
168108

169109
/// Are there any validation errors (as opposed to structural errors.
@@ -207,6 +147,12 @@ impl CatalystSignedDocument {
207147
pub fn doc_reply(&self) -> Option<DocumentRef> {
208148
self.inner.metadata.doc_reply()
209149
}
150+
151+
/// Return Document Reply `Option<DocumentRef>`.
152+
#[must_use]
153+
pub fn doc_section(&self) -> Option<String> {
154+
self.inner.metadata.doc_section()
155+
}
210156
}
211157

212158
/// Generate the COSE protected header used by Catalyst Signed Document.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//! Catalyst Signed Document Metadata.
2+
3+
/// Reference to a Document.
4+
#[derive(Copy, Clone, Debug, serde::Deserialize)]
5+
#[serde(untagged)]
6+
pub enum DocumentRef {
7+
/// Reference to the latest document
8+
Latest {
9+
/// Document ID UUID
10+
id: uuid::Uuid,
11+
},
12+
/// Reference to the specific document version
13+
/// Document ID UUID, Document Ver UUID
14+
WithVer(uuid::Uuid, uuid::Uuid),
15+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
//! Catalyst Signed Document Metadata.
2+
use std::fmt::{Display, Formatter};
3+
4+
mod document_ref;
5+
6+
pub use document_ref::DocumentRef;
7+
8+
/// Document Metadata.
9+
#[derive(Debug, serde::Deserialize)]
10+
pub struct Metadata {
11+
/// Document Type `UUIDv7`.
12+
pub(crate) r#type: uuid::Uuid,
13+
/// Document ID `UUIDv7`.
14+
pub(crate) id: uuid::Uuid,
15+
/// Document Version `UUIDv7`.
16+
pub(crate) ver: uuid::Uuid,
17+
/// Reference to the latest document.
18+
pub(crate) r#ref: Option<DocumentRef>,
19+
/// Reference to the document template.
20+
pub(crate) template: Option<DocumentRef>,
21+
/// Reference to the document reply.
22+
pub(crate) reply: Option<DocumentRef>,
23+
/// Reference to the document section.
24+
pub(crate) section: Option<String>,
25+
}
26+
27+
impl Metadata {
28+
/// Invalid Doc Type UUID
29+
const INVALID_UUID: uuid::Uuid = uuid::Uuid::from_bytes([0x00; 16]);
30+
31+
/// Return Document Type `UUIDv4`.
32+
#[must_use]
33+
pub fn doc_type(&self) -> uuid::Uuid {
34+
self.r#type
35+
}
36+
37+
/// Return Document ID `UUIDv7`.
38+
#[must_use]
39+
pub fn doc_id(&self) -> uuid::Uuid {
40+
self.id
41+
}
42+
43+
/// Return Document Version `UUIDv7`.
44+
#[must_use]
45+
pub fn doc_ver(&self) -> uuid::Uuid {
46+
self.ver
47+
}
48+
49+
/// Return Last Document Reference `Option<DocumentRef>`.
50+
#[must_use]
51+
pub fn doc_ref(&self) -> Option<DocumentRef> {
52+
self.r#ref
53+
}
54+
55+
/// Return Document Template `Option<DocumentRef>`.
56+
#[must_use]
57+
pub fn doc_template(&self) -> Option<DocumentRef> {
58+
self.template
59+
}
60+
61+
/// Return Document Reply `Option<DocumentRef>`.
62+
#[must_use]
63+
pub fn doc_reply(&self) -> Option<DocumentRef> {
64+
self.reply
65+
}
66+
67+
/// Return Document Section `Option<String>`.
68+
#[must_use]
69+
pub fn doc_section(&self) -> Option<String> {
70+
self.section.clone()
71+
}
72+
}
73+
impl Display for Metadata {
74+
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
75+
writeln!(f, "Metadata {{")?;
76+
writeln!(f, " doc_type: {},", self.r#type)?;
77+
writeln!(f, " doc_id: {},", self.id)?;
78+
writeln!(f, " doc_ver: {},", self.ver)?;
79+
writeln!(f, " doc_ref: {:?},", self.r#ref)?;
80+
writeln!(f, " doc_template: {:?},", self.template)?;
81+
writeln!(f, " doc_reply: {:?},", self.reply)?;
82+
writeln!(f, " doc_section: {:?}", self.section)?;
83+
writeln!(f, "}}")
84+
}
85+
}
86+
87+
impl Default for Metadata {
88+
fn default() -> Self {
89+
Self {
90+
r#type: Self::INVALID_UUID,
91+
id: Self::INVALID_UUID,
92+
ver: Self::INVALID_UUID,
93+
r#ref: None,
94+
template: None,
95+
reply: None,
96+
section: None,
97+
}
98+
}
99+
}

0 commit comments

Comments
 (0)