use super::archive::{Tar, Zip, gunzip};
use super::assemble::Files;
use super::{FetchOptions, MAX_TAR, check_sha256};
use crate::Error;
use crate::rulesets::catalogue::{FamilySpec, FetchedRuleSet, PEPPOL_STYLESHEETS};
fn fetch_error(message: impl Into<String>) -> Error {
Error::Fetch {
message: message.into(),
}
}
#[derive(Debug)]
pub(crate) struct Schematron {
files: Vec<(&'static str, Vec<u8>)>,
directory: Vec<(String, Vec<u8>)>,
}
const SCH_DIR: &str = "rules/sch/";
fn stated_version(line: &str) -> Option<&str> {
const KEY: &str = "Schematron version ";
let mut positions: Vec<usize> = line.match_indices(KEY).map(|(i, _)| i).collect();
positions.reverse();
positions.into_iter().find_map(|i| {
let rest = &line[i + KEY.len()..];
let run = rest
.find(|c: char| !(c.is_ascii_digit() || c == '.'))
.unwrap_or(rest.len());
let version = &rest[..run];
(version.len() >= 2
&& version.starts_with(|c: char| c.is_ascii_digit())
&& version.ends_with(|c: char| c.is_ascii_digit())
&& rest[run..].starts_with(' '))
.then_some(version)
})
}
pub(crate) fn schematron(family: &FamilySpec, bytes: &[u8]) -> Result<Schematron, Error> {
let pin = |name: &str| family.pins.get(name).map_err(Error::internal);
let (tag, commit, cen) = (pin("tag")?, pin("commit")?, pin("cen")?);
let tar = gunzip(bytes, MAX_TAR).map_err(|e| fetch_error(format!("Peppol {tag}: {e}")))?;
let archive = Tar::open(&tar).map_err(|e| fetch_error(format!("Peppol {tag}: {e}")))?;
match archive.commit_comment.as_deref() {
Some(recorded) if recorded == commit => {}
Some(recorded) => {
return Err(fetch_error(format!(
"the Peppol {tag} archive is commit {recorded}, expected {commit}"
)));
}
None => {
return Err(fetch_error(format!(
"the Peppol {tag} archive records no commit id"
)));
}
}
let mut files = Vec::new();
for (name, _) in PEPPOL_STYLESHEETS {
let path = format!("{SCH_DIR}{name}.sch");
let content = archive
.stripped(&path)
.ok_or_else(|| fetch_error(format!("the Peppol {tag} archive has no {path}")))?;
files.push((name, content.to_vec()));
}
let cen_sch = files
.iter()
.find(|(n, _)| *n == "CEN-EN16931-UBL")
.map(|(_, c)| String::from_utf8_lossy(c).into_owned())
.unwrap_or_default();
let stated = cen_sch.lines().find_map(stated_version);
match stated {
Some(v) if v == cen => {}
Some(v) => {
return Err(fetch_error(format!(
"rules/sch/CEN-EN16931-UBL.sch states Schematron version {v}, expected {cen}"
)));
}
None => {
return Err(fetch_error(
"rules/sch/CEN-EN16931-UBL.sch states no Schematron version",
));
}
}
let directory = archive
.stripped_under(SCH_DIR)
.into_iter()
.map(|(path, content)| (path.to_owned(), content.to_vec()))
.collect();
Ok(Schematron { files, directory })
}
pub(crate) fn skeleton(family: &FamilySpec, bytes: &[u8]) -> Result<Vec<(String, String)>, Error> {
let plugin = family.pins.get("plugin").map_err(Error::internal)?;
let jar_error = |e: String| fetch_error(format!("ph-schematron-isosch {plugin}: {e}"));
let zip = Zip::open(bytes).map_err(jar_error)?;
let mut files = Vec::new();
for (name, _) in einvoice_schematron::SKELETON_FILES {
let path = format!("{}{name}", einvoice_schematron::SKELETON_DIR);
let entry = zip
.entry(&path)
.ok_or_else(|| jar_error(format!("no {path}")))?;
let content = zip.read(entry, MAX_TAR).map_err(jar_error)?;
let text =
String::from_utf8(content).map_err(|_| jar_error(format!("{path} is not UTF-8")))?;
files.push((name.to_owned(), text));
}
Ok(files)
}
fn archive_uri(path: &str) -> String {
format!("archive:/{path}")
}
pub(crate) fn build(
family: &FamilySpec,
members: &[&FetchedRuleSet],
schematron: &Schematron,
skeleton: &[(String, String)],
options: &FetchOptions,
) -> Result<Files, Error> {
let pin = |name: &str| family.pins.get(name).map_err(Error::internal);
let plugin = pin("plugin")?;
let compile_error = |name: &str, e: einvoice_schematron::Error| {
fetch_error(format!(
"compiling {name}.sch as ph-schematron {plugin} does: {e}"
))
};
let skeleton = einvoice_schematron::Skeleton::new(skeleton)
.map_err(|e| fetch_error(format!("ph-schematron-isosch {plugin}: {e}")))?;
let mut sources = einvoice_schematron::Sources::new();
for (path, content) in &schematron.directory {
if let Ok(text) = std::str::from_utf8(content) {
sources = sources.with(archive_uri(path), text);
}
}
options.say(&format!(
"Peppol BIS Billing: compiling the Schematron as ph-schematron {plugin} does (in Rust)"
));
let mut files = Files::new();
for (name, sha) in PEPPOL_STYLESHEETS {
let content = schematron
.files
.iter()
.find(|(n, _)| *n == name)
.map(|(_, c)| c)
.ok_or_else(|| Error::internal(format!("peppol: no {name}.sch")))?;
let text = std::str::from_utf8(content)
.map_err(|_| fetch_error(format!("{SCH_DIR}{name}.sch is not UTF-8")))?;
let compilation = skeleton
.compile(
text,
&archive_uri(&format!("{SCH_DIR}{name}.sch")),
&sources,
)
.map_err(|e| compile_error(name, e))?;
for message in &compilation.messages {
options.say(&format!("{name}.sch: {message}"));
}
let bytes = compilation.stylesheet.into_bytes();
check_sha256(&format!("the compiled {name}.xslt"), &bytes, pin(sha)?)?;
files.push((format!("xslt/{name}.xslt"), bytes));
}
let manifest = members
.first()
.map(|r| r.manifest().as_bytes().to_vec())
.ok_or_else(|| Error::internal("peppol: no rule set"))?;
files.push(("bundle.toml".to_owned(), manifest));
Ok(files)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn reads_the_stated_version_as_sed_does() {
assert_eq!(
stated_version("<!--Schematron version 1.3.16 - Last update: 2025-->"),
Some("1.3.16")
);
assert_eq!(
stated_version("Schematron version 1.2 and Schematron version 1.3.4 x"),
Some("1.3.4")
);
assert_eq!(stated_version("Schematron version 1.3.16"), None);
assert_eq!(stated_version("Schematron version 1.3. x"), None);
assert_eq!(stated_version("Schematron version 1 x"), None);
assert_eq!(
stated_version("Schematron version 1.3 - Schematron version x "),
Some("1.3")
);
}
}