#[cfg(test)]
mod tests {
use einvoice::validate_invoice;
use rstest::rstest;
use std::borrow::Cow;
use std::io::Read;
use std::path::PathBuf;
#[rstest]
fn validate_each_ubl_file(#[files("tests/inputs/ubl/*.xml")] path: PathBuf) {
let mut reader = std::io::BufReader::new(std::fs::File::open(&path).unwrap());
let mut original = String::new();
reader
.read_to_string(&mut original)
.expect("Cannot read file");
let invoice = validate_invoice(&original).unwrap();
let serialized = einvoice_deps_yaserde::ser::to_string(&invoice).unwrap();
let normalized_original = normalize_xml(&original);
let normalized_serialized = normalize_xml(&serialized);
assert_eq!(normalized_serialized, normalized_original);
}
#[rstest]
fn validate_each_cii_file(#[files("tests/inputs/cii/*.xml")] path: PathBuf) {
let mut reader = std::io::BufReader::new(std::fs::File::open(&path).unwrap());
let mut original = String::new();
reader
.read_to_string(&mut original)
.expect("Cannot read file");
let invoice = validate_invoice(&original).unwrap();
let serialized = einvoice_deps_yaserde::ser::to_string(&invoice).unwrap();
let normalized_original = normalize_xml(&original);
let normalized_serialized = normalize_xml(&serialized);
assert_eq!(normalized_serialized, normalized_original);
}
use regex::Regex;
use xmltree::{Element, ParserConfig};
fn normalize_xml(xml: &str) -> String {
let element = Element::parse_with_config(xml.as_bytes(), ParserConfig::new())
.expect("Failed to parse XML");
let mut buffer = Vec::new();
element
.write_with_config(
&mut buffer,
xmltree::EmitterConfig {
perform_indent: true, indent_string: Cow::from(" "), line_separator: Cow::from("\n".to_string()),
..Default::default()
},
)
.expect("Failed to write XML");
let string = String::from_utf8(buffer).expect("Failed to convert buffer to String");
simplify_invoice(&string)
}
fn simplify_invoice(input: &str) -> String {
let xmlns_re = Regex::new(r#" xmlns(:[a-zA-Z0-9_-]+)?="[^"]*""#).unwrap();
let result = xmlns_re.replace_all(input, "").to_string();
result.replace("ubl:Invoice", "Invoice") }
}