use std::fmt;
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) = 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())
}
}
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)?;
let value = std::str::from_utf8(&after[..end]).ok()?;
rest = &after[end + 1..];
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 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");
}
}