Commit 4f2d6b54 authored by Éloïs's avatar Éloïs

[ref] documents+network-documents: create DomainName type

parent 9db73f17
Pipeline #5866 passed with stages
in 16 minutes and 13 seconds
......@@ -291,7 +291,7 @@ dependencies = [
[[package]]
name = "dubp-documents"
version = "0.12.0"
version = "0.13.0"
dependencies = [
"dup-crypto 0.6.0",
"dup-currency-params 0.1.0",
......@@ -312,7 +312,7 @@ dependencies = [
name = "dubp-documents-tests-tools"
version = "0.1.0"
dependencies = [
"dubp-documents 0.12.0",
"dubp-documents 0.13.0",
"dup-crypto 0.6.0",
"dup-crypto-tests-tools 0.1.0",
"dup-currency-params 0.1.0",
......@@ -372,7 +372,7 @@ name = "durs-blockchain"
version = "0.2.0-a"
dependencies = [
"dirs 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
"dubp-documents 0.12.0",
"dubp-documents 0.13.0",
"dubp-documents-tests-tools 0.1.0",
"dup-crypto 0.6.0",
"dup-crypto-tests-tools 0.1.0",
......@@ -383,7 +383,7 @@ dependencies = [
"durs-message 0.2.0-a",
"durs-module 0.2.0-a",
"durs-network 0.2.0-a",
"durs-network-documents 0.3.1",
"durs-network-documents 0.4.0",
"durs-wot 0.8.0-a0.9",
"failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
"json-pest-parser 0.1.0",
......@@ -401,7 +401,7 @@ dependencies = [
name = "durs-blockchain-dal"
version = "0.2.0-a"
dependencies = [
"dubp-documents 0.12.0",
"dubp-documents 0.13.0",
"dubp-documents-tests-tools 0.1.0",
"dup-crypto 0.6.0",
"dup-crypto-tests-tools 0.1.0",
......@@ -441,7 +441,7 @@ name = "durs-conf"
version = "0.2.0-a"
dependencies = [
"dirs 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
"dubp-documents 0.12.0",
"dubp-documents 0.13.0",
"dup-crypto 0.6.0",
"dup-currency-params 0.1.0",
"durs-common-tools 0.1.0",
......@@ -468,7 +468,7 @@ dependencies = [
"durs-message 0.2.0-a",
"durs-module 0.2.0-a",
"durs-network 0.2.0-a",
"durs-network-documents 0.3.1",
"durs-network-documents 0.4.0",
"failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.86 (registry+https://github.com/rust-lang/crates.io-index)",
......@@ -484,13 +484,13 @@ dependencies = [
name = "durs-message"
version = "0.2.0-a"
dependencies = [
"dubp-documents 0.12.0",
"dubp-documents 0.13.0",
"dup-crypto 0.6.0",
"dup-currency-params 0.1.0",
"durs-blockchain-dal 0.2.0-a",
"durs-module 0.2.0-a",
"durs-network 0.2.0-a",
"durs-network-documents 0.3.1",
"durs-network-documents 0.4.0",
"serde 1.0.86 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.86 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 1.0.38 (registry+https://github.com/rust-lang/crates.io-index)",
......@@ -500,11 +500,11 @@ dependencies = [
name = "durs-module"
version = "0.2.0-a"
dependencies = [
"dubp-documents 0.12.0",
"dubp-documents 0.13.0",
"dup-crypto 0.6.0",
"dup-currency-params 0.1.0",
"durs-common-tools 0.1.0",
"durs-network-documents 0.3.1",
"durs-network-documents 0.4.0",
"failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.86 (registry+https://github.com/rust-lang/crates.io-index)",
......@@ -517,11 +517,11 @@ dependencies = [
name = "durs-network"
version = "0.2.0-a"
dependencies = [
"dubp-documents 0.12.0",
"dubp-documents 0.13.0",
"dup-crypto 0.6.0",
"durs-common-tools 0.1.0",
"durs-module 0.2.0-a",
"durs-network-documents 0.3.1",
"durs-network-documents 0.4.0",
"failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.86 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.86 (registry+https://github.com/rust-lang/crates.io-index)",
......@@ -531,14 +531,15 @@ dependencies = [
[[package]]
name = "durs-network-documents"
version = "0.3.1"
version = "0.4.0"
dependencies = [
"base58 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"bincode 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
"dubp-documents 0.12.0",
"dubp-documents 0.13.0",
"dup-crypto 0.6.0",
"dup-currency-params 0.1.0",
"durs-common-tools 0.1.0",
"failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
"hex 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
"maplit 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
......@@ -553,7 +554,7 @@ dependencies = [
name = "durs-skeleton"
version = "0.1.0"
dependencies = [
"dubp-documents 0.12.0",
"dubp-documents 0.13.0",
"dup-crypto 0.6.0",
"dup-currency-params 0.1.0",
"durs-common-tools 0.1.0",
......@@ -573,7 +574,7 @@ dependencies = [
name = "durs-tui"
version = "0.2.0-a"
dependencies = [
"dubp-documents 0.12.0",
"dubp-documents 0.13.0",
"dup-crypto 0.6.0",
"dup-currency-params 0.1.0",
"durs-common-tools 0.1.0",
......@@ -581,7 +582,7 @@ dependencies = [
"durs-message 0.2.0-a",
"durs-module 0.2.0-a",
"durs-network 0.2.0-a",
"durs-network-documents 0.3.1",
"durs-network-documents 0.4.0",
"failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.86 (registry+https://github.com/rust-lang/crates.io-index)",
......@@ -609,7 +610,7 @@ name = "durs-ws2p"
version = "0.2.0-a"
dependencies = [
"bincode 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
"dubp-documents 0.12.0",
"dubp-documents 0.13.0",
"dup-crypto 0.6.0",
"dup-currency-params 0.1.0",
"durs-common-tests-tools 0.1.0",
......@@ -618,7 +619,7 @@ dependencies = [
"durs-message 0.2.0-a",
"durs-module 0.2.0-a",
"durs-network 0.2.0-a",
"durs-network-documents 0.3.1",
"durs-network-documents 0.4.0",
"durs-ws2p-messages 0.2.0-a",
"durs-ws2p-protocol 0.2.0-a",
"failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
......@@ -637,11 +638,11 @@ version = "0.2.0-a"
dependencies = [
"bincode 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
"byteorder 1.2.7 (registry+https://github.com/rust-lang/crates.io-index)",
"dubp-documents 0.12.0",
"dubp-documents 0.13.0",
"dup-crypto 0.6.0",
"dup-currency-params 0.1.0",
"durs-common-tools 0.1.0",
"durs-network-documents 0.3.1",
"durs-network-documents 0.4.0",
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
"pretty_assertions 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.86 (registry+https://github.com/rust-lang/crates.io-index)",
......@@ -653,12 +654,12 @@ dependencies = [
name = "durs-ws2p-protocol"
version = "0.2.0-a"
dependencies = [
"dubp-documents 0.12.0",
"dubp-documents 0.13.0",
"dup-crypto 0.6.0",
"dup-currency-params 0.1.0",
"durs-common-tools 0.1.0",
"durs-module 0.2.0-a",
"durs-network-documents 0.3.1",
"durs-network-documents 0.4.0",
"durs-ws2p-messages 0.2.0-a",
"failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
......@@ -674,7 +675,7 @@ version = "0.2.0-a"
dependencies = [
"bincode 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
"byteorder 1.2.7 (registry+https://github.com/rust-lang/crates.io-index)",
"dubp-documents 0.12.0",
"dubp-documents 0.13.0",
"dup-crypto 0.6.0",
"dup-currency-params 0.1.0",
"durs-common-tools 0.1.0",
......@@ -682,7 +683,7 @@ dependencies = [
"durs-message 0.2.0-a",
"durs-module 0.2.0-a",
"durs-network 0.2.0-a",
"durs-network-documents 0.3.1",
"durs-network-documents 0.4.0",
"durs-wot 0.8.0-a0.9",
"failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
......
......@@ -198,7 +198,7 @@ mod tests {
api_features: ApiFeatures(vec![7u8]),
ip_v4: None,
ip_v6: None,
host: Some(String::from("g1.durs.ifee.fr")),
domain: Some(String::from("g1.durs.ifee.fr")),
port: 443u16,
path: Some(String::from("ws2p")),
}
......@@ -211,7 +211,7 @@ mod tests {
api_features: ApiFeatures(vec![7u8]),
ip_v4: Some(Ipv4Addr::from_str("84.16.72.210").unwrap()),
ip_v6: None,
host: None,
domain: None,
port: 443u16,
path: Some(String::from("ws2p")),
}
......
[package]
name = "dubp-documents"
version = "0.12.0"
version = "0.13.0"
authors = ["nanocryk <nanocryk@duniter.org>", "elois <elois@ifee.fr>"]
description = "Handles DUBP documents (DUBP: DUniter Blockhain Protocol)"
repository = "https://git.duniter.org/nodes/rust/duniter-rs"
......
......@@ -296,7 +296,9 @@ impl TextDocumentParser<Rule> for CertificationDocumentParser {
let cert_vx_pair = cert_pair.into_inner().next().unwrap(); // get and unwrap the `cert_vX` rule; never fails
match cert_vx_pair.as_rule() {
Rule::cert_v10 => Ok(CertificationDocumentParser::from_pest_pair(cert_vx_pair)),
Rule::cert_v10 => {
Ok(CertificationDocumentParser::from_pest_pair(cert_vx_pair)?)
}
_ => Err(TextDocumentParseError::UnexpectedVersion(format!(
"{:#?}",
cert_vx_pair.as_rule()
......@@ -306,7 +308,7 @@ impl TextDocumentParser<Rule> for CertificationDocumentParser {
Err(pest_error) => fatal_error!("{}", pest_error), //Err(TextDocumentParseError::PestError()),
}
}
fn from_pest_pair(pair: Pair<Rule>) -> Self::DocumentType {
fn from_pest_pair(pair: Pair<Rule>) -> Result<Self::DocumentType, TextDocumentParseError> {
let doc = pair.as_str();
let mut currency = "";
let mut pubkeys = Vec::with_capacity(2);
......@@ -341,7 +343,8 @@ impl TextDocumentParser<Rule> for CertificationDocumentParser {
_ => fatal_error!("unexpected rule"), // Grammar ensures that we never reach this line
}
}
CertificationDocument {
Ok(CertificationDocument {
text: doc.to_owned(),
issuers: vec![pubkeys[0]],
currency: currency.to_owned(),
......@@ -351,7 +354,7 @@ impl TextDocumentParser<Rule> for CertificationDocumentParser {
identity_sig: sigs[0],
blockstamp: blockstamps[1],
signatures: vec![sigs[1]],
}
})
}
}
......
......@@ -233,23 +233,19 @@ impl TextDocumentParser<Rule> for IdentityDocumentParser {
type DocumentType = IdentityDocument;
fn parse(doc: &str) -> Result<Self::DocumentType, TextDocumentParseError> {
match DocumentsParser::parse(Rule::idty, doc) {
Ok(mut doc_pairs) => {
let idty_pair = doc_pairs.next().unwrap(); // get and unwrap the `idty` rule; never fails
let idty_vx_pair = idty_pair.into_inner().next().unwrap(); // get and unwrap the `idty_vx` rule; never fails
match idty_vx_pair.as_rule() {
Rule::idty_v10 => Ok(IdentityDocumentParser::from_pest_pair(idty_vx_pair)),
_ => Err(TextDocumentParseError::UnexpectedVersion(format!(
"{:#?}",
idty_vx_pair.as_rule()
))),
}
}
Err(pest_error) => Err(TextDocumentParseError::PestError(format!("{}", pest_error))),
let mut doc_pairs = DocumentsParser::parse(Rule::idty, doc)?;
let idty_pair = doc_pairs.next().unwrap(); // get and unwrap the `idty` rule; never fails
let idty_vx_pair = idty_pair.into_inner().next().unwrap(); // get and unwrap the `idty_vx` rule; never fails
match idty_vx_pair.as_rule() {
Rule::idty_v10 => Ok(IdentityDocumentParser::from_pest_pair(idty_vx_pair)?),
_ => Err(TextDocumentParseError::UnexpectedVersion(format!(
"{:#?}",
idty_vx_pair.as_rule()
))),
}
}
fn from_pest_pair(pair: Pair<Rule>) -> Self::DocumentType {
fn from_pest_pair(pair: Pair<Rule>) -> Result<Self::DocumentType, TextDocumentParseError> {
let doc = pair.as_str();
let mut currency = "";
let mut pubkey_str = "";
......@@ -276,7 +272,8 @@ impl TextDocumentParser<Rule> for IdentityDocumentParser {
_ => fatal_error!("unexpected rule"), // Grammar ensures that we never reach this line
}
}
IdentityDocument {
Ok(IdentityDocument {
text: Some(doc.to_owned()),
currency: currency.to_owned(),
username: uid.to_owned(),
......@@ -287,7 +284,7 @@ impl TextDocumentParser<Rule> for IdentityDocumentParser {
signatures: vec![Sig::Ed25519(
ed25519::Signature::from_base64(sig_str).unwrap(),
)], // Grammar ensures that we have a base64 string.
}
})
}
}
......
......@@ -291,25 +291,19 @@ impl TextDocumentParser<Rule> for MembershipDocumentParser {
type DocumentType = MembershipDocument;
fn parse(doc: &str) -> Result<Self::DocumentType, TextDocumentParseError> {
match DocumentsParser::parse(Rule::membership, doc) {
Ok(mut ms_pairs) => {
let ms_pair = ms_pairs.next().unwrap(); // get and unwrap the `membership` rule; never fails
let ms_vx_pair = ms_pair.into_inner().next().unwrap(); // get and unwrap the `membership_vX` rule; never fails
match ms_vx_pair.as_rule() {
Rule::membership_v10 => {
Ok(MembershipDocumentParser::from_pest_pair(ms_vx_pair))
}
_ => Err(TextDocumentParseError::UnexpectedVersion(format!(
"{:#?}",
ms_vx_pair.as_rule()
))),
}
}
Err(pest_error) => Err(TextDocumentParseError::PestError(format!("{}", pest_error))),
let mut ms_pairs = DocumentsParser::parse(Rule::membership, doc)?;
let ms_pair = ms_pairs.next().unwrap(); // get and unwrap the `membership` rule; never fails
let ms_vx_pair = ms_pair.into_inner().next().unwrap(); // get and unwrap the `membership_vX` rule; never fails
match ms_vx_pair.as_rule() {
Rule::membership_v10 => Ok(MembershipDocumentParser::from_pest_pair(ms_vx_pair)?),
_ => Err(TextDocumentParseError::UnexpectedVersion(format!(
"{:#?}",
ms_vx_pair.as_rule()
))),
}
}
fn from_pest_pair(pair: Pair<Rule>) -> Self::DocumentType {
fn from_pest_pair(pair: Pair<Rule>) -> Result<Self::DocumentType, TextDocumentParseError> {
let doc = pair.as_str();
let mut currency = "";
let mut pubkey_str = "";
......@@ -339,7 +333,8 @@ impl TextDocumentParser<Rule> for MembershipDocumentParser {
_ => fatal_error!("unexpected rule"), // Grammar ensures that we never reach this line
}
}
MembershipDocument {
Ok(MembershipDocument {
text: Some(doc.to_owned()),
issuers: vec![PubKey::Ed25519(
ed25519::PublicKey::from_base58(pubkey_str).unwrap(),
......@@ -352,7 +347,7 @@ impl TextDocumentParser<Rule> for MembershipDocumentParser {
signatures: vec![Sig::Ed25519(
ed25519::Signature::from_base64(sig_str).unwrap(),
)], // Grammar ensures that we have a base64 string.
}
})
}
}
......
......@@ -107,40 +107,40 @@ impl TextDocumentParser<Rule> for DUBPDocument {
fn parse(doc: &str) -> Result<DUBPDocument, TextDocumentParseError> {
match DocumentsParser::parse(Rule::document, doc) {
Ok(mut doc_pairs) => Ok(DUBPDocument::from_pest_pair(doc_pairs.next().unwrap())), // get and unwrap the `document` rule; never fails
Err(pest_error) => Err(TextDocumentParseError::PestError(format!("{}", pest_error))),
Ok(mut doc_pairs) => Ok(DUBPDocument::from_pest_pair(doc_pairs.next().unwrap())?), // get and unwrap the `document` rule; never fails
Err(pest_error) => Err(pest_error.into()),
}
}
fn from_pest_pair(pair: Pair<Rule>) -> Self::DocumentType {
fn from_pest_pair(pair: Pair<Rule>) -> Result<Self::DocumentType, TextDocumentParseError> {
let doc_vx_pair = pair.into_inner().next().unwrap(); // get and unwrap the `document_vX` rule; never fails
match doc_vx_pair.as_rule() {
Rule::document_v10 => DUBPDocument::from_pest_pair_v10(doc_vx_pair),
Rule::document_v10 => Ok(DUBPDocument::from_pest_pair_v10(doc_vx_pair)?),
_ => fatal_error!("unexpected rule: {:?}", doc_vx_pair.as_rule()), // Grammar ensures that we never reach this line
}
}
}
impl DUBPDocument {
pub fn from_pest_pair_v10(pair: Pair<Rule>) -> DUBPDocument {
pub fn from_pest_pair_v10(pair: Pair<Rule>) -> Result<DUBPDocument, TextDocumentParseError> {
let doc_type_v10_pair = pair.into_inner().next().unwrap(); // get and unwrap the `{DOC_TYPE}_v10` rule; never fails
match doc_type_v10_pair.as_rule() {
Rule::idty_v10 => DUBPDocument::Identity(
identity::IdentityDocumentParser::from_pest_pair(doc_type_v10_pair),
),
Rule::membership_v10 => DUBPDocument::Membership(
membership::MembershipDocumentParser::from_pest_pair(doc_type_v10_pair),
),
Rule::cert_v10 => DUBPDocument::Certification(Box::new(
certification::CertificationDocumentParser::from_pest_pair(doc_type_v10_pair),
Rule::idty_v10 => Ok(DUBPDocument::Identity(
identity::IdentityDocumentParser::from_pest_pair(doc_type_v10_pair)?,
)),
Rule::revoc_v10 => DUBPDocument::Revocation(Box::new(
revocation::RevocationDocumentParser::from_pest_pair(doc_type_v10_pair),
)),
Rule::tx_v10 => DUBPDocument::Transaction(Box::new(
transaction::TransactionDocumentParser::from_pest_pair(doc_type_v10_pair),
Rule::membership_v10 => Ok(DUBPDocument::Membership(
membership::MembershipDocumentParser::from_pest_pair(doc_type_v10_pair)?,
)),
Rule::cert_v10 => Ok(DUBPDocument::Certification(Box::new(
certification::CertificationDocumentParser::from_pest_pair(doc_type_v10_pair)?,
))),
Rule::revoc_v10 => Ok(DUBPDocument::Revocation(Box::new(
revocation::RevocationDocumentParser::from_pest_pair(doc_type_v10_pair)?,
))),
Rule::tx_v10 => Ok(DUBPDocument::Transaction(Box::new(
transaction::TransactionDocumentParser::from_pest_pair(doc_type_v10_pair)?,
))),
_ => fatal_error!("unexpected rule: {:?}", doc_type_v10_pair.as_rule()), // Grammar ensures that we never reach this line
}
}
......
......@@ -246,23 +246,19 @@ impl TextDocumentParser<Rule> for RevocationDocumentParser {
type DocumentType = RevocationDocument;
fn parse(doc: &str) -> Result<Self::DocumentType, TextDocumentParseError> {
match DocumentsParser::parse(Rule::revoc, doc) {
Ok(mut revoc_pairs) => {
let revoc_pair = revoc_pairs.next().unwrap(); // get and unwrap the `revoc` rule; never fails
let revoc_vx_pair = revoc_pair.into_inner().next().unwrap(); // get and unwrap the `revoc_vX` rule; never fails
match revoc_vx_pair.as_rule() {
Rule::revoc_v10 => Ok(RevocationDocumentParser::from_pest_pair(revoc_vx_pair)),
_ => Err(TextDocumentParseError::UnexpectedVersion(format!(
"{:#?}",
revoc_vx_pair.as_rule()
))),
}
}
Err(pest_error) => Err(TextDocumentParseError::PestError(format!("{}", pest_error))),
let mut revoc_pairs = DocumentsParser::parse(Rule::revoc, doc)?;
let revoc_pair = revoc_pairs.next().unwrap(); // get and unwrap the `revoc` rule; never fails
let revoc_vx_pair = revoc_pair.into_inner().next().unwrap(); // get and unwrap the `revoc_vX` rule; never fails
match revoc_vx_pair.as_rule() {
Rule::revoc_v10 => Ok(RevocationDocumentParser::from_pest_pair(revoc_vx_pair)?),
_ => Err(TextDocumentParseError::UnexpectedVersion(format!(
"{:#?}",
revoc_vx_pair.as_rule()
))),
}
}
fn from_pest_pair(pair: Pair<Rule>) -> Self::DocumentType {
fn from_pest_pair(pair: Pair<Rule>) -> Result<Self::DocumentType, TextDocumentParseError> {
let doc = pair.as_str();
let mut currency = "";
let mut pubkeys = Vec::with_capacity(1);
......@@ -297,7 +293,7 @@ impl TextDocumentParser<Rule> for RevocationDocumentParser {
_ => fatal_error!("unexpected rule"), // Grammar ensures that we never reach this line
}
}
RevocationDocument {
Ok(RevocationDocument {
text: doc.to_owned(),
issuers: vec![pubkeys[0]],
currency: currency.to_owned(),
......@@ -305,7 +301,7 @@ impl TextDocumentParser<Rule> for RevocationDocumentParser {
identity_blockstamp: blockstamps[0],
identity_sig: sigs[0],
signatures: vec![sigs[1]],
}
})
}
}
......
......@@ -855,23 +855,19 @@ impl TextDocumentParser<Rule> for TransactionDocumentParser {
type DocumentType = TransactionDocument;
fn parse(doc: &str) -> Result<Self::DocumentType, TextDocumentParseError> {
match DocumentsParser::parse(Rule::tx, doc) {
Ok(mut tx_pairs) => {
let tx_pair = tx_pairs.next().unwrap(); // get and unwrap the `tx` rule; never fails
let tx_vx_pair = tx_pair.into_inner().next().unwrap(); // get and unwrap the `tx_vX` rule; never fails
match tx_vx_pair.as_rule() {
Rule::tx_v10 => Ok(TransactionDocumentParser::from_pest_pair(tx_vx_pair)),
_ => Err(TextDocumentParseError::UnexpectedVersion(format!(
"{:#?}",
tx_vx_pair.as_rule()
))),
}
}
Err(pest_error) => Err(TextDocumentParseError::PestError(format!("{}", pest_error))),
let mut tx_pairs = DocumentsParser::parse(Rule::tx, doc)?;
let tx_pair = tx_pairs.next().unwrap(); // get and unwrap the `tx` rule; never fails
let tx_vx_pair = tx_pair.into_inner().next().unwrap(); // get and unwrap the `tx_vX` rule; never fails
match tx_vx_pair.as_rule() {
Rule::tx_v10 => Ok(TransactionDocumentParser::from_pest_pair(tx_vx_pair)?),
_ => Err(TextDocumentParseError::UnexpectedVersion(format!(
"{:#?}",
tx_vx_pair.as_rule()
))),
}
}
fn from_pest_pair(pair: Pair<Rule>) -> Self::DocumentType {
fn from_pest_pair(pair: Pair<Rule>) -> Result<Self::DocumentType, TextDocumentParseError> {
let doc = pair.as_str();
let mut currency = "";
let mut blockstamp = Blockstamp::default();
......@@ -917,7 +913,8 @@ impl TextDocumentParser<Rule> for TransactionDocumentParser {
_ => fatal_error!("unexpected rule: {:?}", field.as_rule()), // Grammar ensures that we never reach this line
}
}
TransactionDocument {
Ok(TransactionDocument {
text: Some(doc.to_owned()),
currency: currency.to_owned(),
blockstamp,
......@@ -929,7 +926,7 @@ impl TextDocumentParser<Rule> for TransactionDocumentParser {
comment: comment.to_owned(),
signatures: sigs,
hash: None,
}
})
}
}
......
......@@ -49,6 +49,7 @@ use pest::RuleType;
use serde::Serialize;
use std::cmp::Ordering;
use std::fmt::{Debug, Display, Error, Formatter};
use std::net::AddrParseError;
pub use crate::blockstamp::{Blockstamp, PreviousBlockstamp};
......@@ -64,26 +65,58 @@ pub trait TextDocumentParser<R: RuleType> {
/// Parse text document from raw format
fn parse(doc: &str) -> Result<Self::DocumentType, TextDocumentParseError>;
/// Parse text document from pest pairs
fn from_pest_pair(pairs: Pair<R>) -> Self::DocumentType;
fn from_pest_pair(pairs: Pair<R>) -> Result<Self::DocumentType, TextDocumentParseError>;
}
/// List of possible errors while parsing.
#[derive(Debug, Clone, Fail)]
/// Error with pest parser (grammar)
#[derive(Debug, Clone, Eq, Fail, PartialEq)]
#[fail(display = "Grammar error: {}", _0)]
pub struct PestError(pub String);
impl<T: pest::RuleType> From<pest::error::Error<T>> for PestError {
fn from(e: pest::error::Error<T>) -> Self {
PestError(format!("{}", e))
}
}
/// List of possible errors while parsing a text document.
#[derive(Debug, Clone, Eq, Fail, PartialEq)]
pub enum TextDocumentParseError {
/// The given source don't have a valid specific document format (document type).
#[fail(display = "TextDocumentParseError: Invalid inner format: {}", _0)]
InvalidInnerFormat(String),
/// Ip address parse error
#[fail(display = "TextDocumentParseError: invalid ip: {}", _0)]
IpAddrError(AddrParseError),
/// Error with pest parser
#[fail(display = "TextDocumentParseError: PestError.")]
PestError(String),
#[fail(display = "TextDocumentParseError: UnexpectedVersion.")]
/// UnexpectedVersion
#[fail(display = "TextDocumentParseError: {}", _0)]
PestError(PestError),
/// Unexpected version
#[fail(display = "TextDocumentParseError: UnexpectedVersion: {}", _0)]
UnexpectedVersion(String),
#[fail(display = "TextDocumentParseError: UnknownType.")]
/// Unknown type
#[fail(display = "TextDocumentParseError: UnknownType.")]
UnknownType,
}
impl From<AddrParseError> for TextDocumentParseError {
fn from(e: AddrParseError) -> Self {
TextDocumentParseError::IpAddrError(e)
}
}
impl From<PestError> for TextDocumentParseError {
fn from(e: PestError) -> Self {
TextDocumentParseError::PestError(e)
}
}
impl<T: pest::RuleType> From<pest::error::Error<T>> for TextDocumentParseError {
fn from(e: pest::error::Error<T>) -> Self {
TextDocumentParseError::PestError(e.into())
}
}
/// A block Id.
#[derive(Copy, Clone, Debug, Deserialize, Ord, PartialEq, PartialOrd, Eq, Hash, Serialize)]
pub struct BlockNumber(pub u32);
......
[package]
name = "durs-network-documents"
version = "0.3.1"
version = "0.4.0"
authors = ["librelois <elois@duniter.org>"]
description = "Network documents for DUNP (DUniter Network Protocol)"
repository = "https://git.duniter.org/nodes/rust/duniter-rs"
......@@ -12,13 +12,14 @@ edition = "2018"
path = "src/lib.rs"
[dependencies]
log = "0.4.*"
durs-common-tools = { path = "../common-tools" }
base58 = "0.1.*"
durs-common-tools = { path = "../common-tools" }
dup-crypto = { path = "../crypto" }
dup-currency-params = { path = "../../tools/currency-params" }
dubp-documents= { path = "../documents" }
failure = "0.1.5"
hex = "0.3.*"
log = "0.4.*"
pest = "2.1.0"
pest_derive = "2.1.0"
serde = { version = "1.0.*", features = ["derive"] }
......
// Copyright (C) 2018 The Duniter Project Developers.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//! Define host type
use crate::*;
use dubp_documents::PestError;
use failure::Fail;
use pest::Parser;
use std::net::{AddrParseError, IpAddr};
use std::str::FromStr;
#[derive(Clone, Debug, Hash)]
/// Domain name
pub struct DomainName(String);
impl FromStr for DomainName {
type Err = PestError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match NetworkDocsParser::parse(Rule::domain_name_inner, s) {
Ok(pairs) => Ok(DomainName(String::from(pairs.as_str()))),
Err(pest_error) => Err(PestError(format!("{}", pest_error))),
}
}
}
impl AsRef<str> for DomainName {
fn as_ref(&self) -> &str {
&self.0
}
}
impl ToString for DomainName {
fn to_string(&self) -> String {
self.0.clone()
}
}
#[derive(Clone, Debug, Hash)]
/// Host
pub enum Host {
/// Domain name
DomainName(DomainName),
// Ip address
Ip(IpAddr),
}
#[derive(Clone, Debug, Fail)]
#[fail(
display = "Fail to parse host: It's neither a valid ip address nor a valid domain name. IP error: {}. Domain name error: {}.",
ip_err, domain_err
)]
/// Host parse error
pub struct HostParseError {
ip_err: AddrParseError,
domain_err: PestError,
}
impl FromStr for Host {
type Err = HostParseError;
fn from_str(s: &str) -> Result<Self,