|
2 | 2 |
|
3 | 3 | mod builder; |
4 | 4 | mod content; |
| 5 | +pub mod error; |
5 | 6 | mod metadata; |
6 | 7 | mod signature; |
7 | 8 | mod utils; |
8 | 9 |
|
9 | 10 | use std::{ |
10 | 11 | convert::TryFrom, |
11 | | - fmt::{self, Display, Formatter}, |
| 12 | + fmt::{Display, Formatter}, |
12 | 13 | sync::Arc, |
13 | 14 | }; |
14 | 15 |
|
15 | 16 | pub use builder::Builder; |
16 | 17 | use catalyst_types::problem_report::ProblemReport; |
17 | 18 | pub use content::Content; |
18 | 19 | use coset::{CborSerializable, Header}; |
| 20 | +use error::CatalystSignedDocError; |
19 | 21 | pub use metadata::{DocumentRef, ExtraFields, Metadata, UuidV4, UuidV7}; |
20 | 22 | pub use minicbor::{decode, encode, Decode, Decoder, Encode}; |
21 | 23 | pub use signature::{KidUri, Signatures}; |
@@ -65,55 +67,7 @@ impl From<InnerCatalystSignedDocument> for CatalystSignedDocument { |
65 | 67 | } |
66 | 68 | } |
67 | 69 |
|
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 | | - |
90 | 70 | 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 | | - |
117 | 71 | // A bunch of getters to access the contents, or reason through the document, such as. |
118 | 72 |
|
119 | 73 | /// Return Document Type `UUIDv4`. |
@@ -153,6 +107,18 @@ impl CatalystSignedDocument { |
153 | 107 | } |
154 | 108 | } |
155 | 109 |
|
| 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 | + |
156 | 122 | impl Decode<'_, SignDocContext> for CatalystSignedDocument { |
157 | 123 | fn decode(d: &mut Decoder<'_>, ctx: &mut SignDocContext) -> Result<Self, decode::Error> { |
158 | 124 | let start = d.position(); |
@@ -319,7 +285,7 @@ mod tests { |
319 | 285 | let mut bytes = Vec::new(); |
320 | 286 | minicbor::encode_with(doc, &mut bytes, &mut ()).unwrap(); |
321 | 287 |
|
322 | | - let decoded = CatalystSignedDocument::new(&bytes).unwrap(); |
| 288 | + let decoded: CatalystSignedDocument = bytes.as_slice().try_into().unwrap(); |
323 | 289 |
|
324 | 290 | assert_eq!(decoded.doc_type(), uuid_v4); |
325 | 291 | assert_eq!(decoded.doc_id(), uuid_v7); |
|
0 commit comments