feat(anatomy): R5.3 curated draft in Rust (changeset applier, migration map, stats, seed SQL) and pnpm r5:import-equine
crates/core/src/curation/draft behind the curation feature: strict changeset and references parsing as JSON.parse + Zod, the six ops, rename cascades, the B/C review rule, the R2 shape and integrity gate, migration map, document stats, seed SQL. import-equine writes both folders; --check reproduces legacy-import/ and the R5.3 draft files byte for byte (12 files, 0 differences), --out claims an absent or empty, non-symlink destination and never deletes. Parity: 825 records, 46 changes, stats, 612 leaves 442/141/29, 58 synthetic TypeScript cases (oracle r5/curation-synthetic.json).
This commit is contained in:
1 parent
5e114da2ce
commit
e0f149b28a
20 files changed
+4252
-58
No files matched your search
@@ -1,20 +1,29 @@
|
||||
//! `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` (R5.2, R5.3): 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).
|
||||
//!
|
||||
//! import-equine --check compare with the committed folder, write nothing
|
||||
//! import-equine --out <dir> write the folder into <dir> (absent or empty)
|
||||
//! import-equine --check compare with the committed folders, write nothing
|
||||
//! 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. 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.
|
||||
//! The repository is found from this crate's manifest folder; a relative `--out` is typed from
|
||||
//! the folder pnpm was run in (`INIT_CWD`), else the current one. Nothing is ever deleted:
|
||||
//! `--out` refuses an empty path, a symbolic link, a path that is not a folder and a folder
|
||||
//! holding any file, so the official seed can never be rewritten by this tool. 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::draft::{
|
||||
DraftInput, build_equine_draft, parse_changeset, parse_references, parse_view_box,
|
||||
render_draft_artifacts,
|
||||
};
|
||||
use faunosteo_core::curation::legacy::{
|
||||
EquineLegacyInput, SvgSource, import_equine_legacy, render_artifacts,
|
||||
EquineLegacyInput, HEAD_VIEW_ID, ImportResult, SvgSource, import_equine_legacy,
|
||||
render_artifacts,
|
||||
};
|
||||
|
||||
const SEED: &str = concat!(
|
||||
@@ -22,13 +31,40 @@ const SEED: &str = concat!(
|
||||
"/../../supabase/seed/anatomical-sheets/equine"
|
||||
);
|
||||
|
||||
/// A file of the folder: its name and its bytes.
|
||||
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",
|
||||
];
|
||||
|
||||
/// A file of a generated folder: its path relative to the folder and its bytes.
|
||||
type FolderFile = (String, Vec<u8>);
|
||||
|
||||
/// A generated folder: its name under the seed and its files.
|
||||
struct Folder {
|
||||
name: &'static str,
|
||||
files: Vec<FolderFile>,
|
||||
}
|
||||
|
||||
fn read(path: &Path) -> Result<Vec<u8>, String> {
|
||||
fs::read(path).map_err(|error| format!("{}: {error}", path.display()))
|
||||
}
|
||||
|
||||
/// `readFile(path, 'utf8')`: invalid UTF-8 becomes U+FFFD, as the TypeScript read the
|
||||
/// curation files.
|
||||
fn read_text(path: &Path) -> Result<String, String> {
|
||||
Ok(String::from_utf8_lossy(&read(path)?).into_owned())
|
||||
}
|
||||
|
||||
fn svg(name: &str) -> Result<SvgSource, String> {
|
||||
let bytes = read(&Path::new(SEED).join("legacy-sources").join(name))?;
|
||||
Ok(SvgSource {
|
||||
@@ -37,7 +73,7 @@ fn svg(name: &str) -> Result<SvgSource, String> {
|
||||
})
|
||||
}
|
||||
|
||||
fn input() -> Result<EquineLegacyInput, String> {
|
||||
fn legacy_input() -> Result<EquineLegacyInput, String> {
|
||||
let sources = Path::new(SEED).join("legacy-sources");
|
||||
Ok(EquineLegacyInput {
|
||||
main_svg: svg("Horse.svg")?,
|
||||
@@ -47,19 +83,58 @@ fn input() -> Result<EquineLegacyInput, String> {
|
||||
})
|
||||
}
|
||||
|
||||
/// 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())?
|
||||
fn texts(files: Vec<(String, String)>) -> Vec<FolderFile> {
|
||||
files
|
||||
.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));
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// `legacy-import/`: the four texts, then the two SVG copies.
|
||||
fn legacy_folder(input: &EquineLegacyInput, result: &ImportResult) -> Result<Folder, String> {
|
||||
let mut files = texts(render_artifacts(result).map_err(|error| error.to_string())?);
|
||||
for source in [&input.main_svg, &input.head_svg] {
|
||||
files.push((format!("assets/{}", source.file_name), source.bytes.clone()));
|
||||
}
|
||||
Ok(files)
|
||||
Ok(Folder {
|
||||
name: LEGACY_FOLDER,
|
||||
files,
|
||||
})
|
||||
}
|
||||
|
||||
/// `draft/`: the R5.3 texts, then the frozen cleaned drawing (read, never re-cleaned).
|
||||
fn draft_folder(result: &ImportResult) -> Result<Folder, String> {
|
||||
let curation = Path::new(SEED).join("curation");
|
||||
let references = parse_references(&read_text(&curation.join("references.json"))?)
|
||||
.map_err(|error| error.to_string())?;
|
||||
let changeset = parse_changeset(&read_text(&curation.join("changes.json"))?)
|
||||
.map_err(|error| error.to_string())?;
|
||||
let cleaned_svg = read(&Path::new(SEED).join(DRAFT_FOLDER).join(CLEANED_ASSET))?;
|
||||
let head = result.view_boxes.get(HEAD_VIEW_ID).and_then(|v| v.as_str());
|
||||
let head_view_box = parse_view_box(head).map_err(|error| error.to_string())?;
|
||||
let draft = build_equine_draft(&DraftInput {
|
||||
import_result: result,
|
||||
cleaned_svg: &cleaned_svg,
|
||||
head_view_box,
|
||||
references: &references,
|
||||
changes: &changeset.changes,
|
||||
})
|
||||
.map_err(|error| error.to_string())?;
|
||||
let mut files = texts(render_draft_artifacts(&draft).map_err(|error| error.to_string())?);
|
||||
files.push((CLEANED_ASSET.to_owned(), cleaned_svg));
|
||||
Ok(Folder {
|
||||
name: DRAFT_FOLDER,
|
||||
files,
|
||||
})
|
||||
}
|
||||
|
||||
fn folders() -> Result<Vec<Folder>, String> {
|
||||
let input = legacy_input()?;
|
||||
let result = import_equine_legacy(&input).map_err(|error| error.to_string())?;
|
||||
Ok(vec![
|
||||
legacy_folder(&input, &result)?,
|
||||
draft_folder(&result)?,
|
||||
])
|
||||
}
|
||||
|
||||
/// Relative paths of every file under `dir`.
|
||||
@@ -77,63 +152,126 @@ fn listed(dir: &Path, prefix: &str, out: &mut Vec<String>) -> Result<(), String>
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn check(files: &[FolderFile]) -> Result<Vec<String>, String> {
|
||||
let target = Path::new(SEED).join("legacy-import");
|
||||
/// The differences between a produced folder and the committed one; pending R5.4 files of the
|
||||
/// draft are reported, not counted.
|
||||
fn check(folder: &Folder) -> Result<Vec<String>, String> {
|
||||
let target = Path::new(SEED).join(folder.name);
|
||||
let mut problems = Vec::new();
|
||||
for (name, bytes) in files {
|
||||
for (name, bytes) in &folder.files {
|
||||
match fs::read(target.join(name)) {
|
||||
Ok(committed) if committed == *bytes => {}
|
||||
Ok(_) => problems.push(format!("differs: {name}")),
|
||||
Err(_) => problems.push(format!("missing: {name}")),
|
||||
Ok(_) => problems.push(format!("{}: differs: {name}", folder.name)),
|
||||
Err(_) => problems.push(format!("{}: missing: {name}", folder.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}"));
|
||||
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)
|
||||
}
|
||||
|
||||
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()));
|
||||
/// Claims a destination this tool may fill and returns its canonical path: a non-empty path
|
||||
/// that is absent (created here, so nothing can appear between the check and the writes) or
|
||||
/// an empty folder that is not a symbolic link. The canonical path is where the files land,
|
||||
/// symbolic links of the parent folders resolved, and is what the success line prints.
|
||||
fn claim_destination(typed: &Path) -> Result<PathBuf, String> {
|
||||
if typed.as_os_str().is_empty() {
|
||||
return Err("--out needs a destination path".to_owned());
|
||||
}
|
||||
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())?;
|
||||
// Rebuilt from its components: a trailing `/` would make the system resolve a final
|
||||
// symbolic link before the check below could see it.
|
||||
let out: PathBuf = typed.components().collect();
|
||||
let out = out.as_path();
|
||||
match fs::symlink_metadata(out) {
|
||||
Err(_) => {
|
||||
if let Some(parent) = out.parent().filter(|p| !p.as_os_str().is_empty()) {
|
||||
fs::create_dir_all(parent).map_err(|e| format!("{}: {e}", parent.display()))?;
|
||||
}
|
||||
fs::create_dir(out).map_err(|e| format!("{}: {e}, nothing written", out.display()))?;
|
||||
}
|
||||
Ok(metadata) if metadata.file_type().is_symlink() => {
|
||||
return Err(format!(
|
||||
"{}: a symbolic link, nothing written",
|
||||
out.display()
|
||||
));
|
||||
}
|
||||
Ok(metadata) if !metadata.is_dir() => {
|
||||
return Err(format!("{}: not a folder, nothing written", out.display()));
|
||||
}
|
||||
Ok(_) => {
|
||||
let mut entries =
|
||||
fs::read_dir(out).map_err(|error| format!("{}: {error}", out.display()))?;
|
||||
if entries.next().is_some() {
|
||||
return Err(format!("{}: not empty, nothing written", out.display()));
|
||||
}
|
||||
}
|
||||
fs::write(&path, bytes).map_err(|error| format!("{}: {error}", path.display()))?;
|
||||
}
|
||||
Ok(())
|
||||
fs::canonicalize(out).map_err(|error| format!("{}: {error}", out.display()))
|
||||
}
|
||||
|
||||
/// Where a relative destination is typed from: `INIT_CWD` when pnpm set it to an absolute
|
||||
/// path, else the current folder.
|
||||
fn base_dir() -> PathBuf {
|
||||
env::var_os("INIT_CWD")
|
||||
.map(PathBuf::from)
|
||||
.filter(|path| path.is_absolute())
|
||||
.or_else(|| env::current_dir().ok())
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
/// Writes every file under the claimed destination; returns it and the file count.
|
||||
fn write(folders: &[Folder], out: &Path) -> Result<(PathBuf, usize), String> {
|
||||
let root = claim_destination(out)?;
|
||||
let mut written = 0;
|
||||
for folder in folders {
|
||||
for (name, bytes) in &folder.files {
|
||||
let path = root.join(folder.name).join(name);
|
||||
if let Some(parent) = path.parent() {
|
||||
fs::create_dir_all(parent)
|
||||
.map_err(|error| format!("{}: {error}", parent.display()))?;
|
||||
}
|
||||
fs::write(&path, bytes).map_err(|error| format!("{}: {error}", path.display()))?;
|
||||
written += 1;
|
||||
}
|
||||
}
|
||||
Ok((root, written))
|
||||
}
|
||||
|
||||
fn run(args: &[String]) -> Result<i32, String> {
|
||||
let files = folder()?;
|
||||
let folders = folders()?;
|
||||
match args {
|
||||
[flag] if flag == "--check" => {
|
||||
let problems = check(&files)?;
|
||||
for problem in &problems {
|
||||
eprintln!("{problem}");
|
||||
let mut total = 0;
|
||||
for folder in &folders {
|
||||
let problems = check(folder)?;
|
||||
for problem in &problems {
|
||||
eprintln!("{problem}");
|
||||
}
|
||||
println!(
|
||||
"{}: {} files, {} differences",
|
||||
folder.name,
|
||||
folder.files.len(),
|
||||
problems.len()
|
||||
);
|
||||
total += problems.len();
|
||||
}
|
||||
println!(
|
||||
"legacy-import: {} files, {} differences",
|
||||
files.len(),
|
||||
problems.len()
|
||||
);
|
||||
Ok(i32::from(!problems.is_empty()))
|
||||
Ok(i32::from(total != 0))
|
||||
}
|
||||
[flag, dir] if flag == "--out" => {
|
||||
write(&files, &PathBuf::from(dir))?;
|
||||
println!("legacy-import: {} files written to {dir}", files.len());
|
||||
if dir.is_empty() {
|
||||
return Err("--out needs a destination path".to_owned());
|
||||
}
|
||||
let (root, written) = write(&folders, &base_dir().join(dir))?;
|
||||
println!("{written} files written to {}", root.display());
|
||||
Ok(0)
|
||||
}
|
||||
_ => Err("usage: import-equine --check | --out <dir>".to_owned()),
|
||||
|
||||
Reference in new issue
Block a user