Commit 34c6662a authored by Éloïs's avatar Éloïs

[feat] add parsers

parent 847d003b
use dubp_documents::*;
use dubp_documents::v10::identity::*;
use dubp_documents::v10::membership::*;
use dubp_documents::v10::certification::*;
use dubp_documents::v10::revocation::*;
use dubp_documents::v10::transaction::*;
use durs_network_documents::*;
use durs_network_documents::network_peer::PeerCardV11;
use durs_network_documents::network_head_v3::NetworkHeadV3;
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
#[derive(Debug, Copy, Clone)]
pub enum DocumentType {
All,
TxV10,
IdentityV10,
MembershipV10,
CertificationV10,
RevocationV10,
PeerV11,
HeadV3,
}
#[wasm_bindgen]
pub fn parse_doc_into_json(doc: &str, doc_type: DocumentType) -> String {
match doc_type {
DocumentType::All => match DUBPDocument::parse(doc) {
Ok(document) => serde_json::to_string_pretty(&document).unwrap_or("Fail to jsonifie document.".to_owned()),
Err(_) => match NetworkDocument::parse(doc) {
Ok(document) => serde_json::to_string_pretty(&document).unwrap_or("Fail to jsonifie document.".to_owned()),
Err(_) => "Invalid document. Specify the expected document type for more information.".to_owned(),
}
}
DocumentType::TxV10 => match TransactionDocumentParser::parse(doc) {
Ok(document) => serde_json::to_string_pretty(&document).unwrap_or("Fail to jsonifie document.".to_owned()),
Err(pest_error) => format!("{:?}", pest_error),
}
DocumentType::IdentityV10 => match IdentityDocumentParser::parse(doc) {
Ok(document) => IdentityDocument::to_json_string_pretty(&document).unwrap_or("Fail to jsonifie document.".to_owned()),
Err(pest_error) => format!("{:?}", pest_error),
}
DocumentType::MembershipV10 => match MembershipDocumentParser::parse(doc) {
Ok(document) => MembershipDocument::to_json_string_pretty(&document).unwrap_or("Fail to jsonifie document.".to_owned()),
Err(pest_error) => format!("{:?}", pest_error),
}
DocumentType::CertificationV10 => match CertificationDocumentParser::parse(doc) {
Ok(document) => serde_json::to_string_pretty(&document).unwrap_or("Fail to jsonifie document.".to_owned()),
Err(pest_error) => format!("{:?}", pest_error),
}
DocumentType::RevocationV10 => match RevocationDocumentParser::parse(doc) {
Ok(document) => serde_json::to_string_pretty(&document).unwrap_or("Fail to jsonifie document.".to_owned()),
Err(pest_error) => format!("{:?}", pest_error),
}
DocumentType::PeerV11 => match PeerCardV11::parse_from_raw(doc) {
Ok(document) => serde_json::to_string_pretty(&document).unwrap_or("Fail to jsonifie document.".to_owned()),
Err(pest_error) => format!("{:?}", pest_error),
}
DocumentType::HeadV3 => match NetworkHeadV3::parse_from_raw(doc) {
Ok(document) => serde_json::to_string_pretty(&document).unwrap_or("Fail to jsonifie document.".to_owned()),
Err(pest_error) => format!("{:?}", pest_error),
}
//_ => "Error: invalid argument value : doc_type. It's a bug in the calling code, please open an issue on the front interface repository.".to_owned()
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment