feat(anatomy): R5.2 legacy importer in Rust (curation feature, import-equine --check/--out), Prettier JSON and table layout, JS semantics; parity oracles frozen from the TypeScript

This commit is contained in:
bchanot committed 2026-10-22 21:14:45 +02:00
1 parent aed6b1e300
commit 03bdb5eaa0
32 files changed
+4248

No files matched your search

+152
View File
@@ -0,0 +1,152 @@
//! `import-equine` (R5.2): the legacy importer on the core. Reads the historical equine files
//! of the repository and produces the `legacy-import/` folder the TypeScript
//! `pnpm import:equine` generates (the draft folder joins at R5.3).
//!
//! import-equine --check compare with the committed folder, write nothing
//! import-equine --out <dir> write the folder into <dir> (absent or empty)
//!
//! The repository is found from this crate's manifest folder. Nothing is deleted: `--out`
//! refuses a folder that already holds files. Like `report-compare`, this file is the only
//! one of its tool allowed the file system.
#![allow(clippy::disallowed_methods)]
use std::path::{Path, PathBuf};
use std::{env, fs, process};
use faunosteo_core::curation::legacy::{
EquineLegacyInput, SvgSource, import_equine_legacy, render_artifacts,
};
const SEED: &str = concat!(
env!("CARGO_MANIFEST_DIR"),
"/../../supabase/seed/anatomical-sheets/equine"
);
/// A file of the folder: its name and its bytes.
type FolderFile = (String, Vec<u8>);
fn read(path: &Path) -> Result<Vec<u8>, String> {
fs::read(path).map_err(|error| format!("{}: {error}", path.display()))
}
fn svg(name: &str) -> Result<SvgSource, String> {
let bytes = read(&Path::new(SEED).join("legacy-sources").join(name))?;
Ok(SvgSource {
file_name: name.to_owned(),
bytes,
})
}
fn input() -> Result<EquineLegacyInput, String> {
let sources = Path::new(SEED).join("legacy-sources");
Ok(EquineLegacyInput {
main_svg: svg("Horse.svg")?,
head_svg: svg("Horse_head.svg")?,
catalog: read(&sources.join("pathologies.json"))?,
vertebrae: read(&sources.join("vertebrae.ts"))?,
})
}
/// Every file of `legacy-import/`: the four texts, then the two SVG copies.
fn folder() -> Result<Vec<FolderFile>, String> {
let input = input()?;
let result = import_equine_legacy(&input).map_err(|error| error.to_string())?;
let mut files: Vec<FolderFile> = render_artifacts(&result)
.map_err(|error| error.to_string())?
.into_iter()
.map(|(name, text)| (name, text.into_bytes()))
.collect();
for source in [input.main_svg, input.head_svg] {
files.push((format!("assets/{}", source.file_name), source.bytes));
}
Ok(files)
}
/// Relative paths of every file under `dir`.
fn listed(dir: &Path, prefix: &str, out: &mut Vec<String>) -> Result<(), String> {
let entries = fs::read_dir(dir).map_err(|error| format!("{}: {error}", dir.display()))?;
for entry in entries {
let entry = entry.map_err(|error| error.to_string())?;
let name = format!("{prefix}{}", entry.file_name().to_string_lossy());
if entry.path().is_dir() {
listed(&entry.path(), &format!("{name}/"), out)?;
} else {
out.push(name);
}
}
Ok(())
}
fn check(files: &[FolderFile]) -> Result<Vec<String>, String> {
let target = Path::new(SEED).join("legacy-import");
let mut problems = Vec::new();
for (name, bytes) in files {
match fs::read(target.join(name)) {
Ok(committed) if committed == *bytes => {}
Ok(_) => problems.push(format!("differs: {name}")),
Err(_) => problems.push(format!("missing: {name}")),
}
}
let mut present = Vec::new();
listed(&target, "", &mut present)?;
for name in present {
if !files.iter().any(|(produced, _)| *produced == name) {
problems.push(format!("not produced: {name}"));
}
}
Ok(problems)
}
fn write(files: &[FolderFile], out: &Path) -> Result<(), String> {
if out.exists()
&& fs::read_dir(out)
.map_err(|e| e.to_string())?
.next()
.is_some()
{
return Err(format!("{}: not empty, nothing written", out.display()));
}
for (name, bytes) in files {
let path = out.join(name);
if let Some(parent) = path.parent() {
fs::create_dir_all(parent).map_err(|error| error.to_string())?;
}
fs::write(&path, bytes).map_err(|error| format!("{}: {error}", path.display()))?;
}
Ok(())
}
fn run(args: &[String]) -> Result<i32, String> {
let files = folder()?;
match args {
[flag] if flag == "--check" => {
let problems = check(&files)?;
for problem in &problems {
eprintln!("{problem}");
}
println!(
"legacy-import: {} files, {} differences",
files.len(),
problems.len()
);
Ok(i32::from(!problems.is_empty()))
}
[flag, dir] if flag == "--out" => {
write(&files, &PathBuf::from(dir))?;
println!("legacy-import: {} files written to {dir}", files.len());
Ok(0)
}
_ => Err("usage: import-equine --check | --out <dir>".to_owned()),
}
}
fn main() {
let args: Vec<String> = env::args().skip(1).collect();
match run(&args) {
Ok(code) => process::exit(code),
Err(message) => {
eprintln!("{message}");
process::exit(2);
}
}
}