#![allow(dead_code)]
pub mod svg;
use std::path::Path;
pub fn output_path() -> Option<String> {
std::env::args().nth(1)
}
pub fn write(path: &str, contents: &str) {
if let Some(parent) = Path::new(path).parent() {
if !parent.as_os_str().is_empty() {
std::fs::create_dir_all(parent).expect("could not create the output directory");
}
}
std::fs::write(path, contents).unwrap_or_else(|e| panic!("could not write {path}: {e}"));
println!("wrote {path}");
}
pub fn check(label: &str, value: f64, expected: f64, rel_tol: f64, unit: &str) {
let scale = expected.abs().max(value.abs()).max(f64::MIN_POSITIVE);
let error = (value - expected).abs() / scale;
println!(" {label:<44} {value:>12.4} {unit:<8} (expected {expected:.4}, off by {error:.2e})");
assert!(
error <= rel_tol,
"{label}: got {value} {unit}, expected {expected} — relative error {error:.3e} exceeds {rel_tol:.3e}"
);
}
pub fn check_zero(label: &str, value: f64, scale: f64, rel_tol: f64, unit: &str) {
let error = value.abs() / scale.abs().max(f64::MIN_POSITIVE);
println!(" {label:<44} {value:>12.3e} {unit:<8} (against a scale of {scale:.4}, off by {error:.2e})");
assert!(
error <= rel_tol,
"{label}: {value} {unit} against a scale of {scale} — relative {error:.3e} exceeds {rel_tol:.3e}"
);
}
pub fn check_between(label: &str, value: f64, lo: f64, hi: f64, unit: &str) {
println!(" {label:<44} {value:>12.4} {unit:<8} (between {lo} and {hi})");
assert!(
value >= lo && value <= hi,
"{label}: {value} {unit} is outside [{lo}, {hi}]"
);
}
pub fn heading(s: &str) {
println!("\n{s}");
println!("{}", "-".repeat(s.len()));
}