diff --git a/src/eu_vat/vies.rs b/src/eu_vat/vies.rs index c4381a0..6d1e377 100644 --- a/src/eu_vat/vies.rs +++ b/src/eu_vat/vies.rs @@ -25,8 +25,16 @@ impl VIES { for node in xml.descendants() { let tag_name = node.tag_name().name(); + if tag_name.trim().is_empty() { + continue; + } + if let Some(text) = node.text() { - hash.insert(tag_name.to_string(), text.to_string()); + if text == "---" { + hash.insert(tag_name.to_string(), "".to_string()); + } else { + hash.insert(tag_name.to_string(), text.to_string()); + } } } @@ -60,19 +68,23 @@ impl Verifier for VIES { if fault.is_some() { return Ok( Verification::new( - VerificationStatus::Unavailable + VerificationStatus::Unavailable, + hash ) ); } else { + let verification_status = match hash.get("valid") + .expect("Missing valid field in VIES response") + .as_str() { + "true" => VerificationStatus::Verified, + "false" => VerificationStatus::Unverified, + _ => panic!("Invalid value for valid field in VIES response") + }; + Ok( Verification::new( - if hash.get("valid").unwrap() == "true" { - VerificationStatus::Verified - } else if hash.get("valid").unwrap() == "false" { - VerificationStatus::Unverified - } else { - panic!("Unexpected response") - } + verification_status, + hash ) ) } @@ -95,7 +107,7 @@ mod tests { 2021-01-01+01:00 true Test Company -
Test Address
+
---
@@ -108,7 +120,7 @@ mod tests { assert_eq!(hash.get("requestDate"), Some(&"2021-01-01+01:00".to_string())); assert_eq!(hash.get("valid"), Some(&"true".to_string())); assert_eq!(hash.get("name"), Some(&"Test Company".to_string())); - assert_eq!(hash.get("address"), Some(&"Test Address".to_string())); + assert_eq!(hash.get("address"), Some(&"".to_string())); } #[test] @@ -174,5 +186,7 @@ mod tests { let verification = verifier.parse_response(response.to_string()).unwrap(); assert_eq!(*verification.status(), VerificationStatus::Unavailable); + assert_eq!(verification.data().get("faultcode"), Some(&"env:Server".to_string())); + assert_eq!(verification.data().get("faultstring"), Some(&"MS_MAX_CONCURRENT_REQ".to_string())); } } diff --git a/src/verification.rs b/src/verification.rs index 6f14aac..e644017 100644 --- a/src/verification.rs +++ b/src/verification.rs @@ -1,6 +1,7 @@ use chrono::prelude::*; use crate::tax_id::TaxId; use crate::errors::VerificationError; +use std::collections::HashMap; #[derive(Debug, PartialEq)] pub enum VerificationStatus { @@ -13,17 +14,20 @@ pub enum VerificationStatus { pub struct Verification { performed_at: DateTime, status: VerificationStatus, + data: HashMap, } impl Verification { - pub fn new(status: VerificationStatus) -> Verification { + pub fn new(status: VerificationStatus, data: HashMap) -> Verification { Verification { performed_at: Local::now(), status, + data, } } pub fn status(&self) -> &VerificationStatus { &self.status } + pub fn data(&self) -> &HashMap { &self.data } } pub trait Verifier { @@ -43,7 +47,10 @@ mod tests { #[test] fn test_new_verification() { - let verification = Verification::new(VerificationStatus::Verified); + let verification = Verification::new( + VerificationStatus::Verified, + HashMap::new(), + ); assert_eq!(*verification.status(), VerificationStatus::Verified); assert_eq!(verification.performed_at.date_naive(), Local::now().date_naive()); } @@ -56,8 +63,14 @@ mod tests { } fn parse_response(&self, response: String) -> Result { + let mut hash = HashMap::new(); + hash.insert("key".to_string(), "value".to_string()); + if response == "test" { - Ok(Verification::new(VerificationStatus::Verified)) + Ok(Verification::new( + VerificationStatus::Verified, + hash + )) } else { panic!("Unexpected response") } } } @@ -69,5 +82,6 @@ mod tests { let verification = verifier.verify(&tax_id).unwrap(); assert_eq!(*verification.status(), VerificationStatus::Verified); assert_eq!(verification.performed_at.date_naive(), Local::now().date_naive()); + assert_eq!(verification.data().get("key").unwrap(), "value"); } } \ No newline at end of file