feat(anatomy): R5.4 review files in Rust (CHANGELOG, REVIEW-REPORT, review overlays, app drawings) and the whole-draft --check

This commit is contained in:
bchanot committed 2026-10-23 11:21:49 +02:00
1 parent 43ce28a79b
commit fef21d67ef
17 files changed
+2103 -52

No files matched your search

+94 -32
View File
@@ -1,10 +1,12 @@
//! `import-equine` (R5.2, R5.3): the anatomical pipeline on the core. Reads the historical
//! `import-equine` (R5.2 to R5.4): the anatomical pipeline on the core. Reads the historical
//! equine files, the authored curation files and the frozen cleaned drawing of the repository,
//! and produces the two generated folders the TypeScript `pnpm import:equine` writes:
//! `legacy-import/` (complete) and `draft/` (its R5.3 files; the review drawings and the two
//! Markdown reports join at R5.4).
//! and produces the two generated folders the TypeScript `pnpm import:equine` writes, whole:
//! `legacy-import/` (6 files) and `draft/` (12 files).
//!
//! import-equine --check compare with the committed folders, write nothing
//! import-equine --check compare with the committed folders, write nothing: every file
//! byte for byte, except the application's drawings, which may
//! fall back on parsed trees under the declared R3 rule; a file
//! missing, not produced or different fails the check
//! import-equine --out <dir> write <dir>/legacy-import and <dir>/draft (<dir> absent or empty)
//!
//! The repository is found from this crate's manifest folder; a relative `--out` is typed from
@@ -19,6 +21,7 @@ use std::io::Write;
use std::path::{Component, Path, PathBuf};
use std::{env, fs, process};
use faunosteo_core::curation::draft::svg_compare::{TriggerNames, compare_drawing};
use faunosteo_core::curation::draft::{
DraftInput, build_equine_draft, parse_changeset, parse_references, parse_view_box,
render_draft_artifacts,
@@ -27,6 +30,7 @@ use faunosteo_core::curation::legacy::{
EquineLegacyInput, HEAD_VIEW_ID, ImportResult, SvgSource, import_equine_legacy,
render_artifacts,
};
use faunosteo_core::domain::sheet::AnatomicalSheetDocument;
const SEED: &str = concat!(
env!("CARGO_MANIFEST_DIR"),
@@ -37,16 +41,9 @@ const LEGACY_FOLDER: &str = "legacy-import";
const DRAFT_FOLDER: &str = "draft";
const CLEANED_ASSET: &str = "assets/horse.svg";
// shortcut: the draft files R5.4 will produce (review drawings, CHANGELOG, REVIEW-REPORT) are
// listed as pending by `--check` and not counted; R5.4 removes this list and compares them.
const PENDING_R5_4: [&str; 6] = [
"review/annotations-main.svg",
"review/annotations-head.svg",
"review/app-main.svg",
"review/app-head.svg",
"CHANGELOG.md",
"REVIEW-REPORT.md",
];
/// The files of the draft whose comparison may fall back on parsed trees under the declared
/// R3 trigger-name rule (`svg_compare`): the application's drawings of each view.
const APP_DRAWING_PREFIX: &str = "review/app-";
/// A file of a generated folder: its path relative to the folder and its bytes.
type FolderFile = (String, Vec<u8>);
@@ -55,6 +52,8 @@ type FolderFile = (String, Vec<u8>);
struct Folder {
name: &'static str,
files: Vec<FolderFile>,
/// The declared tree rule of the application's drawings (the draft only).
trigger_names: Option<TriggerNames>,
}
/// A bound no source or generated file approaches (the largest, the draft CHANGELOG, is about
@@ -122,6 +121,7 @@ fn legacy_folder(input: &EquineLegacyInput, result: &ImportResult) -> Result<Fol
Ok(Folder {
name: LEGACY_FOLDER,
files,
trigger_names: None,
})
}
@@ -143,11 +143,16 @@ fn draft_folder(result: &ImportResult) -> Result<Folder, String> {
changes: &changeset.changes,
})
.map_err(|error| error.to_string())?;
let mut files = texts(render_draft_artifacts(&draft).map_err(|error| error.to_string())?);
let rendered =
render_draft_artifacts(&draft, &result.anomalies).map_err(|error| error.to_string())?;
let typed: AnatomicalSheetDocument =
serde_json::from_value(draft.document.clone()).map_err(|error| error.to_string())?;
let mut files = texts(rendered);
files.push((CLEANED_ASSET.to_owned(), cleaned_svg));
Ok(Folder {
name: DRAFT_FOLDER,
files,
trigger_names: Some(TriggerNames::of(&typed)),
})
}
@@ -177,8 +182,41 @@ fn listed(dir: &Path, prefix: &str, out: &mut Vec<String>) -> Result<(), String>
Ok(())
}
/// The differences between a produced folder and the committed one; pending R5.4 files of the
/// draft are reported, not counted.
/// How one produced file compares with the committed one: byte identity, or, for the
/// application's drawings only, parsed trees under the declared R3 rule (printed as declared).
fn compare_file(
folder: &Folder,
name: &str,
produced: &[u8],
committed: &[u8],
) -> Result<(), String> {
if produced == committed {
return Ok(());
}
let rule = folder
.trigger_names
.as_ref()
.filter(|_| name.starts_with(APP_DRAWING_PREFIX));
let (Ok(produced), Ok(committed)) = (
std::str::from_utf8(produced),
std::str::from_utf8(committed),
) else {
return Err(format!("{}: differs: {name}", folder.name));
};
match compare_drawing(produced, committed, rule) {
Ok(_) => {
println!(
"{}: declared: {name} (trees equal under the R3 trigger-name rule)",
folder.name
);
Ok(())
}
Err(why) => Err(format!("{}: differs: {name} ({why})", folder.name)),
}
}
/// The differences between a produced folder and the committed one: every committed file must
/// be produced and every produced file committed and equal (`compare_file`).
fn check(folder: &Folder) -> Result<Vec<String>, String> {
let target = Path::new(SEED).join(folder.name);
// The generated folder itself must lie in the seed: a link there is never listed through.
@@ -190,8 +228,11 @@ fn check(folder: &Folder) -> Result<Vec<String>, String> {
let mut problems = Vec::new();
for (name, bytes) in &folder.files {
match read(&target.join(name)) {
Ok(committed) if committed == *bytes => {}
Ok(_) => problems.push(format!("{}: differs: {name}", folder.name)),
Ok(committed) => {
if let Err(problem) = compare_file(folder, name, bytes, &committed) {
problems.push(problem);
}
}
Err(_) => problems.push(format!("{}: missing: {name}", folder.name)),
}
}
@@ -201,10 +242,6 @@ fn check(folder: &Folder) -> Result<Vec<String>, String> {
if folder.files.iter().any(|(produced, _)| *produced == name) {
continue;
}
if folder.name == DRAFT_FOLDER && PENDING_R5_4.contains(&name.as_str()) {
println!("{}: pending R5.4 (not compared): {name}", folder.name);
continue;
}
problems.push(format!("{}: not produced: {name}", folder.name));
}
Ok(problems)
@@ -233,7 +270,7 @@ fn claim_destination(typed: &Path, seed: &Path) -> Result<PathBuf, String> {
}
None => return Err(refuse("not a destination folder")),
};
if inside(&parent, seed) || inside_by_identity(&parent, seed) {
if inside(&parent, seed) || inside_by_identity(&parent, seed)? {
return Err(refuse("inside the seed folder"));
}
let name = out
@@ -245,6 +282,10 @@ fn claim_destination(typed: &Path, seed: &Path) -> Result<PathBuf, String> {
Ok(metadata) if metadata.file_type().is_symlink() => return Err(refuse("a symbolic link")),
Ok(metadata) if !metadata.is_dir() => return Err(refuse("not a folder")),
Ok(_) => {
// An existing folder may itself be a second path to a folder of the seed.
if inside_by_identity(&root, seed)? {
return Err(refuse("inside the seed folder"));
}
let mut entries =
fs::read_dir(&root).map_err(|error| format!("{}: {error}", root.display()))?;
if entries.next().is_some() {
@@ -281,15 +322,18 @@ fn seed_identities(seed: &Path) -> Option<Vec<(u64, u64)>> {
/// Whether `folder` or one of its ancestors is the seed folder or a folder under it, by file
/// identity: a second path to any of them (a bind mount, a share mounted twice on the same
/// device) is caught too. A walk that fails counts as inside.
fn inside_by_identity(folder: &Path, seed: &Path) -> bool {
/// device) is caught too. A seed walk that fails is an error (the claim then refuses).
fn inside_by_identity(folder: &Path, seed: &Path) -> Result<bool, String> {
use std::os::unix::fs::MetadataExt;
let Some(identities) = seed_identities(seed) else {
return true;
};
folder.ancestors().any(|ancestor| {
let identities = seed_identities(seed).ok_or_else(|| {
format!(
"{}: the seed folder cannot be read, nothing written",
seed.display()
)
})?;
Ok(folder.ancestors().any(|ancestor| {
fs::metadata(ancestor).is_ok_and(|m| identities.contains(&(m.dev(), m.ino())))
})
}))
}
/// Where a relative destination is typed from: `INIT_CWD` when pnpm set it to an absolute
@@ -437,6 +481,24 @@ mod tests {
assert!(refused.contains("not empty"), "{refused}");
}
#[test]
fn a_difference_fails_the_check_unless_a_declared_rule_covers_it() {
let draft = Folder {
name: DRAFT_FOLDER,
files: Vec::new(),
trigger_names: None,
};
assert!(compare_file(&draft, "CHANGELOG.md", b"a\n", b"a\n").is_ok());
let differs = compare_file(&draft, "CHANGELOG.md", b"a\n", b"b\n")
.err()
.unwrap_or_default();
assert!(differs.contains("differs: CHANGELOG.md"), "{differs}");
// No rule for the folder: even an application drawing must be byte-identical.
let svg = b"<svg xmlns=\"http://www.w3.org/2000/svg\"/>";
let other = b"<svg xmlns=\"http://www.w3.org/2000/svg\"><g/></svg>";
assert!(compare_file(&draft, "review/app-main.svg", svg, other).is_err());
}
#[test]
fn a_folder_under_the_seed_or_the_seed_itself_is_inside_it() {
let seed = Path::new("/repo/supabase/seed/anatomical-sheets/equine");