Skip to content

Commit 830e4ef

Browse files
committed
add Proposal Document type
1 parent 53deef7 commit 830e4ef

File tree

3 files changed

+72
-3
lines changed

3 files changed

+72
-3
lines changed

rust/signed_doc/src/doc_types/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
//! An implmenetation of different defined document types
22
//! <https://input-output-hk.github.io/catalyst-libs/architecture/08_concepts/signed_doc/types/>
33
4+
mod proposal_document;
5+
46
use catalyst_types::uuid::UuidV4;
7+
pub use proposal_document::ProposalDocument;
58

69
/// Represents different types of documents.
710
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
}

rust/signed_doc/src/validator/mod.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
33
use catalyst_types::problem_report::ProblemReport;
44

5-
use crate::{doc_types::DocumentType, error::CatalystSignedDocError, CatalystSignedDocument};
5+
use crate::{
6+
doc_types::{DocumentType, ProposalDocument},
7+
error::CatalystSignedDocError,
8+
CatalystSignedDocument,
9+
};
610

711
/// `CatalystSignedDocument` type based specific validation
812
///
@@ -16,7 +20,7 @@ pub fn validate(doc: &CatalystSignedDocument) -> Result<(), CatalystSignedDocErr
1620
Ok(doc_type) => doc_type,
1721
Err(e) => {
1822
error_report.invalid_value(
19-
"document `type`",
23+
"`type`",
2024
&doc.doc_type().to_string(),
2125
&e.to_string(),
2226
"verifying document type",
@@ -30,7 +34,9 @@ pub fn validate(doc: &CatalystSignedDocument) -> Result<(), CatalystSignedDocErr
3034

3135
#[allow(clippy::match_same_arms)]
3236
match doc_type {
33-
DocumentType::ProposalDocument => {},
37+
DocumentType::ProposalDocument => {
38+
drop(ProposalDocument::from_signed_doc(doc, &error_report));
39+
},
3440
DocumentType::ProposalTemplate => {},
3541
DocumentType::CommentDocument => {},
3642
DocumentType::CommentTemplate => {},

0 commit comments

Comments
 (0)