use std::fmt;
use einvoice_xml::Encoding;
use crate::manifest::{CII, UBL_CREDIT_NOTE, UBL_INVOICE};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Syntax {
Ubl,
Cii,
}
impl Syntax {
#[must_use]
pub fn detect(document: &[u8]) -> Option<Self> {
let (local, namespace) = match einvoice_xml::encoding_of(document) {
Ok(encoding @ (Encoding::Utf16Le | Encoding::Utf16Be)) => {
root_name(utf16_lossy(document, encoding).as_bytes())?
}
_ => root_name(document)?,
};
match (local.as_str(), namespace.as_str()) {
("Invoice", UBL_INVOICE) | ("CreditNote", UBL_CREDIT_NOTE) => Some(Self::Ubl),
("CrossIndustryInvoice", CII) => Some(Self::Cii),
_ => None,
}
}
#[must_use]
pub const fn builtin_rule_set(self) -> &'static str {
match self {
Self::Ubl => "cen-ubl",
Self::Cii => "cen-cii",
}
}
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::Ubl => "ubl",
Self::Cii => "cii",
}
}
}
impl fmt::Display for Syntax {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
fn utf16_lossy(document: &[u8], encoding: Encoding) -> String {
let bytes = document
.strip_prefix(&[0xFF, 0xFE])
.or_else(|| document.strip_prefix(&[0xFE, 0xFF]))
.unwrap_or(document);
let units = bytes.as_chunks::<2>().0.iter().map(|&pair| {
if encoding == Encoding::Utf16Le {
u16::from_le_bytes(pair)
} else {
u16::from_be_bytes(pair)
}
});
char::decode_utf16(units)
.map(|c| c.unwrap_or(char::REPLACEMENT_CHARACTER))
.collect()
}
const fn is_space(b: u8) -> bool {
matches!(b, b' ' | b'\t' | b'\r' | b'\n')
}
fn root_name(document: &[u8]) -> Option<(String, String)> {
let text = document.strip_prefix(b"\xEF\xBB\xBF").unwrap_or(document);
let mut rest = text;
loop {
let skip = rest.iter().position(|&b| !is_space(b))?;
rest = &rest[skip..];
if let Some(after) = rest.strip_prefix(b"<?") {
rest = &after[find(after, b"?>")? + 2..];
} else if let Some(after) = rest.strip_prefix(b"<!--") {
rest = &after[find(after, b"-->")? + 3..];
} else if rest.starts_with(b"<!") {
return None;
} else {
rest = rest.strip_prefix(b"<")?;
break;
}
}
let (qname, mut rest) = name(rest)?;
let mut namespaces: Vec<(String, String)> = Vec::new();
loop {
let skip = rest.iter().position(|&b| !is_space(b))?;
let spaced = skip > 0;
rest = &rest[skip..];
if rest.starts_with(b">") || rest.starts_with(b"/>") {
break;
}
if !spaced {
return None;
}
let (attribute, after) = name(rest)?;
let after = trim_start(after).strip_prefix(b"=")?;
let after = trim_start(after);
let quote = *after.first().filter(|q| matches!(q, b'"' | b'\''))?;
let after = &after[1..];
let end = after.iter().position(|&b| b == quote)?;
rest = &after[end + 1..];
let Ok(value) = std::str::from_utf8(&after[..end]) else {
continue;
};
if value.contains(['&', '<']) {
continue;
}
if attribute == "xmlns" {
namespaces.push((String::new(), value.to_owned()));
} else if let Some(prefix) = attribute.strip_prefix("xmlns:") {
namespaces.push((prefix.to_owned(), value.to_owned()));
}
}
let (prefix, local) = qname.split_once(':').unwrap_or(("", &qname));
let namespace = namespaces
.iter()
.rev()
.find(|(p, _)| p == prefix)
.map(|(_, uri)| uri.clone())
.unwrap_or_default();
Some((local.to_owned(), namespace))
}
fn trim_start(bytes: &[u8]) -> &[u8] {
let skip = bytes
.iter()
.position(|&b| !is_space(b))
.unwrap_or(bytes.len());
&bytes[skip..]
}
fn find(haystack: &[u8], needle: &[u8]) -> Option<usize> {
haystack.windows(needle.len()).position(|w| w == needle)
}
fn name(bytes: &[u8]) -> Option<(String, &[u8])> {
let end = bytes
.iter()
.position(|&b| is_space(b) || matches!(b, b'=' | b'>' | b'/'))
.unwrap_or(bytes.len());
if end == 0 {
return None;
}
let text = std::str::from_utf8(&bytes[..end]).ok()?;
Some((text.to_owned(), &bytes[end..]))
}
#[cfg(test)]
mod tests {
use super::Syntax;
fn detect(text: &str) -> Option<Syntax> {
Syntax::detect(text.as_bytes())
}
#[test]
fn root_elements() {
assert_eq!(
detect(r#"<Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2"/>"#),
Some(Syntax::Ubl)
);
assert_eq!(
detect(
"\u{feff}<?xml version='1.0'?>\n<!-- c --><?pi x?>\n<ubl:CreditNote a='1' xmlns:ubl='urn:oasis:names:specification:ubl:schema:xsd:CreditNote-2'>"
),
Some(Syntax::Ubl)
);
assert_eq!(
detect(
r#"<rsm:CrossIndustryInvoice xmlns:a="x" xmlns:rsm="urn:un:unece:uncefact:data:standard:CrossIndustryInvoice:100"><x/></rsm:CrossIndustryInvoice>"#
),
Some(Syntax::Cii)
);
assert_eq!(
detect(
"<CrossIndustryInvoice\n xmlns = \"urn:un:unece:uncefact:data:standard:CrossIndustryInvoice:100\" >"
),
Some(Syntax::Cii)
);
}
#[test]
fn other_encodings() {
let ubl = "<?xml version='1.0' encoding='UTF-16'?><Invoice note='\u{e9}' xmlns='urn:oasis:names:specification:ubl:schema:xsd:Invoice-2'/>";
let le: Vec<u8> = [0xFF, 0xFE]
.into_iter()
.chain(ubl.encode_utf16().flat_map(u16::to_le_bytes))
.collect();
assert_eq!(Syntax::detect(&le), Some(Syntax::Ubl));
let be: Vec<u8> = ubl.encode_utf16().flat_map(u16::to_be_bytes).collect();
assert_eq!(Syntax::detect(&be), Some(Syntax::Ubl));
let mut lone = le.clone();
lone.extend([0x00, 0xD8]);
assert_eq!(Syntax::detect(&lone), Some(Syntax::Ubl));
let latin1 = b"<?xml version='1.0' encoding='ISO-8859-1'?><rsm:CrossIndustryInvoice a='\xE9' xmlns:rsm='urn:un:unece:uncefact:data:standard:CrossIndustryInvoice:100'/>";
assert_eq!(Syntax::detect(latin1), Some(Syntax::Cii));
}
#[test]
fn anything_else() {
for text in [
"",
" ",
"not xml",
"<Invoice/>",
r#"<Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:CreditNote-2"/>"#,
r#"<CreditNote xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2"/>"#,
r#"<x:Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2"/>"#,
r#"<rsm:CrossIndustryInvoice xmlns:rsm="urn:un:unece:uncefact:data:standard:CrossIndustryInvoice:13"/>"#,
r#"<!DOCTYPE Invoice><Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2"/>"#,
r#"<Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-21"/>"#,
r#"<Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2"x="1"/>"#,
"<!-- unterminated",
"<Invoice xmlns='urn:oasis:names:specification:ubl:schema:xsd:Invoice-2",
] {
assert_eq!(detect(text), None, "{text}");
}
}
#[test]
fn rule_set_names() {
assert_eq!(Syntax::Ubl.builtin_rule_set(), "cen-ubl");
assert_eq!(Syntax::Cii.builtin_rule_set(), "cen-cii");
assert_eq!(Syntax::Cii.to_string(), "cii");
}
}