|
1 | 1 | //! Catalyst Signed Document Content Payload
|
2 | 2 |
|
| 3 | +use catalyst_types::problem_report::ProblemReport; |
| 4 | + |
3 | 5 | use crate::metadata::{ContentEncoding, ContentType};
|
4 | 6 |
|
5 | 7 | /// Decompressed Document Content type bytes.
|
6 |
| -#[derive(Debug, Clone, PartialEq)] |
| 8 | +#[derive(Debug, Clone, PartialEq, Default)] |
7 | 9 | pub struct Content {
|
8 |
| - /// Content data bytes |
9 |
| - data: Vec<u8>, |
10 |
| - /// Content type |
11 |
| - content_type: ContentType, |
12 |
| - /// Content encoding |
13 |
| - content_encoding: Option<ContentEncoding>, |
| 10 | + /// Original Decompressed Document's data bytes |
| 11 | + data: Option<Vec<u8>>, |
14 | 12 | }
|
15 | 13 |
|
16 | 14 | impl Content {
|
17 | 15 | /// Creates a new `Content` value, from the encoded data.
|
18 | 16 | /// verifies a Document's content, that it is correctly encoded and it corresponds and
|
19 | 17 | /// parsed to the specified type
|
20 |
| - /// |
21 |
| - /// # Errors |
22 |
| - /// Returns an error if content is not correctly encoded |
23 | 18 | pub(crate) fn from_encoded(
|
24 |
| - mut data: Vec<u8>, content_type: ContentType, content_encoding: Option<ContentEncoding>, |
25 |
| - ) -> anyhow::Result<Self> { |
26 |
| - if let Some(encoding) = content_encoding { |
27 |
| - data = encoding |
28 |
| - .decode(&data) |
29 |
| - .map_err(|e| anyhow::anyhow!("Failed to decode {encoding} content: {e}"))?; |
| 19 | + mut data: Vec<u8>, content_type: Option<ContentType>, |
| 20 | + content_encoding: Option<ContentEncoding>, report: &ProblemReport, |
| 21 | + ) -> Self { |
| 22 | + if let Some(content_encoding) = content_encoding { |
| 23 | + if let Ok(decoded_data) = content_encoding.decode(&data) { |
| 24 | + data = decoded_data; |
| 25 | + } else { |
| 26 | + report.invalid_value( |
| 27 | + "payload", |
| 28 | + &hex::encode(&data), |
| 29 | + &format!("Invalid Document content, should {content_encoding} encodable"), |
| 30 | + "Invalid Document content type.", |
| 31 | + ); |
| 32 | + return Self::default(); |
| 33 | + } |
| 34 | + } |
| 35 | + if let Some(content_type) = content_type { |
| 36 | + if content_type.validate(&data).is_err() { |
| 37 | + report.invalid_value( |
| 38 | + "payload", |
| 39 | + &hex::encode(&data), |
| 40 | + &format!("Invalid Document content type, should {content_type} encodable"), |
| 41 | + "Invalid Document content type.", |
| 42 | + ); |
| 43 | + return Self::default(); |
| 44 | + } |
30 | 45 | }
|
31 |
| - content_type.validate(&data)?; |
32 | 46 |
|
33 |
| - Ok(Self { |
34 |
| - data, |
35 |
| - content_type, |
36 |
| - content_encoding, |
37 |
| - }) |
| 47 | + Self { data: Some(data) } |
38 | 48 | }
|
39 | 49 |
|
40 | 50 | /// Creates a new `Content` value, from the decoded (original) data.
|
41 | 51 | /// verifies that it corresponds and parsed to the specified type.
|
42 | 52 | ///
|
43 | 53 | /// # Errors
|
44 | 54 | /// Returns an error if content is not correctly encoded
|
45 |
| - pub(crate) fn from_decoded( |
46 |
| - data: Vec<u8>, content_type: ContentType, content_encoding: Option<ContentEncoding>, |
47 |
| - ) -> anyhow::Result<Self> { |
| 55 | + pub(crate) fn from_decoded(data: Vec<u8>, content_type: ContentType) -> anyhow::Result<Self> { |
48 | 56 | content_type.validate(&data)?;
|
49 |
| - Ok(Self { |
50 |
| - data, |
51 |
| - content_type, |
52 |
| - content_encoding, |
53 |
| - }) |
54 |
| - } |
55 |
| - |
56 |
| - /// Return `true` if Document's content type is Json |
57 |
| - #[must_use] |
58 |
| - pub fn is_json(&self) -> bool { |
59 |
| - matches!(self.content_type, ContentType::Json) |
| 57 | + Ok(Self { data: Some(data) }) |
60 | 58 | }
|
61 | 59 |
|
62 |
| - /// Return `true` if Document's content type is Json |
63 |
| - #[must_use] |
64 |
| - pub fn is_cbor(&self) -> bool { |
65 |
| - matches!(self.content_type, ContentType::Cbor) |
66 |
| - } |
67 |
| - |
68 |
| - /// Return an decoded (original) content bytes, |
69 |
| - /// by the corresponding `content_encoding` provided field. |
70 |
| - #[must_use] |
71 |
| - pub fn decoded_bytes(&self) -> &[u8] { |
72 |
| - &self.data |
| 60 | + /// Return an decoded (original) content bytes. |
| 61 | + /// |
| 62 | + /// # Errors |
| 63 | + /// - Missing Document content |
| 64 | + pub fn decoded_bytes(&self) -> anyhow::Result<&[u8]> { |
| 65 | + self.data |
| 66 | + .as_deref() |
| 67 | + .ok_or(anyhow::anyhow!("Missing Document content")) |
73 | 68 | }
|
74 | 69 |
|
75 | 70 | /// Return an encoded content bytes,
|
76 |
| - /// by the corresponding `content_encoding` provided field |
77 |
| - pub(crate) fn encoded_bytes(&self) -> anyhow::Result<Vec<u8>> { |
78 |
| - if let Some(encoding) = self.content_encoding { |
79 |
| - let data = encoding |
80 |
| - .encode(&self.data) |
81 |
| - .map_err(|e| anyhow::anyhow!("Failed to encode {encoding} content: {e}"))?; |
82 |
| - Ok(data) |
83 |
| - } else { |
84 |
| - Ok(self.data.clone()) |
85 |
| - } |
| 71 | + /// by the provided `content_encoding` provided field. |
| 72 | + /// |
| 73 | + /// # Errors |
| 74 | + /// - Missing Document content |
| 75 | + /// - Failed to encode content. |
| 76 | + pub(crate) fn encoded_bytes( |
| 77 | + &self, content_encoding: ContentEncoding, |
| 78 | + ) -> anyhow::Result<Vec<u8>> { |
| 79 | + let content = self.decoded_bytes()?; |
| 80 | + let data = content_encoding |
| 81 | + .encode(content) |
| 82 | + .map_err(|e| anyhow::anyhow!("Failed to encode {content_encoding} content: {e}"))?; |
| 83 | + Ok(data) |
86 | 84 | }
|
87 | 85 |
|
88 |
| - /// Return content byte size |
| 86 | + /// Return content byte size. |
| 87 | + /// If content is empty returns `0`. |
89 | 88 | #[must_use]
|
90 | 89 | pub fn size(&self) -> usize {
|
91 |
| - self.data.len() |
| 90 | + self.data.as_ref().map(Vec::len).unwrap_or_default() |
92 | 91 | }
|
93 | 92 | }
|
0 commit comments