#![allow(dead_code)]
use std::path::{Path, PathBuf};
use std::process::Command;
use std::sync::OnceLock;
use tabnas::{Tabnas, Value};
use tabnas_support::{find_spec_dir, Failure};
pub fn repo_root() -> PathBuf {
Path::new(env!("CARGO_MANIFEST_DIR"))
.parent()
.expect("rs/ has a parent")
.to_path_buf()
}
pub fn conf_root() -> PathBuf {
repo_root().join("test").join("xmlconf")
}
pub fn catalog_path() -> PathBuf {
conf_root().join("xmlconf.xml")
}
pub fn corpus() -> &'static Path {
static ROOT: OnceLock<PathBuf> = OnceLock::new();
ROOT.get_or_init(|| {
if !catalog_path().is_file() {
let script = repo_root().join("scripts").join("fetch-xml-suite.sh");
eprintln!(
"W3C XML conformance corpus missing; running {}",
script.display()
);
let status = Command::new("bash")
.arg(&script)
.current_dir(repo_root())
.status();
match status {
Ok(status) if status.success() => {}
other => panic!(
"\nFATAL: the W3C XML Conformance Test Suite is missing and could not be fetched.\n \
expected catalogue: {}\n fetch: {other:?}\n \
fix: run scripts/fetch-xml-suite.sh (needs network access to w3.org)\n \
these tests do NOT skip.\n",
catalog_path().display()
),
}
}
assert!(
catalog_path().is_file(),
"FATAL: corpus still missing after fetch: {}",
catalog_path().display()
);
conf_root()
})
}
pub fn spec_dir() -> PathBuf {
find_spec_dir(Some(Path::new(env!("CARGO_MANIFEST_DIR"))))
.expect("a test/spec directory above rs/")
}
pub fn to_value(value: &Value) -> tabnas_support::Value {
tabnas_support::Value::from(value.to_json())
}
pub fn to_failure(error: tabnas::TabnasError) -> Failure {
Failure::new(error.code.clone())
.at(error.row, error.col)
.with_message(error.to_string())
}
pub fn default_parser() -> Tabnas {
tabnas_xml::make()
}
pub fn strip_ansi(text: &str) -> String {
let mut out = String::with_capacity(text.len());
let mut chars = text.chars().peekable();
while let Some(ch) = chars.next() {
if ch == '\u{1b}' && chars.peek() == Some(&'[') {
chars.next();
for next in chars.by_ref() {
if next == 'm' {
break;
}
}
continue;
}
out.push(ch);
}
out
}
pub fn spec_unescape(s: &str) -> String {
if !s.contains('\\') {
return s.to_string();
}
let chars: Vec<char> = s.chars().collect();
let mut out = String::with_capacity(s.len());
let mut i = 0;
while i < chars.len() {
let c = chars[i];
if c == '\\' && i + 1 < chars.len() {
match chars[i + 1] {
'n' => {
out.push('\n');
i += 2;
continue;
}
'r' => {
out.push('\r');
i += 2;
continue;
}
't' => {
out.push('\t');
i += 2;
continue;
}
'\\' => {
out.push('\\');
i += 2;
continue;
}
'u' if i + 5 < chars.len() => {
let hex: String = chars[i + 2..i + 6].iter().collect();
if hex.chars().all(|h| h.is_ascii_hexdigit()) {
if let Some(decoded) =
u32::from_str_radix(&hex, 16).ok().and_then(char::from_u32)
{
out.push(decoded);
i += 6;
continue;
}
}
}
_ => {}
}
}
out.push(c);
i += 1;
}
out
}
pub fn json(value: &Value) -> String {
value.to_json().to_string()
}