|
| 1 | +//! Proposal Document object implementation |
| 2 | +//! <https://input-output-hk.github.io/catalyst-libs/architecture/08_concepts/catalyst_docs/proposal/#proposal-document> |
| 3 | +
|
| 4 | +use catalyst_types::problem_report::ProblemReport; |
| 5 | + |
| 6 | +use crate::{error::CatalystSignedDocError, CatalystSignedDocument}; |
| 7 | + |
| 8 | +/// Proposal document `UuidV4` type. |
| 9 | +const PROPOSAL_DOCUMENT_UUID_TYPE: uuid::Uuid = |
| 10 | + uuid::Uuid::from_u128(0x7808_D2BA_D511_40AF_84E8_C0D1_625F_DFDC); |
| 11 | + |
| 12 | +/// Proposal Document struct |
| 13 | +pub struct ProposalDocument { |
| 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 ProposalDocument { |
| 21 | + /// Try to build `ProposalDocument` from `CatalystSignedDoc` collecting report |
| 22 | + #[allow(dead_code)] |
| 23 | + pub(crate) fn from_signed_doc( |
| 24 | + doc: &CatalystSignedDocument, error_report: &ProblemReport, |
| 25 | + ) -> anyhow::Result<Self> { |
| 26 | + /// Context for error messages. |
| 27 | + const CONTEXT: &str = "Catalyst Signed Document to Proposal Document"; |
| 28 | + let mut failed = false; |
| 29 | + |
| 30 | + if doc.doc_type().uuid() != PROPOSAL_DOCUMENT_UUID_TYPE { |
| 31 | + error_report.invalid_value( |
| 32 | + "`type`", |
| 33 | + &doc.doc_type().to_string(), |
| 34 | + &format!("Proposal Document type UUID value is {PROPOSAL_DOCUMENT_UUID_TYPE}"), |
| 35 | + CONTEXT, |
| 36 | + ); |
| 37 | + failed = true; |
| 38 | + } |
| 39 | + |
| 40 | + // TODO add other validation |
| 41 | + |
| 42 | + if failed { |
| 43 | + anyhow::bail!("Failed to build `ProposalDocument` from `CatalystSignedDoc`"); |
| 44 | + } |
| 45 | + |
| 46 | + let content = doc.doc_content().decoded_bytes().to_vec(); |
| 47 | + Ok(Self { content }) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +impl TryFrom<CatalystSignedDocument> for ProposalDocument { |
| 52 | + type Error = CatalystSignedDocError; |
| 53 | + |
| 54 | + fn try_from(doc: CatalystSignedDocument) -> Result<Self, Self::Error> { |
| 55 | + let error_report = ProblemReport::new("Proposal Document"); |
| 56 | + let res = Self::from_signed_doc(&doc, &error_report) |
| 57 | + .map_err(|e| CatalystSignedDocError::new(error_report, e))?; |
| 58 | + Ok(res) |
| 59 | + } |
| 60 | +} |
0 commit comments