Skip to content

Commit 1254bb4

Browse files
committed
update CatalystSignedDocument decoding
1 parent 7d1537a commit 1254bb4

File tree

1 file changed

+54
-48
lines changed

1 file changed

+54
-48
lines changed

rust/signed_doc/src/lib.rs

Lines changed: 54 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,59 @@ impl Display for CatalystSignedDocument {
4848
}
4949
}
5050

51-
impl TryFrom<&[u8]> for CatalystSignedDocument {
52-
type Error = error::Error;
51+
impl CatalystSignedDocument {
52+
// A bunch of getters to access the contents, or reason through the document, such as.
53+
54+
/// Return Document Type `UUIDv4`.
55+
#[must_use]
56+
pub fn doc_type(&self) -> uuid::Uuid {
57+
self.inner.metadata.doc_type()
58+
}
59+
60+
/// Return Document ID `UUIDv7`.
61+
#[must_use]
62+
pub fn doc_id(&self) -> uuid::Uuid {
63+
self.inner.metadata.doc_id()
64+
}
65+
66+
/// Return Document Version `UUIDv7`.
67+
#[must_use]
68+
pub fn doc_ver(&self) -> uuid::Uuid {
69+
self.inner.metadata.doc_ver()
70+
}
71+
72+
/// Return document `Content`.
73+
#[must_use]
74+
pub fn doc_content(&self) -> &Content {
75+
&self.inner.content
76+
}
77+
78+
/// Return document metadata content.
79+
#[must_use]
80+
pub fn doc_meta(&self) -> &AdditionalFields {
81+
self.inner.metadata.extra()
82+
}
83+
84+
/// Return a Document's signatures
85+
#[must_use]
86+
pub fn signatures(&self) -> &Signatures {
87+
&self.inner.signatures
88+
}
89+
}
90+
91+
impl minicbor::Decode<'_, ()> for CatalystSignedDocument {
92+
fn decode(d: &mut minicbor::Decoder<'_>, (): &mut ()) -> Result<Self, minicbor::decode::Error> {
93+
let start = d.position();
94+
d.skip()?;
95+
let end = d.position();
96+
let cose_bytes = d
97+
.input()
98+
.get(start..end)
99+
.ok_or(minicbor::decode::Error::end_of_input())?;
53100

54-
fn try_from(cose_bytes: &[u8]) -> Result<Self, Self::Error> {
55-
// Try reading as a tagged COSE SIGN, otherwise try reading as untagged.
56-
let cose_sign = coset::CoseSign::from_slice(cose_bytes)
57-
.map_err(|e| vec![anyhow::anyhow!("Invalid COSE Sign document: {e}")])?;
101+
let cose_sign = coset::CoseSign::from_slice(cose_bytes).map_err(|e| {
102+
minicbor::decode::Error::message(format!("Invalid COSE Sign document: {e}"))
103+
})?;
58104

59105
let mut errors = Vec::new();
60106

@@ -86,7 +132,7 @@ impl TryFrom<&[u8]> for CatalystSignedDocument {
86132
)
87133
.map_err(|e| {
88134
errors.push(anyhow!("Invalid Document Content: {e}"));
89-
errors
135+
minicbor::decode::Error::custom(error::Error(errors))
90136
})?;
91137

92138
Ok(CatalystSignedDocument {
@@ -98,47 +144,7 @@ impl TryFrom<&[u8]> for CatalystSignedDocument {
98144
.into(),
99145
})
100146
},
101-
_ => Err(error::Error(errors)),
147+
_ => Err(minicbor::decode::Error::custom(error::Error(errors))),
102148
}
103149
}
104150
}
105-
106-
impl CatalystSignedDocument {
107-
// A bunch of getters to access the contents, or reason through the document, such as.
108-
109-
/// Return Document Type `UUIDv4`.
110-
#[must_use]
111-
pub fn doc_type(&self) -> uuid::Uuid {
112-
self.inner.metadata.doc_type()
113-
}
114-
115-
/// Return Document ID `UUIDv7`.
116-
#[must_use]
117-
pub fn doc_id(&self) -> uuid::Uuid {
118-
self.inner.metadata.doc_id()
119-
}
120-
121-
/// Return Document Version `UUIDv7`.
122-
#[must_use]
123-
pub fn doc_ver(&self) -> uuid::Uuid {
124-
self.inner.metadata.doc_ver()
125-
}
126-
127-
/// Return document `Content`.
128-
#[must_use]
129-
pub fn doc_content(&self) -> &Content {
130-
&self.inner.content
131-
}
132-
133-
/// Return document metadata content.
134-
#[must_use]
135-
pub fn doc_meta(&self) -> &AdditionalFields {
136-
self.inner.metadata.extra()
137-
}
138-
139-
/// Return a Document's signatures
140-
#[must_use]
141-
pub fn signatures(&self) -> &Signatures {
142-
&self.inner.signatures
143-
}
144-
}

0 commit comments

Comments
 (0)