dubp_documents_parser/
lib.rs

1//  Copyright (C) 2020  Éloïs SANCHEZ.
2//
3// This program is free software: you can redistribute it and/or modify
4// it under the terms of the GNU Affero General Public License as
5// published by the Free Software Foundation, either version 3 of the
6// License, or (at your option) any later version.
7//
8// This program is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11// GNU Affero General Public License for more details.
12//
13// You should have received a copy of the GNU Affero General Public License
14// along with this program.  If not, see <https://www.gnu.org/licenses/>.
15
16//! Provide parser for DUBP documents.
17
18#![allow(clippy::upper_case_acronyms)]
19#![deny(
20    clippy::expect_used,
21    clippy::unwrap_used,
22    missing_debug_implementations,
23    missing_copy_implementations,
24    trivial_casts,
25    trivial_numeric_casts,
26    unsafe_code,
27    unstable_features,
28    unused_import_braces
29)]
30
31mod compact_text;
32mod json;
33mod raw_text;
34mod stringified_object;
35mod transaction_utils;
36
37// Re-export crates
38pub use dubp_documents;
39pub use dubp_documents::dubp_wallet;
40
41// Prelude
42pub mod prelude {
43    pub use crate::compact_text::certifications::parse_compact_certifications;
44    pub use crate::compact_text::identities::parse_compact_identities;
45    pub use crate::compact_text::memberships::parse_compact_memberships;
46    pub use crate::compact_text::revoked::parse_compact_revocations;
47    pub use crate::compact_text::ParseCompactDocError;
48    pub use crate::json::transactions::{parse_json_transactions, ParseJsonTxError};
49    pub use crate::raw_text::{ParseFromRawText, Rule};
50    pub use crate::stringified_object::FromStringObject;
51    pub use crate::TextParseError;
52}
53
54// Export profession types
55pub use crate::raw_text::wallet_script::wallet_script_from_str;
56pub use crate::transaction_utils::tx_unlock_v10_from_str;
57
58// Crate imports
59pub(crate) use crate::json::DefaultHasher;
60pub(crate) use crate::prelude::*;
61pub(crate) use crate::raw_text::{FromPestPair, RawDocumentsParser};
62pub(crate) use crate::transaction_utils::{tx_input_v10_from_str, tx_output_v10_from_str};
63pub(crate) use dubp_documents::certification::{
64    v10::CertificationDocumentV10Builder, CertificationDocument, CertificationDocumentV10,
65    CompactCertificationDocumentV10,
66};
67pub(crate) use dubp_documents::dubp_common::crypto::bases::BaseConversionError;
68pub(crate) use dubp_documents::dubp_common::crypto::hashs::Hash;
69pub(crate) use dubp_documents::dubp_common::crypto::keys::*;
70pub(crate) use dubp_documents::dubp_common::prelude::*;
71pub(crate) use dubp_documents::identity::{
72    IdentityDocument, IdentityDocumentV10, IdentityDocumentV10Builder,
73};
74pub(crate) use dubp_documents::membership::{
75    MembershipDocument, MembershipDocumentV10, MembershipDocumentV10Builder, MembershipType,
76};
77pub(crate) use dubp_documents::prelude::*;
78pub(crate) use dubp_documents::revocation::{
79    v10::RevocationDocumentV10Builder, CompactRevocationDocumentV10, RevocationDocument,
80    RevocationDocumentV10,
81};
82pub(crate) use dubp_documents::smallvec::{smallvec as svec, SmallVec};
83pub(crate) use dubp_documents::transaction::{
84    v10::TransactionInputUnlocksV10, TransactionDocument, TransactionDocumentBuilder,
85    TransactionDocumentV10, TransactionDocumentV10Builder, TransactionDocumentV10Stringified,
86    TransactionInputV10, TransactionOutputV10, UTXOConditions,
87};
88pub(crate) use dubp_wallet::prelude::*;
89pub(crate) use pest::{
90    iterators::{Pair, Pairs},
91    Parser,
92};
93pub(crate) use pest_derive::Parser;
94pub(crate) use std::{net::AddrParseError, num::ParseIntError, str::FromStr};
95pub(crate) use thiserror::Error;
96
97/// Error with pest parser (grammar)
98#[derive(Debug, Clone, Eq, Error, PartialEq)]
99#[error("Grammar error: {0}")]
100pub struct PestError(pub String);
101
102impl<T: pest::RuleType> From<pest::error::Error<T>> for PestError {
103    fn from(e: pest::error::Error<T>) -> Self {
104        PestError(format!("{}", e))
105    }
106}
107
108/// List of possible errors while parsing a text document.
109#[derive(Debug, Clone, Eq, Error, PartialEq)]
110pub enum TextParseError {
111    /// Base 16/58/64 convertion error
112    #[error("field {field}: {error}")]
113    BaseConversionError {
114        field: &'static str,
115        error: BaseConversionError,
116    },
117    /// Fail to parse blockstamp
118    #[error("BlockstampParseError: {0}")]
119    BlockstampParseError(BlockstampParseError),
120    /// Fail to parse compact doc
121    #[error("Fail to parse compact doc (field '{field}'): {error}")]
122    CompactDoc {
123        field: &'static str,
124        error: ParseCompactDocError,
125    },
126    /// The given source don't have a valid specific document format (document type).
127    #[error("TextDocumentParseError: Invalid inner format: {0}")]
128    InvalidInnerFormat(String),
129    /// Ip address parse error
130    #[error("TextDocumentParseError: invalid ip: {0}")]
131    IpAddrError(AddrParseError),
132    /// Error with pest parser
133    #[error("TextDocumentParseError: {0}")]
134    PestError(PestError),
135    /// Unexpected rule
136    #[error("TextDocumentParseError: Unexpected rule: '{0}'")]
137    UnexpectedRule(String),
138    /// Unexpected version
139    #[error("TextDocumentParseError: Unexpected version: '{0}'")]
140    UnexpectedVersion(String),
141    /// Unknown type
142    #[error("TextDocumentParseError: UnknownType.")]
143    UnknownType,
144}
145
146impl From<AddrParseError> for TextParseError {
147    fn from(e: AddrParseError) -> Self {
148        TextParseError::IpAddrError(e)
149    }
150}
151
152impl From<PestError> for TextParseError {
153    fn from(e: PestError) -> Self {
154        TextParseError::PestError(e)
155    }
156}
157
158impl<T: pest::RuleType> From<pest::error::Error<T>> for TextParseError {
159    fn from(e: pest::error::Error<T>) -> Self {
160        TextParseError::PestError(e.into())
161    }
162}
163
164#[cfg(test)]
165pub mod tests {
166    use super::*;
167    use unwrap::unwrap;
168
169    #[inline(always)]
170    pub fn h(hash_str: &str) -> Hash {
171        unwrap!(Hash::from_hex(hash_str))
172    }
173
174    #[inline(always)]
175    pub fn pk(pk_b58: &str) -> ed25519::PublicKey {
176        unwrap!(PublicKey::from_base58(pk_b58))
177    }
178}