Skip to content

Commit b352830

Browse files
committed
fix(sign-doc): impl tryfrom instead of new and move err to own file
Signed-off-by: bkioshn <[email protected]>
1 parent c9bf4b0 commit b352830

File tree

3 files changed

+65
-51
lines changed

3 files changed

+65
-51
lines changed

rust/signed_doc/examples/mk_signed_doc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ fn decode_signed_doc(cose_bytes: &[u8]) {
108108
hex::encode(cose_bytes)
109109
);
110110

111-
match CatalystSignedDocument::new(cose_bytes) {
111+
match CatalystSignedDocument::try_from(cose_bytes) {
112112
Ok(cat_signed_doc) => {
113113
println!("This is a valid Catalyst Document.");
114114
println!("{cat_signed_doc}");

rust/signed_doc/src/error.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//! Catalyst Signed Document Error
2+
3+
use std::fmt;
4+
5+
use catalyst_types::problem_report::ProblemReport;
6+
7+
/// Catalyst Signed Document Error
8+
#[allow(clippy::module_name_repetitions)]
9+
#[derive(Debug)]
10+
pub struct CatalystSignedDocError {
11+
/// List of errors during processing.
12+
report: ProblemReport,
13+
/// Actual error.
14+
error: anyhow::Error,
15+
}
16+
17+
impl CatalystSignedDocError {
18+
/// Create a new `CatalystSignedDocError`.
19+
#[must_use]
20+
pub fn new(report: ProblemReport, error: anyhow::Error) -> Self {
21+
Self { report, error }
22+
}
23+
24+
/// Get the error report.
25+
#[must_use]
26+
pub fn report(&self) -> &ProblemReport {
27+
&self.report
28+
}
29+
30+
/// Get the actual error.
31+
#[must_use]
32+
pub fn error(&self) -> &anyhow::Error {
33+
&self.error
34+
}
35+
}
36+
37+
impl fmt::Display for CatalystSignedDocError {
38+
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
39+
let report_json = serde_json::to_string(&self.report)
40+
.unwrap_or_else(|_| String::from("Failed to serialize ProblemReport"));
41+
42+
write!(
43+
fmt,
44+
"CatalystSignedDocError {{ error: {}, report: {} }}",
45+
self.error, report_json
46+
)
47+
}
48+
}

rust/signed_doc/src/lib.rs

Lines changed: 16 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,22 @@
22
33
mod builder;
44
mod content;
5+
pub mod error;
56
mod metadata;
67
mod signature;
78
mod utils;
89

910
use std::{
1011
convert::TryFrom,
11-
fmt::{self, Display, Formatter},
12+
fmt::{Display, Formatter},
1213
sync::Arc,
1314
};
1415

1516
pub use builder::Builder;
1617
use catalyst_types::problem_report::ProblemReport;
1718
pub use content::Content;
1819
use coset::{CborSerializable, Header};
20+
use error::CatalystSignedDocError;
1921
pub use metadata::{DocumentRef, ExtraFields, Metadata, UuidV4, UuidV7};
2022
pub use minicbor::{decode, encode, Decode, Decoder, Encode};
2123
pub use signature::{KidUri, Signatures};
@@ -65,55 +67,7 @@ impl From<InnerCatalystSignedDocument> for CatalystSignedDocument {
6567
}
6668
}
6769

68-
/// Catalyst Signed Document Error
69-
#[derive(Debug)]
70-
pub struct CatalystSignedDocError {
71-
/// List of errors during processing.
72-
report: ProblemReport,
73-
/// Actual error.
74-
error: anyhow::Error,
75-
}
76-
77-
impl fmt::Display for CatalystSignedDocError {
78-
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
79-
let report_json = serde_json::to_string(&self.report)
80-
.unwrap_or_else(|_| String::from("Failed to serialize ProblemReport"));
81-
82-
write!(
83-
fmt,
84-
"CatalystSignedDocError {{ error: {}, report: {} }}",
85-
self.error, report_json
86-
)
87-
}
88-
}
89-
9070
impl CatalystSignedDocument {
91-
/// Create a new Catalyst Signed Document from a COSE Sign document bytes.
92-
///
93-
/// # Arguments
94-
///
95-
/// * `cose_bytes` - COSE Sign document bytes.
96-
///
97-
/// # Returns
98-
///
99-
/// A new Catalyst Signed Document.
100-
///
101-
/// # Errors
102-
///
103-
/// Returns an error if the COSE Sign document bytes are invalid or decode error.
104-
pub fn new(cose_bytes: &[u8]) -> anyhow::Result<Self, CatalystSignedDocError> {
105-
let error_report = ProblemReport::new("Catalyst Signed Document");
106-
let mut ctx = SignDocContext { error_report };
107-
let decoded: CatalystSignedDocument =
108-
minicbor::decode_with(cose_bytes, &mut ctx).map_err(|e| {
109-
CatalystSignedDocError {
110-
report: ctx.error_report,
111-
error: e.into(),
112-
}
113-
})?;
114-
Ok(decoded)
115-
}
116-
11771
// A bunch of getters to access the contents, or reason through the document, such as.
11872

11973
/// Return Document Type `UUIDv4`.
@@ -153,6 +107,18 @@ impl CatalystSignedDocument {
153107
}
154108
}
155109

110+
impl TryFrom<&[u8]> for CatalystSignedDocument {
111+
type Error = CatalystSignedDocError;
112+
113+
fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
114+
let error_report = ProblemReport::new("Catalyst Signed Document");
115+
let mut ctx = SignDocContext { error_report };
116+
let decoded: CatalystSignedDocument = minicbor::decode_with(value, &mut ctx)
117+
.map_err(|e| CatalystSignedDocError::new(ctx.error_report, e.into()))?;
118+
Ok(decoded)
119+
}
120+
}
121+
156122
impl Decode<'_, SignDocContext> for CatalystSignedDocument {
157123
fn decode(d: &mut Decoder<'_>, ctx: &mut SignDocContext) -> Result<Self, decode::Error> {
158124
let start = d.position();
@@ -319,7 +285,7 @@ mod tests {
319285
let mut bytes = Vec::new();
320286
minicbor::encode_with(doc, &mut bytes, &mut ()).unwrap();
321287

322-
let decoded = CatalystSignedDocument::new(&bytes).unwrap();
288+
let decoded: CatalystSignedDocument = bytes.as_slice().try_into().unwrap();
323289

324290
assert_eq!(decoded.doc_type(), uuid_v4);
325291
assert_eq!(decoded.doc_id(), uuid_v7);

0 commit comments

Comments
 (0)