Skip to content

Commit 7014694

Browse files
committed
wip(rust/signed_doc): add comment document type
1 parent d4c9f6a commit 7014694

File tree

3 files changed

+76
-5
lines changed

3 files changed

+76
-5
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
}

rust/signed_doc/src/doc_types/mod.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
//! An implementation of different defined document types
22
//! <https://input-output-hk.github.io/catalyst-libs/architecture/08_concepts/signed_doc/types/>
33
4+
mod comment_document;
45
mod proposal_document;
56

67
use catalyst_types::uuid::UuidV4;
8+
pub use comment_document::{CommentDocument, COMMENT_DOCUMENT_UUID_TYPE};
79
pub use proposal_document::{ProposalDocument, PROPOSAL_DOCUMENT_UUID_TYPE};
810

911
/// Represents different types of documents.
@@ -46,9 +48,6 @@ pub enum DocumentType {
4648
/// Proposal template `UuidV4` type.
4749
const PROPOSAL_TEMPLATE_UUID_TYPE: uuid::Uuid =
4850
uuid::Uuid::from_u128(0x0CE8_AB38_9258_4FBC_A62E_7FAA_6E58_318F);
49-
/// Comment document `UuidV4` type.
50-
const COMMENT_DOCUMENT_UUID_TYPE: uuid::Uuid =
51-
uuid::Uuid::from_u128(0xB679_DED3_0E7C_41BA_89F8_DA62_A178_98EA);
5251
/// Comment template `UuidV4` type.
5352
const COMMENT_TEMPLATE_UUID_TYPE: uuid::Uuid =
5453
uuid::Uuid::from_u128(0x0B84_24D4_EBFD_46E3_9577_1775_A69D_290C);

rust/signed_doc/src/validator/mod.rs

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

55
use crate::{
6-
doc_types::{DocumentType, ProposalDocument},
6+
doc_types::{CommentDocument, DocumentType, ProposalDocument},
77
error::CatalystSignedDocError,
88
CatalystSignedDocument,
99
};
@@ -44,7 +44,11 @@ where F: FnMut() -> Option<CatalystSignedDocument> {
4444
}
4545
},
4646
DocumentType::ProposalTemplate => {},
47-
DocumentType::CommentDocument => {},
47+
DocumentType::CommentDocument => {
48+
if let Ok(comment_doc) = CommentDocument::from_signed_doc(doc, &error_report) {
49+
comment_doc.validate_with_report(doc_getter, &error_report);
50+
}
51+
},
4852
DocumentType::CommentTemplate => {},
4953
DocumentType::ReviewDocument => {},
5054
DocumentType::ReviewTemplate => {},

0 commit comments

Comments
 (0)