use std::process::ExitCode;
use einvoice_validator::{
Clock, Error, FindingKind, Severity, ValidateOptions, Validator, Verdict,
};
const VALID: &[u8] =
include_bytes!("../../../artefacts/cen-en16931-ubl-1.3.17/ubl/examples/ubl-tc434-example1.xml");
const BR_02: &[u8] =
include_bytes!("../../../corpus/cen-en16931-ubl-1.3.17/vefa/Invoice-unit-UBL/BR-02.002.xml");
fn main() -> ExitCode {
ExitCode::from(run())
}
fn run() -> u8 {
let Ok(validator) = Validator::cen_ubl() else {
return 100;
};
let Ok(clock) = Clock::from_unix_millis(60, 1_789_000_123_456) else {
return 101;
};
let options = ValidateOptions::new().clock(clock);
let Ok(report) = validator.validate_with(VALID, &options) else {
return 1;
};
if report.verdict() != Verdict::Valid || !report.schema_errors.is_empty() {
return 2;
}
if report.count(Severity::Fatal) != 0 || report.rule_set.version != "1.3.17" {
return 3;
}
let Ok(report) = validator.validate_owned_with(BR_02.to_vec(), &options) else {
return 4;
};
if report.verdict() != Verdict::Invalid {
return 5;
}
let br02 = report.findings().find(|f| f.id.as_deref() == Some("BR-02"));
if !br02.is_some_and(|f| {
f.kind == FindingKind::FailedAssert
&& f.severity == Severity::Fatal
&& f.text == "[BR-02]-An Invoice shall have an Invoice number (BT-1)."
}) {
return 6;
}
if report.schema_errors.len() != 1 {
return 7;
}
let without_clock = validator.validate(VALID);
let expected = if cfg!(all(target_family = "wasm", target_os = "unknown")) {
matches!(without_clock, Err(Error::Unsupported { .. }))
} else {
without_clock.is_ok_and(|r| r.verdict() == Verdict::Valid)
};
if !expected {
return 8;
}
0
}