|
| 1 | +//! Comment Document object implementation |
| 2 | +//! https://input-output-hk.github.io/catalyst-libs/architecture/08_concepts/catalyst_docs/comment/#comment-document |
| 3 | +
|
| 4 | +use catalyst_types::problem_report::ProblemReport; |
| 5 | + |
| 6 | +use crate::{error::CatalystSignedDocError, CatalystSignedDocument}; |
| 7 | + |
| 8 | +/// Comment document `UuidV4` type. |
| 9 | +pub const COMMENT_DOCUMENT_UUID_TYPE: uuid::Uuid = |
| 10 | + uuid::Uuid::from_u128(0xB679_DED3_0E7C_41BA_89F8_DA62_A178_98EA); |
| 11 | + |
| 12 | +/// Comment Document struct |
| 13 | +pub struct CommentDocument { |
| 14 | + /// Proposal document content data |
| 15 | + /// TODO: change it to `serde_json::Value` type |
| 16 | + #[allow(dead_code)] |
| 17 | + content: Vec<u8>, |
| 18 | +} |
| 19 | + |
| 20 | +impl CommentDocument { |
| 21 | + /// Try to build `CommentDocument` from `CatalystSignedDoc` doing all necessary |
| 22 | + /// stateless verifications, |
| 23 | + #[allow(dead_code)] |
| 24 | + pub(crate) fn from_signed_doc( |
| 25 | + doc: &CatalystSignedDocument, error_report: &ProblemReport, |
| 26 | + ) -> anyhow::Result<Self> { |
| 27 | + /// Context for error messages. |
| 28 | + const CONTEXT: &str = "Catalyst Signed Document to Proposal Document"; |
| 29 | + let mut failed = false; |
| 30 | + |
| 31 | + if doc.doc_type().uuid() != COMMENT_DOCUMENT_UUID_TYPE { |
| 32 | + error_report.invalid_value( |
| 33 | + "`type`", |
| 34 | + &doc.doc_type().to_string(), |
| 35 | + &format!("Proposal Document type UUID value is {COMMENT_DOCUMENT_UUID_TYPE}"), |
| 36 | + CONTEXT, |
| 37 | + ); |
| 38 | + failed = true; |
| 39 | + } |
| 40 | + |
| 41 | + // TODO add other validation |
| 42 | + |
| 43 | + if failed { |
| 44 | + anyhow::bail!("Failed to build `CommentDocument` from `CatalystSignedDoc`"); |
| 45 | + } |
| 46 | + |
| 47 | + let content = doc.doc_content().decoded_bytes().to_vec(); |
| 48 | + Ok(Self { content }) |
| 49 | + } |
| 50 | + |
| 51 | + /// A comprehensive validation of the `CommentDocument` content. |
| 52 | + #[allow(clippy::unused_self)] |
| 53 | + pub(crate) fn validate_with_report<F>(&self, _doc_getter: F, _error_report: &ProblemReport) |
| 54 | + where F: FnMut() -> Option<CatalystSignedDocument> { |
| 55 | + // TODO: implement the rest of the validation |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +impl TryFrom<CatalystSignedDocument> for CommentDocument { |
| 60 | + type Error = CatalystSignedDocError; |
| 61 | + |
| 62 | + fn try_from(doc: CatalystSignedDocument) -> Result<Self, Self::Error> { |
| 63 | + let error_report = ProblemReport::new("Proposal Document"); |
| 64 | + let res = Self::from_signed_doc(&doc, &error_report) |
| 65 | + .map_err(|e| CatalystSignedDocError::new(error_report, e))?; |
| 66 | + Ok(res) |
| 67 | + } |
| 68 | +} |
0 commit comments