dubp_documents_parser/
raw_text.rs1use crate::*;
17
18pub(super) mod certification;
19pub(super) mod document;
20pub(super) mod identity;
21pub(super) mod membership;
22pub(super) mod revocation;
23pub(super) mod transaction;
24pub(super) mod wallet_script;
25
26#[derive(Debug, Clone, Copy, Parser)]
27#[grammar = "documents_grammar.pest"]
28pub(crate) struct RawDocumentsParser;
30
31pub trait FromPestPair: Sized {
32 fn from_pest_pair(pair: Pair<Rule>) -> Result<Self, TextParseError>;
34}
35
36pub trait ParseFromRawText: FromPestPair {
37 fn parse_from_raw_text(doc: &str) -> Result<Self, TextParseError>;
39}
40
41macro_rules! impl_parse_from_raw_text {
42 ($Type:ty, $Rule:expr) => {
43 impl ParseFromRawText for $Type {
44 #[inline]
45 fn parse_from_raw_text(doc: &str) -> Result<Self, TextParseError> {
46 let mut doc_pairs = RawDocumentsParser::parse($Rule, doc)
47 .map_err(|e| TextParseError::PestError(e.into()))?;
48 Self::from_pest_pair(doc_pairs.next().unwrap_or_else(|| unreachable!()))
49 }
51 }
52 };
53}
54
55impl_parse_from_raw_text!(DubpDocument, Rule::document);
56impl_parse_from_raw_text!(IdentityDocument, Rule::idty);
57impl_parse_from_raw_text!(CertificationDocument, Rule::cert);
58impl_parse_from_raw_text!(MembershipDocument, Rule::membership);
59impl_parse_from_raw_text!(RevocationDocument, Rule::revoc);
60impl_parse_from_raw_text!(TransactionDocument, Rule::tx);
61impl_parse_from_raw_text!(TransactionDocumentV10, Rule::tx_v10);