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:
bchanot committed 2026-10-23 00:10:55 +02:00
1 parent 5e114da2ce
commit e0f149b28a
20 files changed
+4252 -58

No files matched your search

+4
View File
@@ -118,3 +118,7 @@ required-features = ["curation"]
[[test]]
name = "curation_legacy"
required-features = ["curation"]
[[test]]
name = "curation_draft"
required-features = ["curation"]
+192 -54
View File
@@ -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()),
+166
View File
@@ -0,0 +1,166 @@
//! Applies a changeset on a baseline document (`packages/anatomy/src/curation/apply.ts`),
//! deterministically and with a record per elementary edit. The references file replaces the
//! document registry first (it is the source of truth). Any op that cannot apply exactly as
//! written, and any integrity error left at the end, refuses: a curated document is never
//! produced from a partially applied changeset.
//!
//! Review rule: a level B or C change never leaves what it touched (findings, finding
//! categories, annotations: every item a set, add, cite, rename or move resolved to, whole
//! added subtrees and subtree scopes included) as inherited or reviewed. After its ops, the
//! applier sets `review_status = 'to_confirm'` on each one and records that edit under the
//! same change id. An explicit 'reviewed' in such a change refuses. Level A is untouched by
//! the rule.
use serde_json::Value;
use super::ops::{apply_op, assert_declared};
use super::records::{ChangeRecord, CurationResult, Recorder, strip_marks, touch_order};
use super::schema::{Change, ChangeSummary, Op, SetOp};
use super::targets::{
Path, Step, TargetKind, all_findings, at, fail, item_at_mut, list_at, top_collection_path,
};
use crate::curation::PipelineError;
use crate::domain::sheet::{
AnatomicalSheetDocument, Reference, SheetIssue, SheetIssueSeverity, validate_sheet_document,
};
/// A finding or an annotation a change touched: its touch order, its path, its target.
struct Touched {
order: u64,
path: Path,
target: String,
}
/// Findings and annotations of the document the change touched, in first-touch order.
fn touched_reviewable(doc: &Value) -> Result<Vec<Touched>, PipelineError> {
let mut candidates: Vec<(Path, String)> = all_findings(doc)?
.into_iter()
.map(|entry| (entry.path, entry.target))
.collect();
let annotations = top_collection_path(TargetKind::Annotation);
for (index, annotation) in list_at(doc, &annotations)?.iter().enumerate() {
let mut path = annotations.clone();
path.push(Step::Index(index));
let id = super::records::js_string(annotation.get("id"));
candidates.push((path, format!("annotation:{id}")));
}
let mut touched = Vec::new();
for (path, target) in candidates {
let Some(order) = at(doc, &path)?.as_object().and_then(touch_order) else {
continue;
};
touched.push(Touched {
order,
path,
target,
});
}
touched.sort_by_key(|item| item.order);
Ok(touched)
}
/// Marks every touched item still in the document 'to_confirm'.
fn mark_to_confirm(
doc: &mut Value,
change: &ChangeSummary,
records: &mut Vec<ChangeRecord>,
) -> Result<(), PipelineError> {
for touched in touched_reviewable(doc)? {
let item = item_at_mut(doc, &touched.path)?;
let status = item.get("review_status").cloned();
if status == Some(Value::from("to_confirm")) {
continue;
}
let before = match status {
None | Some(Value::Null) => Value::from("inherited"),
Some(value) => value,
};
item.insert("review_status".to_owned(), Value::from("to_confirm"));
let op = Op::Set(SetOp {
target: touched.target.clone(),
field: "review_status".to_owned(),
value: Value::from("to_confirm"),
scope: None,
});
records.push(ChangeRecord {
change: change.clone(),
op,
target: touched.target,
field: Some("review_status".to_owned()),
before,
after: Value::from("to_confirm"),
});
}
Ok(())
}
fn apply_change(
doc: &mut Value,
change: &Change,
records: &mut Vec<ChangeRecord>,
) -> Result<(), PipelineError> {
let summary = change.summary();
assert_declared(doc, &change.reference_ids)
.map_err(|error| fail(format!("{}: {error}", change.id)))?;
let mut next_touch = 0;
for (index, op) in change.ops.iter().enumerate() {
let mut recorder = Recorder::new(&summary, op, records, &mut next_touch);
apply_op(doc, &mut recorder)
.map_err(|error| fail(format!("{} op {index}: {error}", change.id)))?;
}
if change.level.needs_confirmation() {
mark_to_confirm(doc, &summary, records)?;
}
strip_marks(doc);
Ok(())
}
fn describe(issues: &[SheetIssue]) -> String {
issues
.iter()
.map(|issue| format!("{}: {}", issue.path, issue.message))
.collect::<Vec<_>>()
.join("; ")
}
/// Shape (the strict R2 contract) then integrity check of a curated document. Refuses on any
/// shape issue or integrity error; returns the warnings, which are allowed.
pub fn curated_issues(doc: &Value) -> Result<Vec<SheetIssue>, PipelineError> {
let parsed: AnatomicalSheetDocument = serde_json::from_value(doc.clone())
.map_err(|error| fail(format!("curated document is not shape-valid: {error}")))?;
let issues = validate_sheet_document(&parsed);
let errors: Vec<SheetIssue> = issues
.iter()
.filter(|issue| issue.severity == SheetIssueSeverity::Error)
.cloned()
.collect();
if !errors.is_empty() {
return Err(fail(format!(
"curated document has integrity errors: {}",
describe(&errors)
)));
}
Ok(issues)
}
pub fn apply_changeset(
baseline: &Value,
references: &[Reference],
changes: &[Change],
) -> Result<CurationResult, PipelineError> {
let mut doc = baseline.clone();
let registry =
serde_json::to_value(references).map_err(|error| fail(format!("references: {error}")))?;
doc.as_object_mut()
.ok_or_else(|| fail("the baseline is not an object"))?
.insert("references".to_owned(), registry);
let mut records = Vec::new();
for change in changes {
apply_change(&mut doc, change, &mut records)?;
}
curated_issues(&doc)?;
Ok(CurationResult {
document: doc,
records,
})
}
+126
View File
@@ -0,0 +1,126 @@
//! Fields a `set` may write, per kind of item (`packages/anatomy/src/curation/fields.ts`). Ids
//! are left out on purpose: an id changes through `rename`, which also rewrites every
//! reference to it. Unlisted field → the op fails instead of writing a stray key.
use serde_json::{Map, Value};
use super::targets::{Item, ParsedTarget, TargetKind, fail};
use crate::curation::PipelineError;
/// Item kind of the settable table; a finding is a category or a node.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FieldKind {
Document,
Region,
Reference,
Category,
View,
Annotation,
Zone,
FindingCategory,
FindingNode,
}
impl FieldKind {
pub fn of(target: &ParsedTarget, is_category: Option<bool>) -> Self {
match target.kind {
TargetKind::Document => Self::Document,
TargetKind::Region => Self::Region,
TargetKind::Reference => Self::Reference,
TargetKind::Category => Self::Category,
TargetKind::View => Self::View,
TargetKind::Annotation => Self::Annotation,
TargetKind::Zone => Self::Zone,
TargetKind::Finding => {
if is_category.unwrap_or(target.ids.len() == 2) {
Self::FindingCategory
} else {
Self::FindingNode
}
}
}
}
fn settable(self) -> &'static [&'static str] {
match self {
Self::Document => &["name", "description", "scope_disclaimer", "attribution"],
Self::Region => &["name", "count", "count_range"],
Self::Reference => &["type", "citation", "url", "doi"],
Self::Category => &["name", "color"],
Self::View => &["name", "svg_asset_id", "view_box", "parent_view_id"],
Self::Annotation => &[
"name",
"kind",
"view_id",
"category_id",
"laterality",
"geometry",
"review_status",
"finding_zone_id",
"vertebral_region_code",
"opens_view_id",
"color",
],
Self::Zone => &["name"],
Self::FindingCategory => &[
"kind",
"name",
"description",
"reference_ids",
"review_status",
],
Self::FindingNode => &[
"name",
"description",
"requires_vet_referral",
"reference_ids",
"review_status",
],
}
}
}
pub fn split_field(field: &str) -> Result<Vec<&str>, PipelineError> {
let path: Vec<&str> = field.split('.').collect();
if path.iter().any(|key| key.is_empty()) {
return Err(fail(format!("malformed field \"{field}\"")));
}
Ok(path)
}
pub fn is_settable(kind: FieldKind, field: &str) -> Result<bool, PipelineError> {
Ok(kind.settable().contains(&split_field(field)?[0]))
}
/// The value at a dot path; `None` where JavaScript read `undefined`.
pub fn read_field(item: &Item, field: &str) -> Result<Option<Value>, PipelineError> {
let mut value: Option<&Map<String, Value>> = Some(item);
let mut found: Option<&Value> = None;
for key in split_field(field)? {
let Some(holder) = value else {
return Ok(None);
};
found = holder.get(key);
value = found.and_then(Value::as_object);
}
Ok(found.cloned())
}
/// Writes a deep copy of value; missing intermediate objects are created (a non-object value
/// on the way is replaced, as `holder[key] = {}` did).
pub fn write_field(item: &mut Item, field: &str, value: &Value) -> Result<(), PipelineError> {
let path = split_field(field)?;
let (last, holders) = path.split_last().ok_or_else(|| fail("empty field"))?;
let mut holder = item;
for key in holders {
if !holder.get(*key).is_some_and(Value::is_object) {
holder.insert((*key).to_owned(), Value::Object(Map::new()));
}
holder = holder
.get_mut(*key)
.and_then(Value::as_object_mut)
.ok_or_else(|| fail("field holder vanished"))?;
}
holder.insert((*last).to_owned(), value.clone());
Ok(())
}
@@ -0,0 +1,310 @@
//! Report migration map (`packages/anatomy/src/curation/migration-map.ts`): where every
//! identifier a Report v2 may carry lands in the curated draft. Annotations come from the
//! legacy id map (then follow annotation renames), finding paths from replaying the applier's
//! rename, move and remove records over every node of the legacy baseline. A node removed with
//! its category and re-added under another category at the same relative place (the sphenoid
//! restructure) is re-homed when the match is unique; anything else stays null = "removed",
//! and the report keeps the finding with its snapshots.
use serde::Serialize;
use serde_json::{Map, Value, json};
use super::records::ChangeRecord;
use super::schema::Op;
use super::targets::{TargetKind, all_findings, list_at, top_collection_path};
use crate::curation::PipelineError;
use crate::curation::text::js_object;
const FINDING: &str = "finding:";
/// The sheet a report references: id, database version (null for a bundled draft), document
/// fingerprint (the `SheetRef` of Report v3, camelCase).
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SheetRef {
pub sheet_id: String,
pub version_id: Option<String>,
pub document_sha256: String,
}
pub struct MigrationMapInput<'a> {
pub baseline: &'a Value,
pub final_document: &'a Value,
pub legacy_id_map: &'a Map<String, Value>,
pub records: &'a [ChangeRecord],
pub sheet: &'a SheetRef,
pub main_view_id: &'a str,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct MigrationMapStats {
pub nodes: usize,
pub identical: usize,
pub remapped: usize,
pub rehomed: usize,
pub removed: usize,
}
struct PathEntry {
legacy: String,
current: Option<String>,
rehomed: bool,
}
/// `target.slice(FINDING.length)`: the first eight UTF-16 units dropped, whatever they are.
fn strip(target: &str) -> String {
let mut units: Vec<u16> = target.encode_utf16().collect();
units.drain(..units.len().min(FINDING.len()));
String::from_utf16_lossy(&units)
}
fn last_segment(path: &str) -> &str {
path.rsplit('/').next().unwrap_or(path)
}
fn with_last_segment(path: &str, id: &str) -> String {
match path.rfind('/') {
None => id.to_owned(),
Some(cut) => format!("{}{id}", &path[..=cut]),
}
}
fn replace_prefix(entries: &mut [PathEntry], from: &str, to: Option<&str>) {
for entry in entries {
let Some(current) = &entry.current else {
continue;
};
if current == from {
entry.current = to.map(str::to_owned);
} else if let Some(rest) = current.strip_prefix(&format!("{from}/")) {
entry.current = to.map(|to| format!("{to}/{rest}"));
}
}
}
/// Legacy id → current id, for the annotations and the views (`Map` in the TypeScript).
struct Ids {
annotations: Vec<(String, String)>,
views: Vec<(String, String)>,
}
fn id_entries(map: &Map<String, Value>, key: &str) -> Vec<(String, String)> {
map.get(key)
.and_then(Value::as_object)
.map(|object| {
object
.iter()
.map(|(legacy, current)| {
(
legacy.clone(),
current.as_str().unwrap_or_default().to_owned(),
)
})
.collect()
})
.unwrap_or_default()
}
fn apply_record(entries: &mut [PathEntry], ids: &mut Ids, record: &ChangeRecord) {
let target = record.target.as_str();
let (rename_to, move_to) = match &record.op {
Op::Rename(op) => (Some(op.id.as_str()), None),
Op::Remove(_) => (None, None),
Op::Move(op) => (None, Some(op.to.as_str())),
_ => return,
};
if let Some(from) = target.strip_prefix(FINDING) {
match &record.op {
Op::Rename(op) => replace_prefix(entries, from, Some(&with_last_segment(from, &op.id))),
Op::Remove(_) => replace_prefix(entries, from, None),
_ => {
let to = format!(
"{}/{}",
strip(move_to.unwrap_or_default()),
last_segment(from)
);
replace_prefix(entries, from, Some(&to));
}
}
return;
}
// `target.split(':', 2)`: the kind, then what lies before the next colon.
let mut parts = target.splitn(3, ':');
let kind = parts.next().unwrap_or_default();
let id = parts.next().unwrap_or_default();
if kind == "zone" {
for entry in entries {
let Some(current) = &entry.current else {
continue;
};
let Some(rest) = current.strip_prefix(&format!("{id}/")) else {
continue;
};
entry.current = match &record.op {
Op::Remove(_) => None,
Op::Rename(op) => Some(format!("{}/{rest}", op.id)),
_ => Some(format!("{id}/{rest}")),
};
}
return;
}
if let Some(to) = rename_to
&& (kind == "annotation" || kind == "view")
{
let map = if kind == "annotation" {
&mut ids.annotations
} else {
&mut ids.views
};
for (_, current) in map.iter_mut() {
if current == id {
*current = to.to_owned();
}
}
}
}
/// `zone/<any category>/<rest>` present exactly once in the final document.
fn rehome(entry: &mut PathEntry, final_paths: &[String]) {
let mut segments = entry.legacy.split('/');
let zone = segments.next().unwrap_or_default();
let _category = segments.next();
let rest: Vec<&str> = segments.collect();
if rest.is_empty() {
return;
}
let tail = format!("/{}", rest.join("/"));
let matches: Vec<&String> = final_paths
.iter()
.filter(|path| {
path.starts_with(&format!("{zone}/"))
&& path.ends_with(&tail)
&& path.split('/').count() == rest.len() + 2
})
.collect();
if let [only] = matches[..] {
entry.current = Some(only.clone());
entry.rehomed = true;
}
}
fn text(value: Option<&Value>) -> String {
value.and_then(Value::as_str).unwrap_or_default().to_owned()
}
/// Annotation id → `{ fr, en }` of its name, empty strings where the name is missing.
fn annotation_names(doc: &Value) -> Result<Vec<(String, Value)>, PipelineError> {
let annotations = list_at(doc, &top_collection_path(TargetKind::Annotation))?;
Ok(annotations
.iter()
.map(|annotation| {
let name = annotation.get("name");
let id = super::records::js_string(annotation.get("id"));
let fr = text(name.and_then(|n| n.get("fr")));
let en = text(name.and_then(|n| n.get("en")));
(id, json!({ "fr": fr, "en": en }))
})
.collect())
}
fn initial_entries(baseline: &Value) -> Result<Vec<PathEntry>, PipelineError> {
Ok(all_findings(baseline)?
.into_iter()
.map(|entry| {
let legacy = strip(&entry.target);
PathEntry {
current: Some(legacy.clone()),
legacy,
rehomed: false,
}
})
.collect())
}
fn stats_of(entries: &[PathEntry]) -> MigrationMapStats {
MigrationMapStats {
nodes: entries.len(),
identical: entries
.iter()
.filter(|e| e.current.as_deref() == Some(e.legacy.as_str()))
.count(),
remapped: entries
.iter()
.filter(|e| {
e.current.is_some() && e.current.as_deref() != Some(e.legacy.as_str()) && !e.rehomed
})
.count(),
rehomed: entries.iter().filter(|e| e.rehomed).count(),
removed: entries.iter().filter(|e| e.current.is_none()).count(),
}
}
fn pairs(entries: &[(String, String)]) -> Map<String, Value> {
js_object(
entries
.iter()
.map(|(legacy, current)| (legacy.clone(), Value::from(current.as_str()))),
)
}
/// The map (in the TypeScript key order) and its stats.
pub fn build_report_migration_map(
input: &MigrationMapInput<'_>,
) -> Result<(Value, MigrationMapStats), PipelineError> {
let mut entries = initial_entries(input.baseline)?;
let mut ids = Ids {
annotations: id_entries(input.legacy_id_map, "annotations"),
views: id_entries(input.legacy_id_map, "views"),
};
for record in input.records {
apply_record(&mut entries, &mut ids, record);
}
// `new Set(...)`: a path met twice counts once.
let mut final_paths: Vec<String> = Vec::new();
for entry in all_findings(input.final_document)? {
let path = strip(&entry.target);
if !final_paths.contains(&path) {
final_paths.push(path);
}
}
for entry in &mut entries {
if entry
.current
.as_ref()
.is_some_and(|current| !final_paths.contains(current))
{
entry.current = None;
}
if entry.current.is_none() {
rehome(entry, &final_paths);
}
}
let legacy_names = annotation_names(input.baseline)?;
let import_ids = id_entries(input.legacy_id_map, "annotations");
let annotations = js_object(ids.annotations.iter().map(|(legacy, final_id)| {
let imported = import_ids
.iter()
.find(|(key, _)| key == legacy)
.map(|(_, id)| id.as_str());
let name = imported
.and_then(|id| legacy_names.iter().find(|(key, _)| key == id))
.map_or_else(|| json!({ "fr": "", "en": "" }), |(_, name)| name.clone());
(legacy.clone(), json!({ "id": final_id, "name": name }))
}));
let finding_paths = js_object(entries.iter().map(|entry| {
let current = entry
.current
.as_ref()
.map_or(Value::Null, |c| Value::from(c.as_str()));
(entry.legacy.clone(), current)
}));
let map = json!({
"schema_version": 1,
"sheet": input.sheet,
"main_view_id": input.main_view_id,
"views": pairs(&ids.views),
"annotations": annotations,
"finding_paths": finding_paths,
});
Ok((map, stats_of(&entries)))
}
+24
View File
@@ -0,0 +1,24 @@
//! The draft side of the pipeline (R5.3): the authored changeset applied on the legacy
//! baseline, the report migration map, the document statistics and the seed SQL
//! (`packages/anatomy/src/curation`, without the review drawings and the Markdown reports of
//! R5.4). Parity with the TypeScript is proven by `tests/curation_draft.rs` against the
//! committed `draft/` folder, `oracles/r5/pipeline.json` and `oracles/r5/curation-synthetic.json`.
pub mod apply;
pub mod fields;
pub mod migration_map;
pub mod ops;
pub mod pipeline;
pub mod records;
pub mod rename;
pub mod schema;
pub mod seed;
pub mod stats;
pub mod targets;
pub use apply::{apply_changeset, curated_issues};
pub use pipeline::{
CLEANED_ASSET_FILE, DRAFT_SEED_SQL_FILE, DraftInput, DraftResult, MIGRATION_MAP_FILE,
SHEET_REF_FILE, build_equine_draft, cleaned_asset, parse_view_box, render_draft_artifacts,
};
pub use schema::{ASSET_CHANGE_ID, Change, Changeset, parse_changeset, parse_references};
+399
View File
@@ -0,0 +1,399 @@
//! The six op kinds of a change (`packages/anatomy/src/curation/ops.ts`). Each one resolves
//! its target, edits the working document in place and records what it changed; anything it
//! cannot do exactly as written refuses (the applier adds the change id and op index).
use serde_json::Value;
use super::fields::{FieldKind, is_settable, read_field, write_field};
use super::records::{Recorder, js_deep_equal, js_string};
use super::rename::{Rename, propagate_rename};
use super::schema::{
AddOp, CiteOp, DocumentCollection, MoveOp, Op, Position, RemoveOp, RenameOp, Scope, SetOp,
};
use super::targets::{
Located, Path, Step, TargetKind, at, child_list, child_prefix, fail, finding_scope, has_key,
id_text, item_at_mut, list_at, list_at_mut, locate, top_collection_path,
};
use crate::curation::PipelineError;
fn collection_kind(field: DocumentCollection) -> TargetKind {
match field {
DocumentCollection::References => TargetKind::Reference,
DocumentCollection::Categories => TargetKind::Category,
DocumentCollection::Views => TargetKind::View,
DocumentCollection::Annotations => TargetKind::Annotation,
DocumentCollection::FindingZones => TargetKind::Zone,
DocumentCollection::Regions => TargetKind::Region,
}
}
fn collection_name(field: DocumentCollection) -> &'static str {
match field {
DocumentCollection::References => "references",
DocumentCollection::Categories => "categories",
DocumentCollection::Views => "views",
DocumentCollection::Annotations => "annotations",
DocumentCollection::FindingZones => "finding_zones",
DocumentCollection::Regions => "regions",
}
}
/// No sibling but `excluded` may already carry the id.
fn assert_free(
list: &[Value],
key: &str,
id: &str,
excluded: Option<usize>,
) -> Result<(), PipelineError> {
let taken = list
.iter()
.enumerate()
.any(|(index, entry)| Some(index) != excluded && has_key(entry, key, id));
if taken {
return Err(fail(format!("\"{id}\" already exists among its siblings")));
}
Ok(())
}
fn insertion_index(
list: &[Value],
key: &str,
position: Option<&Position>,
) -> Result<usize, PipelineError> {
let Some(position) = position else {
return Ok(list.len());
};
let (anchor, after) = match position {
Position::Index(index) => {
let index = usize::try_from(*index).unwrap_or(usize::MAX);
if index > list.len() {
return Err(fail(format!("position {index} out of range")));
}
return Ok(index);
}
Position::After { after } => (after, true),
Position::Before { before } => (before, false),
};
let index = list
.iter()
.position(|entry| has_key(entry, key, anchor))
.ok_or_else(|| fail(format!("unknown sibling \"{anchor}\"")))?;
Ok(if after { index + 1 } else { index })
}
fn unknown_field(op: &SetOp) -> PipelineError {
fail(format!(
"unknown field \"{}\" on \"{}\"",
op.field, op.target
))
}
/// A B or C change leaves findings to the professional: no 'reviewed'.
fn assert_not_reviewed(recorder: &Recorder<'_>, op: &SetOp) -> Result<(), PipelineError> {
if op.field != "review_status" || op.value != "reviewed" {
return Ok(());
}
if recorder.change.level.needs_confirmation() {
return Err(fail(format!(
"level {:?} cannot set \"{}\" review_status to \"reviewed\"",
recorder.change.level, op.target
)));
}
Ok(())
}
fn set_self(
doc: &mut Value,
recorder: &mut Recorder<'_>,
located: &Located,
op: &SetOp,
) -> Result<(), PipelineError> {
if !is_settable(FieldKind::of(&located.target, None), &op.field)? {
return Err(unknown_field(op));
}
let item = item_at_mut(doc, &located.item_path())?;
recorder.touch(item);
let before = read_field(item, &op.field)?;
write_field(item, &op.field, &op.value)?;
recorder.record(&op.target, Some(&op.field), before, Some(op.value.clone()));
Ok(())
}
/// Target and descendants that carry the field; unchanged items are skipped.
fn set_subtree(
doc: &mut Value,
recorder: &mut Recorder<'_>,
located: &Located,
op: &SetOp,
) -> Result<(), PipelineError> {
if located.target.kind != TargetKind::Finding {
return Err(fail(format!(
"scope subtree needs a finding target, got \"{}\"",
op.target
)));
}
let mut entries = Vec::new();
for entry in finding_scope(doc, located, true)? {
let kind = FieldKind::of(&located.target, Some(entry.is_category));
if is_settable(kind, &op.field)? {
entries.push(entry);
}
}
if entries.is_empty() {
return Err(unknown_field(op));
}
for entry in &entries {
recorder.touch(item_at_mut(doc, &entry.path)?);
}
for entry in &entries {
let item = item_at_mut(doc, &entry.path)?;
let before = read_field(item, &op.field)?;
if before
.as_ref()
.is_some_and(|value| js_deep_equal(value, &op.value))
{
continue;
}
write_field(item, &op.field, &op.value)?;
recorder.record(
&entry.target,
Some(&op.field),
before,
Some(op.value.clone()),
);
}
Ok(())
}
fn apply_set(
doc: &mut Value,
recorder: &mut Recorder<'_>,
op: &SetOp,
) -> Result<(), PipelineError> {
assert_not_reviewed(recorder, op)?;
let located = locate(doc, &op.target)?;
match op.scope {
None | Some(Scope::OwnItem) => set_self(doc, recorder, &located, op),
Some(Scope::Subtree) => set_subtree(doc, recorder, &located, op),
}
}
fn apply_rename(
doc: &mut Value,
recorder: &mut Recorder<'_>,
op: &RenameOp,
) -> Result<(), PipelineError> {
let located = locate(doc, &op.target)?;
let Some(list) = &located.list else {
return Err(fail("the document has no id to rename"));
};
let key = located.target.kind.key_field();
assert_free(list_at(doc, list)?, key, &op.id, Some(located.index))?;
let item = item_at_mut(doc, &located.item_path())?;
let before = Value::Object(item.clone());
let from = js_string(item.get(key));
item.insert(key.to_owned(), Value::from(op.id.clone()));
recorder.touch(item);
let after = Value::Object(item.clone());
recorder.record(&op.target, None, Some(before), Some(after));
let rename = Rename {
kind: located.target.kind,
from,
to: op.id.clone(),
};
propagate_rename(doc, recorder, &rename)
}
struct Destination {
list: Path,
key: &'static str,
prefix: String,
}
fn add_destination(doc: &mut Value, op: &AddOp) -> Result<Destination, PipelineError> {
let parent = locate(doc, &op.target)?;
if parent.target.kind != TargetKind::Document {
if let Some(field) = op.field {
return Err(fail(format!(
"field \"{}\" is only valid on document",
collection_name(field)
)));
}
return Ok(Destination {
list: child_list(doc, &parent)?,
key: "id",
prefix: child_prefix(&parent),
});
}
let Some(field) = op.field else {
return Err(fail("add on document needs a field"));
};
let kind = collection_kind(field);
let list = top_collection_path(kind);
list_at(doc, &list)?;
Ok(Destination {
list,
key: kind.key_field(),
prefix: format!("{}:", kind.name()),
})
}
fn apply_add(
doc: &mut Value,
recorder: &mut Recorder<'_>,
op: &AddOp,
) -> Result<(), PipelineError> {
let Some(value) = op.value.as_object() else {
return Err(fail("add needs an object value"));
};
let dest = add_destination(doc, op)?;
let Some(id) = value.get(dest.key).and_then(Value::as_str) else {
return Err(fail(format!(
"the added item needs a string \"{}\"",
dest.key
)));
};
let list = list_at(doc, &dest.list)?;
assert_free(list, dest.key, id, None)?;
let index = insertion_index(list, dest.key, op.position.as_ref())?;
list_at_mut(doc, &dest.list)?.insert(index, op.value.clone());
let mut added = dest.list.clone();
added.push(Step::Index(index));
recorder.touch_tree(doc, &added)?;
let target = format!("{}{id}", dest.prefix);
recorder.record(&target, None, None, Some(op.value.clone()));
Ok(())
}
fn apply_remove(
doc: &mut Value,
recorder: &mut Recorder<'_>,
op: &RemoveOp,
) -> Result<(), PipelineError> {
let located = locate(doc, &op.target)?;
let Some(list) = &located.list else {
return Err(fail("the document cannot be removed"));
};
let item = list_at_mut(doc, list)?.remove(located.index);
recorder.record(&op.target, None, Some(item), None);
Ok(())
}
/// `after` of a move record is the new target path of the item.
fn apply_move(
doc: &mut Value,
recorder: &mut Recorder<'_>,
op: &MoveOp,
) -> Result<(), PipelineError> {
let source = locate(doc, &op.target)?;
if source.target.kind != TargetKind::Finding {
return Err(fail(format!("only findings move, got \"{}\"", op.target)));
}
if op.to == op.target || op.to.starts_with(&format!("{}/", op.target)) {
return Err(fail(format!("cannot move \"{}\" into itself", op.target)));
}
let parent = locate(doc, &op.to)?;
let list = child_list(doc, &parent)?;
let id = id_text(at(doc, &source.item_path())?);
let excluded = (Some(&list) == source.list.as_ref()).then_some(source.index);
assert_free(list_at(doc, &list)?, "id", &id, excluded)?;
let source_list = source
.list
.as_ref()
.ok_or_else(|| fail("a finding has siblings"))?;
let item = list_at_mut(doc, source_list)?.remove(source.index);
// The removal may have shifted the destination's indices: resolve it again.
let parent = locate(doc, &op.to)?;
let list = child_list(doc, &parent)?;
let index = insertion_index(list_at(doc, &list)?, "id", op.position.as_ref())?;
list_at_mut(doc, &list)?.insert(index, item);
let mut moved = list;
moved.push(Step::Index(index));
let item = item_at_mut(doc, &moved)?;
recorder.touch(item);
let before = Value::Object(item.clone());
let after = format!("{}{id}", child_prefix(&parent));
recorder.record(&op.target, None, Some(before), Some(Value::from(after)));
Ok(())
}
/// Every cited id must be declared in the document references.
pub fn assert_declared(doc: &Value, ids: &[String]) -> Result<(), PipelineError> {
let declared: Vec<&str> = list_at(doc, &top_collection_path(TargetKind::Reference))?
.iter()
.filter_map(|reference| reference.get("id").and_then(Value::as_str))
.collect();
let missing: Vec<&str> = ids
.iter()
.map(String::as_str)
.filter(|id| !declared.contains(id))
.collect();
if !missing.is_empty() {
return Err(fail(format!(
"undeclared reference(s): {}",
missing.join(", ")
)));
}
Ok(())
}
/// `[...new Set(ids)]`: first occurrences, in order.
fn distinct(ids: &[String]) -> Vec<&String> {
let mut out: Vec<&String> = Vec::new();
for id in ids {
if !out.contains(&id) {
out.push(id);
}
}
out
}
/// Appends citations in order, never twice on the same item.
fn apply_cite(
doc: &mut Value,
recorder: &mut Recorder<'_>,
op: &CiteOp,
) -> Result<(), PipelineError> {
assert_declared(doc, &op.reference_ids)?;
let located = locate(doc, &op.target)?;
let scope = finding_scope(doc, &located, op.scope == Some(Scope::Subtree))?;
for entry in &scope {
recorder.touch(item_at_mut(doc, &entry.path)?);
}
for entry in &scope {
let item = item_at_mut(doc, &entry.path)?;
let before: Vec<Value> = item
.get("reference_ids")
.and_then(Value::as_array)
.cloned()
.unwrap_or_default();
let added: Vec<Value> = distinct(&op.reference_ids)
.into_iter()
.filter(|id| !before.contains(&Value::from(id.as_str())))
.map(|id| Value::from(id.clone()))
.collect();
if added.is_empty() {
continue;
}
let after: Vec<Value> = before.iter().cloned().chain(added).collect();
item.insert("reference_ids".to_owned(), Value::Array(after.clone()));
recorder.record(
&entry.target,
Some("reference_ids"),
Some(Value::Array(before)),
Some(Value::Array(after)),
);
}
Ok(())
}
pub fn apply_op(doc: &mut Value, recorder: &mut Recorder<'_>) -> Result<(), PipelineError> {
match recorder.op {
Op::Set(op) => apply_set(doc, recorder, op),
Op::Rename(op) => apply_rename(doc, recorder, op),
Op::Add(op) => apply_add(doc, recorder, op),
Op::Remove(op) => apply_remove(doc, recorder, op),
Op::Move(op) => apply_move(doc, recorder, op),
Op::Cite(op) => apply_cite(doc, recorder, op),
}
}
+210
View File
@@ -0,0 +1,210 @@
//! Legacy import → curated working draft (`packages/anatomy/src/curation/pipeline.ts`). The
//! asset step runs first as the reserved change CHG-000 (one cleaned drawing, head view =
//! crop), then the authored changeset; both land in the same records, so the CHANGELOG lists
//! them alike. Deterministic: same inputs, same bytes.
//!
//! The cleaned drawing is an input here, not a product: the owner froze the bytes of
//! `draft/assets/horse.svg` (BDR-068), so the Rust pipeline describes them (sha256, byte size,
//! asset id) and copies them, and never re-cleans the legacy drawing. The review drawings and
//! the two Markdown reports are R5.4.
use serde_json::{Map, Value, json};
use super::apply::{apply_changeset, curated_issues};
use super::migration_map::{MigrationMapInput, SheetRef, build_report_migration_map};
use super::records::ChangeRecord;
use super::schema::{
ASSET_CHANGE_ID, Change, ChangeKind, ChangeLevel, ChangePriority, ChangeSummary, ChangeTitle,
Op, SetOp,
};
use super::seed::{DRAFT_CHANGE_NOTE, DraftSeedParams, render_draft_seed_sql};
use super::stats::{DocumentStats, document_stats};
use super::targets::fail;
use crate::anatomy::svg::number::{js_number_of, js_split_list};
use crate::curation::PipelineError;
use crate::curation::ids::{asset_id_from_sha256, sha256_hex};
use crate::curation::legacy::types::{AssetRecord, ImportResult};
use crate::curation::legacy::{HEAD_VIEW_ID, MAIN_VIEW_ID};
use crate::curation::prettier_json::format_json;
use crate::domain::sheet::{Reference, SheetIssue, ViewBox};
pub const DRAFT_SEED_SQL_FILE: &str = "equine_draft.sql";
pub const SHEET_REF_FILE: &str = "sheet-ref.json";
pub const MIGRATION_MAP_FILE: &str = "report-migration-map.json";
pub const CLEANED_ASSET_FILE: &str = "horse.svg";
pub struct DraftInput<'a> {
pub import_result: &'a ImportResult,
/// The frozen cleaned drawing, `draft/assets/horse.svg`.
pub cleaned_svg: &'a [u8],
pub head_view_box: ViewBox,
pub references: &'a [Reference],
pub changes: &'a [Change],
}
pub struct DraftResult {
pub document: Value,
/// Legacy import the changeset was applied to.
pub baseline: Value,
pub legacy_id_map: Map<String, Value>,
pub records: Vec<ChangeRecord>,
pub changes: Vec<ChangeSummary>,
pub assets: Vec<AssetRecord>,
pub stats_before: DocumentStats,
pub stats_after: DocumentStats,
/// Integrity warnings of the curated document (errors refuse).
pub issues: Vec<SheetIssue>,
pub sheet_id: String,
}
/// Manifest entry of the cleaned drawing; id derived from its sha256 (`cleanedAsset`).
pub fn cleaned_asset(bytes: &[u8]) -> AssetRecord {
let sha256 = sha256_hex(bytes);
AssetRecord {
file: CLEANED_ASSET_FILE.to_owned(),
svg_asset_id: asset_id_from_sha256(&sha256),
sha256,
byte_size: bytes.len(),
}
}
/// SVG viewBox attribute ("minX minY width height") → document view_box (`parseViewBox`).
pub fn parse_view_box(raw: Option<&str>) -> Result<ViewBox, PipelineError> {
let numbers: Vec<f64> = js_split_list(raw.unwrap_or_default())
.into_iter()
.map(js_number_of)
.collect();
if numbers.len() != 4 || numbers.iter().any(|n| !n.is_finite()) {
return Err(fail(format!("invalid viewBox {:?}", raw.unwrap_or("null"))));
}
Ok(ViewBox {
min_x: numbers[0],
min_y: numbers[1],
width: numbers[2],
height: numbers[3],
})
}
fn set(target: String, field: &str, value: Value) -> Op {
Op::Set(SetOp {
target,
field: field.to_owned(),
value,
scope: None,
})
}
fn asset_change(asset: &AssetRecord, head_view_box: &ViewBox) -> Result<Change, PipelineError> {
let view_box = serde_json::to_value(head_view_box)
.map_err(|error| fail(format!("head view box: {error}")))?;
Ok(Change {
id: ASSET_CHANGE_ID.to_owned(),
level: ChangeLevel::A,
kind: ChangeKind::Technical,
title: ChangeTitle {
fr: "Un seul dessin, vue tête = recadrage".to_owned(),
en: "Single drawing, head view = crop".to_owned(),
},
rationale: "Les deux fichiers historiques portent le même dessin (tracés identiques) et \
les marqueurs sont désormais des données de la fiche : un seul dessin \
nettoyé, la vue tête en est un recadrage."
.to_owned(),
reference_ids: Vec::new(),
question: None,
priority: ChangePriority::Medium,
ops: vec![
set(
format!("view:{MAIN_VIEW_ID}"),
"svg_asset_id",
Value::from(asset.svg_asset_id.as_str()),
),
set(format!("view:{HEAD_VIEW_ID}"), "svg_asset_id", Value::Null),
set(format!("view:{HEAD_VIEW_ID}"), "view_box", view_box),
],
})
}
fn assert_not_reserved(changes: &[Change]) -> Result<(), PipelineError> {
if changes.iter().any(|change| change.id == ASSET_CHANGE_ID) {
return Err(fail(format!(
"{ASSET_CHANGE_ID} is reserved for the asset step"
)));
}
Ok(())
}
pub fn build_equine_draft(input: &DraftInput<'_>) -> Result<DraftResult, PipelineError> {
assert_not_reserved(input.changes)?;
let asset = cleaned_asset(input.cleaned_svg);
let mut changes = vec![asset_change(&asset, &input.head_view_box)?];
changes.extend(input.changes.iter().cloned());
let baseline = &input.import_result.document;
let result = apply_changeset(baseline, input.references, &changes)?;
Ok(DraftResult {
stats_before: document_stats(baseline)?,
stats_after: document_stats(&result.document)?,
issues: curated_issues(&result.document)?,
document: result.document,
baseline: baseline.clone(),
legacy_id_map: input.import_result.legacy_id_map.clone(),
records: result.records,
changes: changes.iter().map(Change::summary).collect(),
assets: vec![asset],
sheet_id: input.import_result.sheet_id.clone(),
})
}
/// What a report references: the sheet and the exact document bytes (the historical rule,
/// kept until the R5.6 switch).
fn sheet_ref(draft: &DraftResult, document_json: &str) -> SheetRef {
SheetRef {
sheet_id: draft.sheet_id.clone(),
version_id: None,
document_sha256: sha256_hex(document_json.as_bytes()),
}
}
/// The migration map (`report-migration-map.json` value) and its stats.
pub fn draft_migration_map(
draft: &DraftResult,
reference: &SheetRef,
) -> Result<(Value, super::migration_map::MigrationMapStats), PipelineError> {
build_report_migration_map(&MigrationMapInput {
baseline: &draft.baseline,
final_document: &draft.document,
legacy_id_map: &draft.legacy_id_map,
records: &draft.records,
sheet: reference,
main_view_id: MAIN_VIEW_ID,
})
}
/// The R5.3 text artifacts of the draft folder, in the TypeScript order; the R5.4 files
/// (review drawings, CHANGELOG, REVIEW-REPORT) are not produced yet.
pub fn render_draft_artifacts(draft: &DraftResult) -> Result<Vec<(String, String)>, PipelineError> {
let document_json = format_json(&draft.document)?;
let reference = sheet_ref(draft, &document_json);
let (map, _stats) = draft_migration_map(draft, &reference)?;
let assets = serde_json::to_value(&draft.assets)
.map_err(|error| fail(format!("assets.json: {error}")))?;
let sheet_ref_json = json!({
"sheet_id": reference.sheet_id,
"version_id": reference.version_id,
"document_sha256": reference.document_sha256,
"schema_version": draft.document.get("schema_version"),
"species_id": draft.document.get("species_id"),
});
let seed = render_draft_seed_sql(&DraftSeedParams {
sheet_id: &draft.sheet_id,
assets: &draft.assets,
document_json: &document_json,
change_note: DRAFT_CHANGE_NOTE,
})?;
Ok(vec![
("document.json".to_owned(), document_json),
("assets.json".to_owned(), format_json(&assets)?),
(SHEET_REF_FILE.to_owned(), format_json(&sheet_ref_json)?),
(MIGRATION_MAP_FILE.to_owned(), format_json(&map)?),
(DRAFT_SEED_SQL_FILE.to_owned(), seed),
])
}
+176
View File
@@ -0,0 +1,176 @@
//! One elementary edit of the curated document, kept for the CHANGELOG
//! (`packages/anatomy/src/curation/types.ts`), and the state shared by the ops of one change.
//!
//! The TypeScript remembered the items a change touched by object identity. The port marks
//! each touched item with a transient key (`TOUCH_MARK`, holding the touch order) that the
//! applier removes at the end of the change and that never reaches a record.
use serde::Serialize;
use serde_json::{Map, Value};
use super::schema::{ChangeSummary, Op};
use super::targets::{Item, Path, Step, at_mut};
use crate::curation::PipelineError;
/// A key no authored document holds: a NUL character starts it.
pub const TOUCH_MARK: &str = "\u{0}touched";
/// `before` / `after` are deep copies: the field value for a set, the whole item for a rename,
/// remove or move, null on the missing side.
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct ChangeRecord {
pub change: ChangeSummary,
pub op: Op,
pub target: String,
pub field: Option<String>,
pub before: Value,
pub after: Value,
}
#[derive(Debug, Clone, PartialEq)]
pub struct CurationResult {
pub document: Value,
pub records: Vec<ChangeRecord>,
}
/// What the op functions of one change share besides the document; the touch counter spans
/// the ops of the change (the first touch of an item fixes its place in the review marking).
pub struct Recorder<'a> {
pub change: &'a ChangeSummary,
pub op: &'a Op,
pub records: &'a mut Vec<ChangeRecord>,
next_touch: &'a mut u64,
}
impl<'a> Recorder<'a> {
pub fn new(
change: &'a ChangeSummary,
op: &'a Op,
records: &'a mut Vec<ChangeRecord>,
next_touch: &'a mut u64,
) -> Self {
Self {
change,
op,
records,
next_touch,
}
}
/// Absent values are recorded as null so records stay plain JSON.
pub fn record(
&mut self,
target: &str,
field: Option<&str>,
before: Option<Value>,
after: Option<Value>,
) {
self.records.push(ChangeRecord {
change: self.change.clone(),
op: self.op.clone(),
target: target.to_owned(),
field: field.map(str::to_owned),
before: unmarked(before),
after: unmarked(after),
});
}
/// Marks an item as touched by the change, once (the first touch keeps its order).
pub fn touch(&mut self, item: &mut Item) {
if item.contains_key(TOUCH_MARK) {
return;
}
item.insert(TOUCH_MARK.to_owned(), Value::from(*self.next_touch));
*self.next_touch += 1;
}
/// Touches the item at `path` and every descendant node, depth first (`descendants`).
pub fn touch_tree(&mut self, doc: &mut Value, path: &Path) -> Result<(), PipelineError> {
let holder = at_mut(doc, path)?;
let Some(item) = holder.as_object_mut() else {
return Ok(());
};
self.touch(item);
let count = item
.get("children")
.and_then(Value::as_array)
.map_or(0, Vec::len);
for index in 0..count {
let mut child = path.clone();
child.push(Step::Key("children"));
child.push(Step::Index(index));
self.touch_tree(doc, &child)?;
}
Ok(())
}
}
/// The touch order of a marked item.
pub fn touch_order(item: &Item) -> Option<u64> {
item.get(TOUCH_MARK).and_then(Value::as_u64)
}
/// Removes the marks from a value and every value under it, keeping the key order.
pub fn strip_marks(value: &mut Value) {
match value {
Value::Object(map) => {
map.shift_remove(TOUCH_MARK);
map.values_mut().for_each(strip_marks);
}
Value::Array(items) => items.iter_mut().for_each(strip_marks),
_ => {}
}
}
fn unmarked(value: Option<Value>) -> Value {
let mut value = value.unwrap_or(Value::Null);
strip_marks(&mut value);
value
}
/// `String(value)` of a JSON value read from the document.
pub fn js_string(value: Option<&Value>) -> String {
match value {
None => "undefined".to_owned(),
Some(Value::Null) => "null".to_owned(),
Some(Value::Bool(flag)) => flag.to_string(),
Some(Value::Number(number)) => number
.as_f64()
.map_or_else(|| number.to_string(), crate::json::js_number),
Some(Value::String(text)) => text.clone(),
Some(Value::Array(items)) => items
.iter()
.map(|item| match item {
Value::Null => String::new(),
other => js_string(Some(other)),
})
.collect::<Vec<_>>()
.join(","),
Some(Value::Object(_)) => "[object Object]".to_owned(),
}
}
fn same_number(a: &serde_json::Number, b: &serde_json::Number) -> bool {
match (a.as_f64(), b.as_f64()) {
(Some(a), Some(b)) => a.to_bits() == b.to_bits(),
_ => a == b,
}
}
/// `isDeepStrictEqual` on JSON values: numbers by value (`Object.is`), objects by key set.
pub fn js_deep_equal(a: &Value, b: &Value) -> bool {
match (a, b) {
(Value::Number(a), Value::Number(b)) => same_number(a, b),
(Value::Array(a), Value::Array(b)) => {
a.len() == b.len() && a.iter().zip(b).all(|(x, y)| js_deep_equal(x, y))
}
(Value::Object(a), Value::Object(b)) => same_entries(a, b),
_ => a == b,
}
}
fn same_entries(a: &Map<String, Value>, b: &Map<String, Value>) -> bool {
a.len() == b.len()
&& a.iter()
.all(|(key, value)| b.get(key).is_some_and(|other| js_deep_equal(value, other)))
}
+130
View File
@@ -0,0 +1,130 @@
//! Rename propagation (`packages/anatomy/src/curation/rename.ts`): every field pointing at the
//! renamed id follows, and each rewrite is a record of its own so the CHANGELOG shows the
//! cascade.
use serde_json::Value;
use super::records::{Recorder, js_string};
use super::targets::{Step, TargetKind, all_findings, item_at_mut, list_at, top_collection_path};
use crate::curation::PipelineError;
pub struct Rename {
pub kind: TargetKind,
pub from: String,
pub to: String,
}
/// A field of the annotations or the views that holds an id of the renamed kind.
struct Link {
kind: TargetKind,
field: &'static str,
}
fn links(kind: TargetKind) -> &'static [Link] {
match kind {
TargetKind::Zone => &[Link {
kind: TargetKind::Annotation,
field: "finding_zone_id",
}],
TargetKind::Region => &[Link {
kind: TargetKind::Annotation,
field: "vertebral_region_code",
}],
TargetKind::View => &[
Link {
kind: TargetKind::View,
field: "parent_view_id",
},
Link {
kind: TargetKind::Annotation,
field: "view_id",
},
Link {
kind: TargetKind::Annotation,
field: "opens_view_id",
},
],
TargetKind::Category => &[Link {
kind: TargetKind::Annotation,
field: "category_id",
}],
_ => &[],
}
}
fn follow_link(
doc: &mut Value,
recorder: &mut Recorder<'_>,
link: &Link,
rename: &Rename,
) -> Result<(), PipelineError> {
let list = top_collection_path(link.kind);
let from = Value::from(rename.from.as_str());
for index in 0..list_at(doc, &list)?.len() {
let mut path = list.clone();
path.push(Step::Index(index));
let item = item_at_mut(doc, &path)?;
if item.get(link.field) != Some(&from) {
continue;
}
item.insert(link.field.to_owned(), Value::from(rename.to.as_str()));
let target = format!("{}:{}", link.kind.name(), js_string(item.get("id")));
recorder.record(
&target,
Some(link.field),
Some(from.clone()),
Some(Value::from(rename.to.as_str())),
);
}
Ok(())
}
fn follow_citations(
doc: &mut Value,
recorder: &mut Recorder<'_>,
rename: &Rename,
) -> Result<(), PipelineError> {
let from = Value::from(rename.from.as_str());
for entry in all_findings(doc)? {
let item = item_at_mut(doc, &entry.path)?;
let Some(ids) = item.get("reference_ids").and_then(Value::as_array) else {
continue;
};
if !ids.contains(&from) {
continue;
}
let before = Value::Array(ids.clone());
let after: Vec<Value> = ids
.iter()
.map(|id| {
if *id == from {
Value::from(rename.to.as_str())
} else {
id.clone()
}
})
.collect();
recorder.record(
&entry.target,
Some("reference_ids"),
Some(before),
Some(Value::Array(after.clone())),
);
item.insert("reference_ids".to_owned(), Value::Array(after));
}
Ok(())
}
pub fn propagate_rename(
doc: &mut Value,
recorder: &mut Recorder<'_>,
rename: &Rename,
) -> Result<(), PipelineError> {
if rename.kind == TargetKind::Reference {
return follow_citations(doc, recorder, rename);
}
for link in links(rename.kind) {
follow_link(doc, recorder, link, rename)?;
}
Ok(())
}
+442
View File
@@ -0,0 +1,442 @@
//! The authored curation inputs (`packages/anatomy/src/curation/schema.ts`): a changeset of
//! traceable changes applied on the legacy baseline, and the references registry they cite.
//! Every object is strict, as the Zod schemas were, so a typo in a hand-written file fails
//! loud instead of being ignored. Serialisation follows the Zod output: the shape order of the
//! keys, `priority` always present, an absent option absent.
//!
//! An optional key is `.optional()` as in Zod: absent is fine, `null` is refused.
//!
//! Declared difference: a `set` without `value` is refused (`z.unknown()` accepted the missing
//! key and `JSON.stringify` then dropped the field).
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};
use crate::curation::PipelineError;
use crate::domain::js;
use crate::domain::sheet::Reference;
/// Reserved for the asset step of the pipeline, never authored.
pub const ASSET_CHANGE_ID: &str = "CHG-000";
/// A demonstrated error, B supported improvement, C uncertain (question).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ChangeLevel {
A,
B,
C,
}
impl ChangeLevel {
/// Derived, never stored: only a demonstrated error needs no sign-off.
pub fn needs_confirmation(self) -> bool {
self != Self::A
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ChangeKind {
Correction,
Terminology,
Translation,
Restructure,
Enrichment,
Reference,
Geometry,
Removal,
Technical,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ChangePriority {
High,
Medium,
Low,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Scope {
#[serde(rename = "self")]
OwnItem,
Subtree,
}
/// Document-level collections an `add` on 'document' may extend.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum DocumentCollection {
References,
Categories,
Views,
Annotations,
FindingZones,
Regions,
}
/// Where an added or moved item lands among its siblings.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(untagged, deny_unknown_fields)]
pub enum Position {
Index(#[serde(deserialize_with = "non_negative_int")] u64),
After {
#[serde(deserialize_with = "non_empty")]
after: String,
},
Before {
#[serde(deserialize_with = "non_empty")]
before: String,
},
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct SetOp {
#[serde(deserialize_with = "non_empty")]
pub target: String,
#[serde(deserialize_with = "non_empty")]
pub field: String,
pub value: Value,
#[serde(
default,
deserialize_with = "js::present",
skip_serializing_if = "Option::is_none"
)]
pub scope: Option<Scope>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct RenameOp {
#[serde(deserialize_with = "non_empty")]
pub target: String,
#[serde(deserialize_with = "non_empty")]
pub id: String,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct AddOp {
#[serde(deserialize_with = "non_empty")]
pub target: String,
#[serde(
default,
deserialize_with = "js::present",
skip_serializing_if = "Option::is_none"
)]
pub field: Option<DocumentCollection>,
pub value: Value,
#[serde(
default,
deserialize_with = "js::present",
skip_serializing_if = "Option::is_none"
)]
pub position: Option<Position>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct RemoveOp {
#[serde(deserialize_with = "non_empty")]
pub target: String,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct MoveOp {
#[serde(deserialize_with = "non_empty")]
pub target: String,
#[serde(deserialize_with = "non_empty")]
pub to: String,
#[serde(
default,
deserialize_with = "js::present",
skip_serializing_if = "Option::is_none"
)]
pub position: Option<Position>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct CiteOp {
#[serde(deserialize_with = "non_empty")]
pub target: String,
#[serde(deserialize_with = "non_empty_strings_min_one")]
pub reference_ids: Vec<String>,
#[serde(
default,
deserialize_with = "js::present",
skip_serializing_if = "Option::is_none"
)]
pub scope: Option<Scope>,
}
/// The six op kinds of a change, tagged by `op`.
#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(tag = "op", rename_all = "snake_case")]
pub enum Op {
Set(SetOp),
Rename(RenameOp),
Add(AddOp),
Remove(RemoveOp),
Move(MoveOp),
Cite(CiteOp),
}
impl Op {
pub fn target(&self) -> &str {
match self {
Self::Set(op) => &op.target,
Self::Rename(op) => &op.target,
Self::Add(op) => &op.target,
Self::Remove(op) => &op.target,
Self::Move(op) => &op.target,
Self::Cite(op) => &op.target,
}
}
}
/// The discriminated union: the `op` key picks the variant, the rest is parsed strictly.
impl<'de> Deserialize<'de> for Op {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
use serde::de::Error;
let mut object = Map::<String, Value>::deserialize(deserializer)?;
let kind = match object.remove("op") {
Some(Value::String(kind)) => kind,
Some(_) => return Err(D::Error::custom("op must be a string")),
None => return Err(D::Error::custom("missing field `op`")),
};
let rest = Value::Object(object);
let parsed = match kind.as_str() {
"set" => serde_json::from_value(rest).map(Self::Set),
"rename" => serde_json::from_value(rest).map(Self::Rename),
"add" => serde_json::from_value(rest).map(Self::Add),
"remove" => serde_json::from_value(rest).map(Self::Remove),
"move" => serde_json::from_value(rest).map(Self::Move),
"cite" => serde_json::from_value(rest).map(Self::Cite),
other => return Err(D::Error::custom(format!("unknown op \"{other}\""))),
};
parsed.map_err(|error| D::Error::custom(format!("{kind} op: {error}")))
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ChangeTitle {
#[serde(deserialize_with = "non_empty")]
pub fr: String,
#[serde(deserialize_with = "non_empty")]
pub en: String,
}
/// A change as cited by its records: everything but the ops, in the Zod shape order.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct ChangeSummary {
pub id: String,
pub level: ChangeLevel,
pub kind: ChangeKind,
pub title: ChangeTitle,
pub rationale: String,
pub reference_ids: Vec<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub question: Option<String>,
pub priority: ChangePriority,
}
/// One authored change: `ChangeSchema` (strict, `priority` defaulted, `ops` non-empty).
#[derive(Debug, Clone, PartialEq, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Change {
#[serde(deserialize_with = "change_id")]
pub id: String,
pub level: ChangeLevel,
pub kind: ChangeKind,
pub title: ChangeTitle,
#[serde(deserialize_with = "non_empty")]
pub rationale: String,
#[serde(deserialize_with = "non_empty_strings")]
pub reference_ids: Vec<String>,
#[serde(default, deserialize_with = "optional_non_empty")]
pub question: Option<String>,
#[serde(default = "default_priority")]
pub priority: ChangePriority,
#[serde(deserialize_with = "ops_min_one")]
pub ops: Vec<Op>,
}
fn default_priority() -> ChangePriority {
ChangePriority::Medium
}
impl Change {
/// `changeSummary`: the change without its ops.
pub fn summary(&self) -> ChangeSummary {
ChangeSummary {
id: self.id.clone(),
level: self.level,
kind: self.kind,
title: self.title.clone(),
rationale: self.rationale.clone(),
reference_ids: self.reference_ids.clone(),
question: self.question.clone(),
priority: self.priority,
}
}
}
#[derive(Debug, Clone, PartialEq, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Changeset {
pub changes: Vec<Change>,
}
#[derive(Debug, Clone, PartialEq, Deserialize)]
#[serde(deny_unknown_fields)]
struct ReferencesFile {
references: Vec<Reference>,
}
fn non_empty<'de, D: serde::Deserializer<'de>>(d: D) -> Result<String, D::Error> {
let text = String::deserialize(d)?;
if text.is_empty() {
return Err(serde::de::Error::custom("must not be empty"));
}
Ok(text)
}
fn optional_non_empty<'de, D: serde::Deserializer<'de>>(d: D) -> Result<Option<String>, D::Error> {
non_empty(d).map(Some)
}
fn non_empty_strings<'de, D: serde::Deserializer<'de>>(d: D) -> Result<Vec<String>, D::Error> {
let items = Vec::<String>::deserialize(d)?;
if items.iter().any(String::is_empty) {
return Err(serde::de::Error::custom("ids must not be empty"));
}
Ok(items)
}
fn non_empty_strings_min_one<'de, D: serde::Deserializer<'de>>(
d: D,
) -> Result<Vec<String>, D::Error> {
let items = non_empty_strings(d)?;
if items.is_empty() {
return Err(serde::de::Error::custom("at least one id"));
}
Ok(items)
}
fn ops_min_one<'de, D: serde::Deserializer<'de>>(d: D) -> Result<Vec<Op>, D::Error> {
let ops = Vec::<Op>::deserialize(d)?;
if ops.is_empty() {
return Err(serde::de::Error::custom("a change needs at least one op"));
}
Ok(ops)
}
/// `^CHG-\d{3,}$` (ASCII digits, as a JavaScript regex without the `u` flag).
fn change_id<'de, D: serde::Deserializer<'de>>(d: D) -> Result<String, D::Error> {
let id = String::deserialize(d)?;
let digits = id.strip_prefix("CHG-").unwrap_or_default();
if digits.len() < 3 || !digits.bytes().all(|byte| byte.is_ascii_digit()) {
return Err(serde::de::Error::custom(format!(
"invalid change id {id:?}"
)));
}
Ok(id)
}
/// `z.number().int().min(0)`: an integral, non-negative JavaScript number (−0 and `1.0`
/// included); a value beyond `u64` saturates and fails the range check of the op.
fn non_negative_int<'de, D: serde::Deserializer<'de>>(d: D) -> Result<u64, D::Error> {
let value = f64::deserialize(d)?;
if !value.is_finite() || value.fract() != 0.0 || value < 0.0 {
return Err(serde::de::Error::custom(
"position must be a non-negative integer",
));
}
Ok(value as u64)
}
fn duplicates<'a>(ids: impl Iterator<Item = &'a str>) -> Vec<&'a str> {
let mut seen: Vec<&str> = Vec::new();
let mut repeated = Vec::new();
for id in ids {
if seen.contains(&id) {
repeated.push(id);
} else {
seen.push(id);
}
}
repeated
}
fn refuse(file: &str, error: impl std::fmt::Display) -> PipelineError {
PipelineError::Source(format!("{file}: {error}"))
}
/// `JSON.parse`: a repeated key keeps its first position and its last value (the parsed
/// value is read into the typed shape afterwards, so duplicates never reach serde's derive).
fn json_parse(file: &str, text: &str) -> Result<Value, PipelineError> {
js::parse_json(text).map_err(|error| refuse(file, error))
}
/// A key starting with NUL is reserved for the applier's transient touch marks
/// (`records::TOUCH_MARK`); an authored value holding one is refused (the TypeScript refused
/// such a key at the strict document gate).
fn reserved_key(value: &Value) -> Option<&str> {
match value {
Value::Object(map) => map.iter().find_map(|(key, item)| {
if key.starts_with('\0') {
Some(key.as_str())
} else {
reserved_key(item)
}
}),
Value::Array(items) => items.iter().find_map(reserved_key),
_ => None,
}
}
/// `ChangesetSchema.parse(JSON.parse(text))`.
pub fn parse_changeset(text: &str) -> Result<Changeset, PipelineError> {
let value = json_parse("changes.json", text)?;
if let Some(key) = reserved_key(&value) {
return Err(refuse("changes.json", format!("reserved key {key:?}")));
}
let changeset: Changeset =
serde_json::from_value(value).map_err(|e| refuse("changes.json", e))?;
for change in &changeset.changes {
if change.level == ChangeLevel::C && change.question.is_none() {
return Err(refuse(
"changes.json",
format!("{}: a level C change needs a question", change.id),
));
}
}
let ids = changeset.changes.iter().map(|change| change.id.as_str());
if let Some(id) = duplicates(ids).first() {
return Err(refuse("changes.json", format!("duplicate change id {id}")));
}
Ok(changeset)
}
/// `ReferencesFileSchema.parse(JSON.parse(text)).references`.
pub fn parse_references(text: &str) -> Result<Vec<Reference>, PipelineError> {
let value = json_parse("references.json", text)?;
let file: ReferencesFile =
serde_json::from_value(value).map_err(|e| refuse("references.json", e))?;
let ids = file
.references
.iter()
.map(|reference| reference.id.as_str());
if let Some(id) = duplicates(ids).first() {
return Err(refuse(
"references.json",
format!("duplicate reference id {id}"),
));
}
Ok(file.references)
}
+147
View File
@@ -0,0 +1,147 @@
//! Idempotent and converging PostgreSQL seed of an official sheet's working DRAFT
//! (`packages/anatomy/src/curation/seed.ts`). Runs as the database owner in `supabase db
//! reset` and in the PGlite harness. A fresh database gets the draft through
//! `private.insert_sheet_draft` (audited, null actor); a database still holding an older draft
//! as the sheet's only version gets its content replaced in place. Published rows are never
//! touched and nothing is published: that stays a human act through `publish_sheet_version`.
use super::targets::fail;
use crate::anatomy::svg::number::is_js_space;
use crate::curation::PipelineError;
use crate::curation::legacy::types::AssetRecord;
pub const DOCUMENT_DOLLAR_TAG: &str = "$equine_doc$";
const SHEET_DESCRIPTION: &str =
"Fiche anatomique officielle du cheval (import des données historiques, brouillon à valider)";
pub const DRAFT_CHANGE_NOTE: &str = "Révision de travail 4c (audit anatomique et ostéopathique) sur l’import des données historiques";
pub struct DraftSeedParams<'a> {
pub sheet_id: &'a str,
pub assets: &'a [AssetRecord],
/// `document.json` text, embedded verbatim in a dollar-quoted literal.
pub document_json: &'a str,
pub change_note: &'a str,
}
/// Only ever fed fixed values; doubled quotes keep it literal-safe.
fn sql_text(value: &str) -> String {
format!("'{}'", value.replace('\'', "''"))
}
fn header() -> String {
[
"-- Official equine anatomical sheet: working revision, DRAFT.",
"-- Generated by `pnpm import:equine` (packages/anatomy): do not edit by hand.",
"-- Idempotent and converging. Never publishes: the version stays in status",
"-- draft until a platform admin validates REVIEW-REPORT.md and publishes it.",
]
.join("\n")
}
fn sheet_insert(sheet_id: &str) -> String {
[
"insert into public.anatomical_sheets".to_owned(),
" (id, species_id, name, slug, description, creator_user_id, is_official, visibility)"
.to_owned(),
"values (".to_owned(),
format!(
" {}, 'equine', 'Faunosteo / Cheval', 'faunosteo-cheval',",
sql_text(sheet_id)
),
format!(" {},", sql_text(SHEET_DESCRIPTION)),
" null, true, 'private'".to_owned(),
")".to_owned(),
"on conflict (id) do nothing;".to_owned(),
]
.join("\n")
}
fn asset_inserts(sheet_id: &str, assets: &[AssetRecord]) -> String {
let rows: Vec<String> = assets
.iter()
.map(|a| {
format!(
" ({}, {}, {}, {}, 'image/svg+xml', {})",
sql_text(&a.svg_asset_id),
sql_text(sheet_id),
sql_text(&a.sha256),
a.byte_size,
sql_text(&a.file)
)
})
.collect();
[
"insert into public.anatomical_sheet_assets".to_owned(),
" (id, sheet_id, sha256, byte_size, content_type, file_name)".to_owned(),
"values".to_owned(),
rows.join(",\n"),
"on conflict (sheet_id, sha256) do nothing;".to_owned(),
]
.join("\n")
}
/// `trimEnd`: the JavaScript white space and line terminators.
fn trim_end_js(text: &str) -> &str {
text.trim_end_matches(is_js_space)
}
fn document_literal(document_json: &str) -> Result<String, PipelineError> {
if document_json.contains(DOCUMENT_DOLLAR_TAG) {
return Err(fail(format!(
"document JSON contains the dollar-quote tag {DOCUMENT_DOLLAR_TAG}"
)));
}
Ok(format!(
"{DOCUMENT_DOLLAR_TAG}{}{DOCUMENT_DOLLAR_TAG}::jsonb",
trim_end_js(document_json)
))
}
fn draft_insert(params: &DraftSeedParams<'_>, literal: &str) -> String {
let sheet = sql_text(params.sheet_id);
[
"select private.insert_sheet_draft(".to_owned(),
" null,".to_owned(),
format!(" {sheet},"),
format!(" {literal},"),
format!(" {}", sql_text(params.change_note)),
")".to_owned(),
"where not exists (".to_owned(),
format!(" select 1 from public.anatomical_sheet_versions where sheet_id = {sheet}"),
");".to_owned(),
]
.join("\n")
}
/// Only when the sheet's single version is a draft whose content differs; the document
/// literal is read once through the derived table.
fn draft_converge(params: &DraftSeedParams<'_>, literal: &str) -> String {
let sheet = sql_text(params.sheet_id);
[
"update public.anatomical_sheet_versions v".to_owned(),
format!(
"set content = d.content, change_note = {}",
sql_text(params.change_note)
),
format!("from (select {literal} as content) d"),
format!("where v.sheet_id = {sheet} and v.status = 'draft'"),
" and v.content <> d.content".to_owned(),
" and (select count(*) from public.anatomical_sheet_versions o".to_owned(),
format!(" where o.sheet_id = {sheet}) = 1;"),
]
.join("\n")
}
pub fn render_draft_seed_sql(params: &DraftSeedParams<'_>) -> Result<String, PipelineError> {
let literal = document_literal(params.document_json)?;
let parts = [
header(),
sheet_insert(params.sheet_id),
asset_inserts(params.sheet_id, params.assets),
draft_insert(params, &literal),
draft_converge(params, &literal),
];
Ok(format!("{}\n", parts.join("\n\n")))
}
+174
View File
@@ -0,0 +1,174 @@
//! Volumes of a sheet document (`packages/anatomy/src/curation/stats.ts`), compared before /
//! after curation in the CHANGELOG and shown for the baseline in the review report. Nodes
//! count finding categories too, as `countTree` does in the import report. Field order is the
//! TypeScript object order, so the serialised stats compare with `oracles/r5/pipeline.json`.
use serde::Serialize;
use serde_json::{Map, Value};
use super::records::js_string;
use super::targets::{FindingEntry, all_findings, at};
use crate::curation::PipelineError;
const ANNOTATION_KINDS: [&str; 3] = ["point", "zone", "detail_trigger"];
const REVIEW_STATUSES: [&str; 3] = ["inherited", "reviewed", "to_confirm"];
/// `countBy`: the listed keys first (zero when absent), then any other value met.
pub type Counts = Map<String, Value>;
#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct DocumentStats {
pub views: usize,
pub assets: usize,
pub annotations: usize,
pub annotations_by_kind: Counts,
pub annotation_categories: usize,
pub finding_zones: usize,
pub finding_categories: usize,
pub nodes: usize,
pub leaves: usize,
pub vet_referrals: usize,
pub descriptions: usize,
pub descriptions_fr_only: usize,
pub cited_nodes: usize,
pub nodes_by_review_status: Counts,
pub references: usize,
pub annotations_by_review_status: Counts,
pub regions_with_count_range: usize,
}
fn count_by<'a>(
items: impl Iterator<Item = &'a Value>,
keys: &[&str],
key: impl Fn(&Value) -> String,
) -> Counts {
let mut counts = Map::new();
for name in keys {
counts.insert((*name).to_owned(), Value::from(0));
}
for item in items {
let name = key(item);
let current = counts.get(&name).and_then(Value::as_u64).unwrap_or(0);
counts.insert(name, Value::from(current + 1));
}
counts
}
/// `Boolean(text.fr)`: a non-empty string, a non-zero number, true, a non-empty object.
fn truthy(value: Option<&Value>) -> bool {
match value {
None | Some(Value::Null) => false,
Some(Value::Bool(flag)) => *flag,
Some(Value::Number(number)) => number.as_f64().is_some_and(|n| n != 0.0),
Some(Value::String(text)) => !text.is_empty(),
Some(Value::Array(_) | Value::Object(_)) => true,
}
}
fn description(item: &Value) -> (bool, bool) {
let text = item.get("description");
(
truthy(text.and_then(|t| t.get("fr"))),
truthy(text.and_then(|t| t.get("en"))),
)
}
fn is_leaf(item: &Value) -> bool {
item.get("children")
.and_then(Value::as_array)
.is_none_or(Vec::is_empty)
}
fn is_cited(item: &Value) -> bool {
item.get("reference_ids")
.and_then(Value::as_array)
.is_some_and(|ids| !ids.is_empty())
}
/// `String(item.review_status ?? 'inherited')`.
fn review_status(item: &Value) -> String {
match item.get("review_status") {
None | Some(Value::Null) => "inherited".to_owned(),
Some(value) => js_string(Some(value)),
}
}
fn list<'a>(doc: &'a Value, key: &str) -> &'a [Value] {
doc.get(key)
.and_then(Value::as_array)
.map_or(&[], Vec::as_slice)
}
struct FindingStats {
categories: usize,
nodes: usize,
leaves: usize,
vet_referrals: usize,
descriptions: usize,
descriptions_fr_only: usize,
cited: usize,
by_review_status: Counts,
}
fn finding_stats(doc: &Value, entries: &[FindingEntry]) -> Result<FindingStats, PipelineError> {
let items: Vec<&Value> = entries
.iter()
.map(|entry| at(doc, &entry.path))
.collect::<Result<_, _>>()?;
let texts: Vec<(bool, bool)> = items.iter().map(|item| description(item)).collect();
Ok(FindingStats {
categories: entries.iter().filter(|entry| entry.is_category).count(),
nodes: items.len(),
leaves: items.iter().filter(|item| is_leaf(item)).count(),
vet_referrals: items
.iter()
.filter(|item| item.get("requires_vet_referral") == Some(&Value::Bool(true)))
.count(),
descriptions: texts.iter().filter(|(fr, en)| *fr || *en).count(),
descriptions_fr_only: texts.iter().filter(|(fr, en)| *fr && !*en).count(),
cited: items.iter().filter(|item| is_cited(item)).count(),
by_review_status: count_by(items.iter().copied(), &REVIEW_STATUSES, review_status),
})
}
pub fn document_stats(doc: &Value) -> Result<DocumentStats, PipelineError> {
let views = list(doc, "views");
let mut assets: Vec<&Value> = Vec::new();
for view in views {
let asset = view.get("svg_asset_id");
if truthy(asset) && !assets.contains(&asset.unwrap_or(&Value::Null)) {
assets.push(asset.unwrap_or(&Value::Null));
}
}
let annotations = list(doc, "annotations");
let findings = finding_stats(doc, &all_findings(doc)?)?;
let regions = doc
.get("vertebral_config")
.map(|config| list(config, "regions"))
.unwrap_or(&[]);
Ok(DocumentStats {
views: views.len(),
assets: assets.len(),
annotations: annotations.len(),
annotations_by_kind: count_by(annotations.iter(), &ANNOTATION_KINDS, |a| {
js_string(a.get("kind"))
}),
annotation_categories: list(doc, "categories").len(),
finding_zones: list(doc, "finding_zones").len(),
finding_categories: findings.categories,
nodes: findings.nodes,
leaves: findings.leaves,
vet_referrals: findings.vet_referrals,
descriptions: findings.descriptions,
descriptions_fr_only: findings.descriptions_fr_only,
cited_nodes: findings.cited,
nodes_by_review_status: findings.by_review_status,
references: list(doc, "references").len(),
annotations_by_review_status: count_by(annotations.iter(), &REVIEW_STATUSES, review_status),
regions_with_count_range: regions
.iter()
.filter(|region| truthy(region.get("count_range")))
.count(),
})
}
+414
View File
@@ -0,0 +1,414 @@
//! Target grammar of the changeset (`packages/anatomy/src/curation/targets.ts`), one string
//! per target:
//!
//! document | region:<code> | reference:<id> | category:<id> | view:<id>
//! annotation:<id> | zone:<zoneId> | finding:<zoneId>/<catId>[/<nodeId>…]
//!
//! A finding path walks ids from the zone down; ids are unique among siblings, so the path
//! names one category or node. Resolution works on the plain JSON form of the document
//! (`serde_json::Value`, insertion order kept): the changeset reaches any field without one
//! accessor per type, and the final strict parse checks the shape.
//!
//! The TypeScript held object references; here a located item is a path of keys and indices
//! into the document, resolved again at each use (`item_mut`, `siblings_mut`).
use serde_json::{Map, Value};
use crate::curation::PipelineError;
pub type Item = Map<String, Value>;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TargetKind {
Document,
Region,
Reference,
Category,
View,
Annotation,
Zone,
Finding,
}
impl TargetKind {
pub fn name(self) -> &'static str {
match self {
Self::Document => "document",
Self::Region => "region",
Self::Reference => "reference",
Self::Category => "category",
Self::View => "view",
Self::Annotation => "annotation",
Self::Zone => "zone",
Self::Finding => "finding",
}
}
fn collection(name: &str) -> Option<Self> {
Some(match name {
"region" => Self::Region,
"reference" => Self::Reference,
"category" => Self::Category,
"view" => Self::View,
"annotation" => Self::Annotation,
"zone" => Self::Zone,
"finding" => Self::Finding,
_ => return None,
})
}
/// Regions are keyed by their free code, everything else by a stable id.
pub fn key_field(self) -> &'static str {
if self == Self::Region { "code" } else { "id" }
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParsedTarget {
pub kind: TargetKind,
pub ids: Vec<String>,
}
/// One step into the document: an object key or an array index.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Step {
Key(&'static str),
Index(usize),
}
pub type Path = Vec<Step>;
/// A resolved target: the path of the list that holds the item (None: the document root) and
/// the item's index in it.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Located {
pub target: ParsedTarget,
pub list: Option<Path>,
pub index: usize,
}
impl Located {
pub fn item_path(&self) -> Path {
let mut path = self.list.clone().unwrap_or_default();
if self.list.is_some() {
path.push(Step::Index(self.index));
}
path
}
}
/// One finding category or node met while walking a zone's tree.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FindingEntry {
pub target: String,
pub path: Path,
pub is_category: bool,
}
pub fn fail(message: impl Into<String>) -> PipelineError {
PipelineError::Guard(message.into())
}
pub fn parse_target(target: &str) -> Result<ParsedTarget, PipelineError> {
if target == "document" {
return Ok(ParsedTarget {
kind: TargetKind::Document,
ids: Vec::new(),
});
}
let unknown = || fail(format!("unknown target \"{target}\""));
let colon = target.find(':').ok_or_else(unknown)?;
let (kind, rest) = (&target[..colon], &target[colon + 1..]);
if colon < 1 || rest.is_empty() {
return Err(unknown());
}
let kind = TargetKind::collection(kind).ok_or_else(unknown)?;
let ids: Vec<String> = if kind == TargetKind::Finding {
rest.split('/').map(str::to_owned).collect()
} else {
vec![rest.to_owned()]
};
if ids.iter().any(String::is_empty) || (kind == TargetKind::Finding && ids.len() < 2) {
return Err(fail(format!("malformed target \"{target}\"")));
}
Ok(ParsedTarget { kind, ids })
}
pub fn format_target(target: &ParsedTarget) -> String {
if target.kind == TargetKind::Document {
return "document".to_owned();
}
format!("{}:{}", target.kind.name(), target.ids.join("/"))
}
/// Path of the top-level list holding the items of a target kind.
pub fn top_collection_path(kind: TargetKind) -> Path {
match kind {
TargetKind::Region => vec![Step::Key("vertebral_config"), Step::Key("regions")],
TargetKind::Reference => vec![Step::Key("references")],
TargetKind::Category => vec![Step::Key("categories")],
TargetKind::View => vec![Step::Key("views")],
TargetKind::Annotation => vec![Step::Key("annotations")],
TargetKind::Zone | TargetKind::Finding | TargetKind::Document => {
vec![Step::Key("finding_zones")]
}
}
}
fn describe(step: &Step) -> String {
match step {
Step::Key(key) => (*key).to_owned(),
Step::Index(index) => index.to_string(),
}
}
/// The value at a path.
pub fn at<'a>(doc: &'a Value, path: &[Step]) -> Result<&'a Value, PipelineError> {
let mut value = doc;
for step in path {
value = match step {
Step::Key(key) => value.get(key),
Step::Index(index) => value.get(index),
}
.ok_or_else(|| fail(format!("document has no \"{}\"", describe(step))))?;
}
Ok(value)
}
pub fn at_mut<'a>(doc: &'a mut Value, path: &[Step]) -> Result<&'a mut Value, PipelineError> {
let mut value = doc;
for step in path {
value = match step {
Step::Key(key) => value.get_mut(key),
Step::Index(index) => value.get_mut(index),
}
.ok_or_else(|| fail(format!("document has no \"{}\"", describe(step))))?;
}
Ok(value)
}
/// `listAt`: the array at a path, or "document has no list".
pub fn list_at<'a>(doc: &'a Value, path: &[Step]) -> Result<&'a Vec<Value>, PipelineError> {
let last = path.last().map(describe).unwrap_or_default();
at(doc, path)?
.as_array()
.ok_or_else(|| fail(format!("document has no list \"{last}\"")))
}
pub fn list_at_mut<'a>(
doc: &'a mut Value,
path: &[Step],
) -> Result<&'a mut Vec<Value>, PipelineError> {
let last = path.last().map(describe).unwrap_or_default();
at_mut(doc, path)?
.as_array_mut()
.ok_or_else(|| fail(format!("document has no list \"{last}\"")))
}
pub fn item_at<'a>(doc: &'a Value, path: &[Step]) -> Result<&'a Item, PipelineError> {
at(doc, path)?
.as_object()
.ok_or_else(|| fail("target is not an object"))
}
pub fn item_at_mut<'a>(doc: &'a mut Value, path: &[Step]) -> Result<&'a mut Item, PipelineError> {
at_mut(doc, path)?
.as_object_mut()
.ok_or_else(|| fail("target is not an object"))
}
/// `entry[key] === id` on an element of a list.
pub fn has_key(entry: &Value, key: &str, id: &str) -> bool {
entry.get(key).and_then(Value::as_str) == Some(id)
}
/// `findIn`: the index of the sibling whose key is `id`.
fn find_in(list: &[Value], key: &str, id: &str, raw: &str) -> Result<usize, PipelineError> {
list.iter()
.position(|entry| has_key(entry, key, id))
.ok_or_else(|| fail(format!("unknown target \"{raw}\" (no \"{id}\")")))
}
fn with(path: &[Step], step: Step) -> Path {
let mut next = path.to_vec();
next.push(step);
next
}
/// `childrenOf`: children of a finding item; a lean leaf gets an empty list on demand (the key
/// is appended to the item, as the TypeScript did).
pub fn children_path(doc: &mut Value, item: &[Step]) -> Result<Path, PipelineError> {
let holder = item_at_mut(doc, item)?;
if !holder.get("children").is_some_and(Value::is_array) {
holder.insert("children".into(), Value::Array(Vec::new()));
}
Ok(with(item, Step::Key("children")))
}
fn locate_finding(doc: &Value, target: ParsedTarget, raw: &str) -> Result<Located, PipelineError> {
let zones = top_collection_path(TargetKind::Zone);
let zone = find_in(list_at(doc, &zones)?, "id", &target.ids[0], raw)?;
let mut list = with(&with(&zones, Step::Index(zone)), Step::Key("categories"));
let mut index = find_in(list_at(doc, &list)?, "id", &target.ids[1], raw)?;
for id in &target.ids[2..] {
list = with(&with(&list, Step::Index(index)), Step::Key("children"));
let siblings = at(doc, &list).ok().and_then(Value::as_array);
index = find_in(siblings.map_or(&[][..], Vec::as_slice), "id", id, raw)?;
}
Ok(Located {
target,
list: Some(list),
index,
})
}
/// Resolves a target string or refuses with "unknown target".
pub fn locate(doc: &Value, raw: &str) -> Result<Located, PipelineError> {
let target = parse_target(raw)?;
if target.kind == TargetKind::Document {
return Ok(Located {
target,
list: None,
index: 0,
});
}
if target.kind == TargetKind::Finding {
return locate_finding(doc, target, raw);
}
let list = top_collection_path(target.kind);
let index = find_in(
list_at(doc, &list)?,
target.kind.key_field(),
&target.ids[0],
raw,
)?;
Ok(Located {
target,
list: Some(list),
index,
})
}
/// `String(child.id)` of a finding item.
pub fn id_text(item: &Value) -> String {
super::records::js_string(item.get("id"))
}
/// The item and every descendant node, depth first, in document order.
fn walk(doc: &Value, path: &Path, target: &str, depth: usize, out: &mut Vec<FindingEntry>) {
out.push(FindingEntry {
target: target.to_owned(),
path: path.clone(),
is_category: depth == 0,
});
let children = at(doc, path)
.ok()
.and_then(|item| item.get("children"))
.and_then(Value::as_array);
let Some(children) = children else {
return;
};
let list = with(path, Step::Key("children"));
for (index, child) in children.iter().enumerate() {
let next = with(&list, Step::Index(index));
walk(
doc,
&next,
&format!("{target}/{}", id_text(child)),
depth + 1,
out,
);
}
}
/// Every finding category and node of a zone, depth first.
pub fn zone_findings(doc: &Value, zone: &Path) -> Result<Vec<FindingEntry>, PipelineError> {
let zone_id = id_text(at(doc, zone)?);
let categories = with(zone, Step::Key("categories"));
let mut out = Vec::new();
for (index, category) in list_at(doc, &categories)?.iter().enumerate() {
let target = format!("finding:{zone_id}/{}", id_text(category));
walk(
doc,
&with(&categories, Step::Index(index)),
&target,
0,
&mut out,
);
}
Ok(out)
}
/// Every finding category and node of the document.
pub fn all_findings(doc: &Value) -> Result<Vec<FindingEntry>, PipelineError> {
let zones = top_collection_path(TargetKind::Zone);
let mut out = Vec::new();
for index in 0..list_at(doc, &zones)?.len() {
out.extend(zone_findings(doc, &with(&zones, Step::Index(index)))?);
}
Ok(out)
}
/// Finding items an op touches. A finding target alone (self) or with its descendants
/// (subtree); a zone target stands for its categories alone (self) or its whole tree
/// (subtree): a zone carries no citation.
pub fn finding_scope(
doc: &Value,
located: &Located,
subtree: bool,
) -> Result<Vec<FindingEntry>, PipelineError> {
let item = located.item_path();
match located.target.kind {
TargetKind::Zone => {
let entries = zone_findings(doc, &item)?;
Ok(if subtree {
entries
} else {
entries.into_iter().filter(|e| e.is_category).collect()
})
}
TargetKind::Finding => {
let depth = usize::from(located.target.ids.len() != 2);
let mut entries = Vec::new();
walk(
doc,
&item,
&format_target(&located.target),
depth,
&mut entries,
);
if !subtree {
entries.truncate(1);
}
Ok(entries)
}
_ => Err(fail(format!(
"\"{}\" is not a finding or a zone",
format_target(&located.target)
))),
}
}
/// The list an add or a move inserts into under a zone or a finding.
pub fn child_list(doc: &mut Value, parent: &Located) -> Result<Path, PipelineError> {
match parent.target.kind {
TargetKind::Zone => {
let path = with(&parent.item_path(), Step::Key("categories"));
list_at(doc, &path)?;
Ok(path)
}
TargetKind::Finding => children_path(doc, &parent.item_path()),
_ => Err(fail(format!(
"\"{}\" holds no findings",
format_target(&parent.target)
))),
}
}
/// Target prefix of the items of `child_list(parent)`.
pub fn child_prefix(parent: &Located) -> String {
if parent.target.kind == TargetKind::Zone {
return format!("finding:{}/", parent.target.ids[0]);
}
format!("{}/", format_target(&parent.target))
}
+6 -4
View File
@@ -1,13 +1,15 @@
//! The anatomical pipeline (R5, plan 2026-10-22): the legacy importer, and the formatting of
//! the generated seed files, ported from `packages/anatomy` with its outputs frozen as
//! oracles (`crates/core/tests/oracles/r5`). Inputs are bytes and texts, outputs are texts;
//! the `import-equine` binary owns the file system.
//! The anatomical pipeline (R5, plan 2026-10-22): the legacy importer, the curated draft
//! (changeset applier, migration map, stats, seed SQL) and the formatting of the generated seed
//! files, ported from `packages/anatomy` with its outputs frozen as oracles
//! (`crates/core/tests/oracles/r5`). Inputs are bytes and texts, outputs are texts; the
//! `import-equine` binary owns the file system.
//!
//! JavaScript behaviours the TypeScript pipeline depended on are reproduced here and pinned
//! by `oracles/r5/js-semantics.json`: object key order, `Number()`, `toLowerCase` after NFD,
//! UTF-16 ordering; the Prettier layout of the JSON and Markdown files by
//! `oracles/r5/prettier-json.json` and `markdown-tables.json`.
pub mod draft;
pub mod ids;
pub mod legacy;
pub mod markdown;
+414
View File
@@ -0,0 +1,414 @@
//! The Rust draft pipeline against the TypeScript (R5.3): the R5.3 files of the committed
//! `draft/` folder byte for byte, the draft section frozen in `oracles/r5/pipeline.json` (46
//! changes, 825 records, stats, migration stats), two runs byte-equal, the 612 legacy leaves
//! migrated through the core's reader on the produced map, and the synthetic cases of
//! `oracles/r5/curation-synthetic.json` (the generic contract, refusals included). Every
//! expectation was written by the TypeScript pipeline.
#![allow(clippy::disallowed_methods, clippy::unwrap_used, clippy::expect_used)]
mod archive_support;
mod common;
use faunosteo_core::curation::draft::migration_map::{
MigrationMapInput, SheetRef, build_report_migration_map,
};
use faunosteo_core::curation::draft::records::ChangeRecord;
use faunosteo_core::curation::draft::schema::Changeset;
use faunosteo_core::curation::draft::seed::{DraftSeedParams, render_draft_seed_sql};
use faunosteo_core::curation::draft::stats::document_stats;
use faunosteo_core::curation::draft::{
DraftInput, DraftResult, apply_changeset, build_equine_draft, parse_changeset,
parse_references, parse_view_box, render_draft_artifacts,
};
use faunosteo_core::curation::ids::sha256_hex;
use faunosteo_core::curation::legacy::types::AssetRecord;
use faunosteo_core::curation::legacy::{
EquineLegacyInput, HEAD_VIEW_ID, ImportResult, SvgSource, import_equine_legacy,
};
use faunosteo_core::curation::prettier_json::format_json;
use faunosteo_core::domain::report::ReportMigrationMap;
use faunosteo_core::domain::report::read::read_report_document;
use faunosteo_core::domain::sheet::AnatomicalSheetDocument;
use faunosteo_core::json::to_js_compact;
use serde_json::{Map, Value, json};
const SOURCES: &str = "supabase/seed/anatomical-sheets/equine/legacy-sources";
const CURATION: &str = "supabase/seed/anatomical-sheets/equine/curation";
const DRAFT: &str = "supabase/seed/anatomical-sheets/equine/draft";
/// The files R5.3 produces; the review drawings and the Markdown reports are R5.4.
const R5_3_FILES: [&str; 5] = [
"document.json",
"assets.json",
"sheet-ref.json",
"report-migration-map.json",
"equine_draft.sql",
];
fn bytes(relative: &str) -> Vec<u8> {
std::fs::read(common::repo_path(relative)).unwrap()
}
fn legacy_result() -> ImportResult {
let svg = |name: &str| SvgSource {
file_name: name.to_owned(),
bytes: bytes(&format!("{SOURCES}/{name}")),
};
import_equine_legacy(&EquineLegacyInput {
main_svg: svg("Horse.svg"),
head_svg: svg("Horse_head.svg"),
catalog: bytes(&format!("{SOURCES}/pathologies.json")),
vertebrae: bytes(&format!("{SOURCES}/vertebrae.ts")),
})
.unwrap()
}
fn equine_draft() -> DraftResult {
let import_result = legacy_result();
let references =
parse_references(&common::repo_file(&format!("{CURATION}/references.json"))).unwrap();
let changeset =
parse_changeset(&common::repo_file(&format!("{CURATION}/changes.json"))).unwrap();
let head = import_result
.view_boxes
.get(HEAD_VIEW_ID)
.and_then(Value::as_str);
build_equine_draft(&DraftInput {
import_result: &import_result,
cleaned_svg: &bytes(&format!("{DRAFT}/assets/horse.svg")),
head_view_box: parse_view_box(head).unwrap(),
references: &references,
changes: &changeset.changes,
})
.unwrap()
}
fn oracle(name: &str) -> Value {
serde_json::from_str(&common::repo_file(&format!(
"crates/core/tests/oracles/r5/{name}"
)))
.unwrap()
}
fn assert_same(actual: &Value, expected: &Value, what: &str) {
assert_eq!(
to_js_compact(actual).unwrap(),
to_js_compact(expected).unwrap(),
"{what}"
);
}
fn value<T: serde::Serialize + ?Sized>(value: &T) -> Value {
serde_json::to_value(value).unwrap()
}
#[test]
fn the_r5_3_files_of_the_committed_draft_folder_are_reproduced_byte_for_byte() {
let draft = equine_draft();
let files = render_draft_artifacts(&draft).unwrap();
let names: Vec<&str> = files.iter().map(|(name, _)| name.as_str()).collect();
assert_eq!(names, R5_3_FILES);
for (name, text) in &files {
assert!(
*text == common::repo_file(&format!("{DRAFT}/{name}")),
"{name} differs"
);
}
let asset = &draft.assets[0];
let frozen = bytes(&format!("{DRAFT}/assets/{}", asset.file));
assert_eq!(sha256_hex(&frozen), asset.sha256);
assert_eq!(frozen.len(), asset.byte_size);
}
#[test]
fn the_document_fingerprint_is_the_historical_value() {
let files = render_draft_artifacts(&equine_draft()).unwrap();
let document = &files[0].1;
let reference: Value = serde_json::from_str(&files[2].1).unwrap();
assert_eq!(
reference["document_sha256"],
sha256_hex(document.as_bytes())
);
assert_eq!(
reference["document_sha256"],
"27c7383c445c73dc2001ebf4794a3ea63d416910bbbaa0d7848dd720de7c2660"
);
}
#[test]
fn the_draft_section_of_the_frozen_run_is_reproduced() {
let draft = equine_draft();
let frozen = oracle("pipeline.json");
let expected = frozen["draft"].as_object().unwrap();
assert_eq!(draft.sheet_id, expected["sheetId"].as_str().unwrap());
assert_eq!(draft.changes.len(), 46);
assert_same(&value(&draft.changes), &expected["changes"], "changes");
assert_same(&value(&draft.assets), &expected["assets"], "assets");
assert_same(
&value(&draft.stats_before),
&expected["statsBefore"],
"statsBefore",
);
assert_same(
&value(&draft.stats_after),
&expected["statsAfter"],
"statsAfter",
);
assert_same(&value(&draft.issues), &expected["issues"], "issues");
assert_eq!(draft.records.len(), 825);
let records = value(&draft.records);
for (index, record) in expected["records"].as_array().unwrap().iter().enumerate() {
assert_same(&records[index], record, &format!("record {index}"));
}
let document_json = format_json(&draft.document).unwrap();
let reference = SheetRef {
sheet_id: draft.sheet_id.clone(),
version_id: None,
document_sha256: sha256_hex(document_json.as_bytes()),
};
let (_, stats) =
faunosteo_core::curation::draft::pipeline::draft_migration_map(&draft, &reference).unwrap();
assert_same(
&value(&stats),
&expected["migrationStats"],
"migrationStats",
);
}
#[test]
fn two_runs_emit_the_same_bytes() {
let first = render_draft_artifacts(&equine_draft()).unwrap();
let second = render_draft_artifacts(&equine_draft()).unwrap();
assert_eq!(first, second);
}
fn node_is_leaf(
document: &AnatomicalSheetDocument,
zone_id: &str,
path: &[String],
) -> Option<bool> {
let zone = document.finding_zones.iter().find(|z| z.id == zone_id)?;
let category = zone.categories.iter().find(|c| c.id == path[0])?;
let mut children = &category.children;
let mut leaf = children.is_empty();
for segment in &path[1..] {
let node = children.iter().find(|n| n.id == *segment)?;
children = &node.children;
leaf = children.is_empty();
}
Some(leaf)
}
#[test]
fn the_612_legacy_leaves_migrate_through_the_produced_map_as_the_typescript_measured() {
let draft = equine_draft();
let files = render_draft_artifacts(&draft).unwrap();
let map: ReportMigrationMap = serde_json::from_str(&files[3].1).unwrap();
let document: AnatomicalSheetDocument = serde_json::from_value(draft.document.clone()).unwrap();
let leaves = archive_support::legacy_leaves();
assert_eq!(leaves.len(), 612);
let outcome =
read_report_document(&archive_support::leaves_as_v2_report(&leaves), &map).unwrap();
let findings: Vec<_> = outcome
.report
.observations
.iter()
.flat_map(|o| o.findings.iter())
.collect();
assert_eq!(findings.len(), 612);
let (mut identical, mut moved, mut removed) = (0, 0, 0);
let mut flagged = Vec::new();
for (leaf, finding) in leaves.iter().zip(&findings) {
let same = finding.finding_zone_id == leaf.zone_id && finding.path == leaf.path;
if finding.unresolved.is_some() {
removed += 1;
flagged.push(format!(
"{}/{}",
finding.finding_zone_id,
finding.path.join("/")
));
assert!(node_is_leaf(&document, &finding.finding_zone_id, &finding.path).is_none());
} else {
if same {
identical += 1
} else {
moved += 1
}
assert_eq!(
node_is_leaf(&document, &finding.finding_zone_id, &finding.path),
Some(true),
"{}/{}",
finding.finding_zone_id,
finding.path.join("/")
);
}
assert_eq!(finding.label_fr_snapshot, leaf.fr);
}
assert_eq!((identical, moved, removed), (442, 141, 29));
let nulls: Vec<String> = leaves
.iter()
.map(|leaf| format!("{}/{}", leaf.zone_id, leaf.path.join("/")))
.filter(|key| map.finding_paths.get(key).is_some_and(Option::is_none))
.collect();
assert_eq!(flagged, nulls);
}
// ---------------------------------------------------------------------------------------------
// Synthetic cases written by the TypeScript (`oracles/r5/curation-synthetic.json`).
fn synthetic_records(records: &[ChangeRecord]) -> Value {
value(records)
}
fn synthetic_map(
baseline: &Value,
result_document: &Value,
records: &[ChangeRecord],
) -> (Value, Value) {
let legacy_id_map: Map<String, Value> = json!({
"views": { "head": "head" },
"annotations": { "legacy-hip": "hip", "legacy-neck": "neck" },
"categories": {},
"finding_nodes": {},
"assets": {},
})
.as_object()
.unwrap()
.clone();
let sheet = SheetRef {
sheet_id: "11111111-1111-5111-8111-111111111111".to_owned(),
version_id: None,
document_sha256: "0".repeat(64),
};
let (map, stats) = build_report_migration_map(&MigrationMapInput {
baseline,
final_document: result_document,
legacy_id_map: &legacy_id_map,
records,
sheet: &sheet,
main_view_id: "main",
})
.unwrap();
(map, value(&stats))
}
fn synthetic_seed(document_json: &str) -> String {
let assets = [AssetRecord {
file: "mini.svg".to_owned(),
sha256: "ab".repeat(32),
byte_size: 42,
svg_asset_id: "3f6c1b52-8a4e-4d17-9b0c-5e2a7d91c4f8".to_owned(),
}];
render_draft_seed_sql(&DraftSeedParams {
sheet_id: "11111111-1111-5111-8111-111111111111",
assets: &assets,
document_json,
change_note: "Essai 'quoted'",
})
.unwrap()
}
/// Cases the TypeScript accepted and the port refuses, declared in RUST.md §11.12.
const DECLARED_REFUSALS: [&str; 1] = ["declared-set-without-value-drops-key"];
#[test]
fn the_synthetic_cases_match_the_typescript_or_are_refused_alike() {
let frozen = oracle("curation-synthetic.json");
let baseline = &frozen["baseline"];
let references = parse_references(&to_js_compact(&frozen["references_file"]).unwrap()).unwrap();
let (mut compared, mut refused) = (0, 0);
for case in frozen["cases"].as_array().unwrap() {
let name = case["name"].as_str().unwrap();
let changeset: Result<Changeset, _> =
parse_changeset(&to_js_compact(&case["changes_file"]).unwrap());
let outcome =
changeset.and_then(|set| apply_changeset(baseline, &references, &set.changes));
if !case["ok"].as_bool().unwrap() || DECLARED_REFUSALS.contains(&name) {
assert!(
outcome.is_err(),
"{name}: the TypeScript refused, the port accepted"
);
refused += 1;
continue;
}
compared += 1;
let result = outcome.unwrap_or_else(|error| panic!("{name}: {error}"));
let document_json = format_json(&result.document).unwrap();
assert_eq!(
document_json,
case["document"].as_str().unwrap(),
"{name} document"
);
assert_same(
&synthetic_records(&result.records),
&case["records"],
&format!("{name} records"),
);
assert_same(
&value(&document_stats(&result.document).unwrap()),
&case["stats"],
&format!("{name} stats"),
);
let (map, stats) = synthetic_map(baseline, &result.document, &result.records);
assert_eq!(
format_json(&map).unwrap(),
case["migration_map"].as_str().unwrap(),
"{name} map"
);
assert_same(
&stats,
&case["migration_stats"],
&format!("{name} migration stats"),
);
assert_eq!(
synthetic_seed(&document_json),
case["seed_sql"].as_str().unwrap(),
"{name} seed"
);
}
assert_eq!((compared, refused), (9, 49));
let reference_cases = frozen["reference_cases"].as_array().unwrap();
assert_eq!(reference_cases.len(), 5);
for case in reference_cases {
let name = case["name"].as_str().unwrap();
let outcome = parse_references(&to_js_compact(&case["file"]).unwrap());
assert!(
outcome.is_err(),
"{name}: the TypeScript refused, the port accepted"
);
}
}
// ---------------------------------------------------------------------------------------------
// Parse semantics the synthetic oracle cannot carry (a JSON value holds no repeated key).
const ONE_CHANGE: &str = r#"{"changes":[{"id":"CHG-001","level":"A","kind":"technical",
"title":{"fr":"Essai","en":"Test"},"rationale":"first","reference_ids":[],
"ops":[{"op":"remove","target":"finding:hip/hip-dysfunctions/mobility/flexion"}]}]}"#;
#[test]
fn a_repeated_key_keeps_its_last_value_as_json_parse_does() {
let repeated = ONE_CHANGE.replace(
r#""rationale":"first","#,
r#""rationale":"first","rationale":"last","#,
);
let expected = ONE_CHANGE.replace(r#""rationale":"first","#, r#""rationale":"last","#);
assert_eq!(
parse_changeset(&repeated).unwrap(),
parse_changeset(&expected).unwrap()
);
let references = r#"{"references":[{"id":"atlas","type":"book","citation":"a",
"citation":"b","url":null,"doi":null}]}"#;
assert_eq!(parse_references(references).unwrap()[0].citation, "b");
}
#[test]
fn a_reserved_touch_mark_key_in_an_authored_value_is_refused() {
let text = ONE_CHANGE.replace(
r#"{"op":"remove","target":"finding:hip/hip-dysfunctions/mobility/flexion"}"#,
r#"{"op":"set","target":"finding:hip/hip-dysfunctions/mobility","field":"name",
"value":{"fr":"a","en":"a","\u0000touched":0}}"#,
);
let error = parse_changeset(&text).unwrap_err();
assert!(error.to_string().contains("reserved key"), "{error}");
}
@@ -0,0 +1 @@
{"baseline":{"schema_version":2,"species_id":"equine","name":{"fr":"Mini","en":"Mini"},"description":{},"scope_disclaimer":{},"attribution":{"authors":[],"license":null,"notes":null},"vertebral_config":{"regions":[{"code":"C","name":{"fr":"Cervicales","en":"Cervical"},"count":7},{"code":"T","name":{"fr":"Thoraciques","en":"Thoracic"},"count":18}]},"references":[],"categories":[{"id":"joints","name":{"fr":"Articulations","en":"Joints"},"color":null},{"id":"bones","name":{"fr":"Os","en":"Bones"},"color":null}],"views":[{"id":"main","kind":"main","name":{"fr":"Vue","en":"View"},"svg_asset_id":"3f6c1b52-8a4e-4d17-9b0c-5e2a7d91c4f8","view_box":null},{"id":"head","kind":"detail","parent_view_id":"main","name":{"fr":"Tête","en":"Head"},"svg_asset_id":null,"view_box":{"min_x":0,"min_y":0,"width":10,"height":10}}],"annotations":[{"id":"hip","kind":"point","view_id":"main","category_id":"joints","laterality":"bilateral","geometry":{"type":"circle","cx":1,"cy":2,"r":3},"name":{"fr":"Hanche","en":"Hip"},"finding_zone_id":"hip","vertebral_region_code":null},{"id":"neck","kind":"zone","view_id":"main","category_id":"bones","laterality":"unpaired","geometry":null,"name":{"fr":"Cou","en":"Neck"},"finding_zone_id":"neck","vertebral_region_code":"C"},{"id":"head-trigger","kind":"detail_trigger","view_id":"main","category_id":"bones","laterality":"unpaired","geometry":null,"name":null,"opens_view_id":"head"}],"finding_zones":[{"id":"hip","name":{"fr":"Hanche","en":"Hip"},"categories":[{"id":"hip-dysfunctions","kind":"dysfunction","name":{"fr":"Dysfonctions","en":"Dysfunctions"},"children":[{"id":"mobility","name":{"fr":"mobility fr","en":"mobility en"},"children":[{"id":"flexion","name":{"fr":"flexion fr","en":"flexion en"}},{"id":"extension","name":{"fr":"extension fr","en":"extension en"}}]},{"id":"pain","name":{"fr":"pain fr","en":"pain en"}}]},{"id":"anamnesis","kind":"anamnesis","name":{"fr":"Anamnèse","en":"Anamnesis"},"children":[{"id":"trauma","name":{"fr":"trauma fr","en":"trauma en"}}]}]},{"id":"neck","name":{"fr":"Cou","en":"Neck"},"categories":[{"id":"neck-dysfunctions","kind":"dysfunction","name":{"fr":"Dysfonctions","en":"Dysfunctions"},"children":[{"id":"stiffness","name":{"fr":"stiffness fr","en":"stiffness en"}},{"id":"vet","name":{"fr":"vet fr","en":"vet en"},"requires_vet_referral":true}]}]}]},"references_file":{"references":[{"id":"atlas","type":"book","citation":"Atlas of anatomy","url":null,"doi":null},{"id":"paper","type":"journal","citation":"A paper","url":null,"doi":"10.1/x"}]},"cases":[{"name":"set-fields","changes_file":{"changes":[{"id":"CHG-001","level":"A","kind":"technical","title":{"fr":"Essai","en":"Test"},"rationale":"Essai","reference_ids":[],"ops":[{"op":"set","target":"region:C","field":"count_range","value":{"min":6,"max":8}},{"op":"set","target":"document","field":"name.en","value":"Mini sheet"},{"op":"set","target":"document","field":"scope_disclaimer.fr","value":"Périmètre"},{"op":"set","target":"annotation:hip","field":"geometry","value":{"type":"circle","cx":1.5,"cy":2,"r":0.25}},{"op":"set","target":"annotation:neck","field":"color","value":"#aabbcc"},{"op":"set","target":"finding:hip/hip-dysfunctions/mobility/flexion","field":"description.en","value":"Flexion described"},{"op":"set","target":"finding:hip/anamnesis","field":"name.fr","value":"Anamnèse complète"},{"op":"set","target":"zone:neck","field":"name","value":{"fr":"Encolure","en":"Neck"}},{"op":"set","target":"view:head","field":"name.en","value":"Skull"},{"op":"set","target":"category:bones","field":"color","value":"#112233"},{"op":"set","target":"reference:paper","field":"doi","value":"10.1/y"},{"op":"set","target":"finding:hip/hip-dysfunctions/mobility/flexion","field":"review_status","value":"reviewed"}]}]},"ok":true,"document":"{\n \"schema_version\": 2,\n \"species_id\": \"equine\",\n \"name\": {\n \"fr\": \"Mini\",\n \"en\": \"Mini sheet\"\n },\n \"description\": {},\n \"scope_disclaimer\": {\n \"fr\": \"Périmètre\"\n },\n \"attribution\": {\n \"authors\": [],\n \"license\": null,\n \"notes\": null\n },\n \"vertebral_config\": {\n \"regions\": [\n {\n \"code\": \"C\",\n \"name\": {\n \"fr\": \"Cervicales\",\n \"en\": \"Cervical\"\n },\n \"count\": 7,\n \"count_range\": {\n \"min\": 6,\n \"max\": 8\n }\n },\n {\n \"code\": \"T\",\n \"name\": {\n \"fr\": \"Thoraciques\",\n \"en\": \"Thoracic\"\n },\n \"count\": 18\n }\n ]\n },\n \"references\": [\n {\n \"id\": \"atlas\",\n \"type\": \"book\",\n \"citation\": \"Atlas of anatomy\",\n \"url\": null,\n \"doi\": null\n },\n {\n \"id\": \"paper\",\n \"type\": \"journal\",\n \"citation\": \"A paper\",\n \"url\": null,\n \"doi\": \"10.1/y\"\n }\n ],\n \"categories\": [\n {\n \"id\": \"joints\",\n \"name\": {\n Line truncated
+1
View File
@@ -26,6 +26,7 @@
"check:all": "bash scripts/check-all.sh",
"report:compare": "bash scripts/report-compare.sh",
"import:equine": "pnpm --filter @faunosteo/anatomy import:equine",
"r5:import-equine": "bash scripts/import-equine.sh",
"supabase": "bash scripts/supabase.sh",
"db:start": "bash scripts/supabase.sh start",
"db:stop": "bash scripts/supabase.sh stop",
@@ -0,0 +1,901 @@
/**
* R5.3 freeze (owner brief 2026-10-22 §7, differential tests): the TypeScript changeset applier,
* migration map, stats and seed SQL on synthetic changesets over the mini sheet, so the Rust
* port is held to the generic contract and not only to the equine changeset. Each case is a
* raw `changes.json` text; the oracle records the formatted document, the records, the stats,
* the migration map (replayed on a fixed legacy id map) and the seed SQL, or the refusal.
* `MAKE_R5_ORACLES=1` writes crates/core/tests/oracles/r5/curation-synthetic.json.
*/
import { mkdirSync, readFileSync, writeFileSync } from 'node:fs'
import { dirname, resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
import { describe, expect, it } from 'vitest'
import {
applyChangeset,
buildReportMigrationMap,
ChangesetSchema,
documentStats,
ReferencesFileSchema,
renderDraftSeedSql,
} from '../src'
import { formatJson } from '../src/format'
import { MINI_REFERENCES, miniSheet } from './fixtures/mini-sheet'
const HERE = dirname(fileURLToPath(import.meta.url))
const OUT = resolve(HERE, '../../../crates/core/tests/oracles/r5')
const MAKE = process.env.MAKE_R5_ORACLES === '1'
type Json = Record<string, unknown>
interface Case {
name: string
changes_file: Json
}
const REFERENCES_FILE = { references: MINI_REFERENCES }
const LEGACY_ID_MAP = {
views: { head: 'head' },
annotations: { 'legacy-hip': 'hip', 'legacy-neck': 'neck' },
categories: {},
finding_nodes: {},
assets: {},
}
const SHEET = {
sheetId: '11111111-1111-5111-8111-111111111111',
versionId: null,
documentSha256: '0'.repeat(64),
}
const SEED_ASSETS = [
{
file: 'mini.svg',
sha256: 'ab'.repeat(32),
byte_size: 42,
svg_asset_id: '3f6c1b52-8a4e-4d17-9b0c-5e2a7d91c4f8',
},
]
const MOBILITY = 'finding:hip/hip-dysfunctions/mobility'
function change(ops: unknown[], extra: Json = {}): Json {
return {
id: 'CHG-001',
level: 'A',
kind: 'technical',
title: { fr: 'Essai', en: 'Test' },
rationale: 'Essai',
reference_ids: [],
...extra,
ops,
}
}
function one(ops: unknown[], extra: Json = {}): Json {
return { changes: [change(ops, extra)] }
}
function leaf(id: string, extra: Json = {}): Json {
return { id, name: { fr: `${id} fr`, en: `${id} en` }, ...extra }
}
const ACCEPTED: Case[] = [
{
name: 'set-fields',
changes_file: one([
{
op: 'set',
target: 'region:C',
field: 'count_range',
value: { min: 6, max: 8 },
},
{ op: 'set', target: 'document', field: 'name.en', value: 'Mini sheet' },
{
op: 'set',
target: 'document',
field: 'scope_disclaimer.fr',
value: 'Périmètre',
},
{
op: 'set',
target: 'annotation:hip',
field: 'geometry',
value: { type: 'circle', cx: 1.5, cy: 2, r: 0.25 },
},
{
op: 'set',
target: 'annotation:neck',
field: 'color',
value: '#aabbcc',
},
{
op: 'set',
target: `${MOBILITY}/flexion`,
field: 'description.en',
value: 'Flexion described',
},
{
op: 'set',
target: 'finding:hip/anamnesis',
field: 'name.fr',
value: 'Anamnèse complète',
},
{
op: 'set',
target: 'zone:neck',
field: 'name',
value: { fr: 'Encolure', en: 'Neck' },
},
{ op: 'set', target: 'view:head', field: 'name.en', value: 'Skull' },
{ op: 'set', target: 'category:bones', field: 'color', value: '#112233' },
{ op: 'set', target: 'reference:paper', field: 'doi', value: '10.1/y' },
{
op: 'set',
target: `${MOBILITY}/flexion`,
field: 'review_status',
value: 'reviewed',
},
]),
},
{
name: 'subtree-sets',
changes_file: one([
{
op: 'set',
target: 'finding:hip/hip-dysfunctions',
field: 'review_status',
value: 'reviewed',
scope: 'subtree',
},
{
op: 'set',
target: 'finding:hip/hip-dysfunctions',
field: 'kind',
value: 'custom',
scope: 'subtree',
},
{
op: 'set',
target: MOBILITY,
field: 'description',
value: { fr: 'Même texte' },
scope: 'subtree',
},
{
op: 'set',
target: `${MOBILITY}/flexion`,
field: 'description',
value: { fr: 'Même texte' },
},
{
op: 'set',
target: MOBILITY,
field: 'description',
value: { fr: 'Même texte' },
scope: 'subtree',
},
]),
},
{
name: 'renames-cascade',
changes_file: one([
{ op: 'cite', target: MOBILITY, reference_ids: ['atlas', 'paper'] },
{
op: 'cite',
target: 'finding:neck/neck-dysfunctions/vet',
reference_ids: ['atlas'],
},
{ op: 'rename', target: 'zone:hip', id: 'hip-joint' },
{ op: 'rename', target: 'region:C', id: 'CE' },
{ op: 'rename', target: 'view:main', id: 'principal' },
{ op: 'rename', target: 'view:head', id: 'skull' },
{ op: 'rename', target: 'category:joints', id: 'articulations' },
{ op: 'rename', target: 'reference:atlas', id: 'atlas-2' },
{
op: 'rename',
target: 'finding:hip-joint/hip-dysfunctions/mobility',
id: 'motion',
},
{ op: 'rename', target: 'annotation:hip', id: 'coxofemoral' },
]),
},
{
name: 'add-remove-move',
changes_file: one([
{ op: 'add', target: `${MOBILITY}/flexion`, value: leaf('deep-flexion') },
{ op: 'add', target: MOBILITY, value: leaf('rotation'), position: 0 },
{
op: 'add',
target: MOBILITY,
value: leaf('abduction'),
position: { after: 'flexion' },
},
{
op: 'add',
target: MOBILITY,
value: leaf('adduction'),
position: { before: 'extension' },
},
{
op: 'add',
target: 'finding:hip/anamnesis',
value: leaf('surgery'),
position: 1,
},
{
op: 'add',
target: 'zone:neck',
value: {
id: 'neck-anamnesis',
kind: 'anamnesis',
name: { fr: 'Anamnèse', en: 'Anamnesis' },
children: [leaf('fall')],
},
},
{
op: 'add',
target: 'document',
field: 'references',
value: {
id: 'web',
type: 'web',
citation: 'A page',
url: 'https://example.org/a',
doi: null,
},
},
{
op: 'add',
target: 'document',
field: 'finding_zones',
value: {
id: 'carpus',
name: { fr: 'Carpe', en: 'Carpus' },
categories: [
{
id: 'carpus-dysfunctions',
kind: 'dysfunction',
name: { fr: 'Dysfonctions', en: 'Dysfunctions' },
children: [leaf('effusion')],
},
],
},
},
{
op: 'add',
target: 'document',
field: 'annotations',
value: {
id: 'carpus',
kind: 'point',
view_id: 'main',
category_id: 'joints',
laterality: 'bilateral',
geometry: { type: 'circle', cx: 3, cy: 4, r: 1 },
name: { fr: 'Carpe', en: 'Carpus' },
finding_zone_id: 'carpus',
vertebral_region_code: null,
},
},
{
op: 'add',
target: 'document',
field: 'regions',
value: { code: 'L', name: { fr: 'Lombaires', en: 'Lumbar' }, count: 6 },
position: { after: 'T' },
},
{
op: 'add',
target: 'document',
field: 'categories',
value: {
id: 'muscles',
name: { fr: 'Muscles', en: 'Muscles' },
color: '#ff0000',
},
position: 0,
},
{ op: 'remove', target: `${MOBILITY}/extension` },
{
op: 'move',
target: `${MOBILITY}/rotation`,
to: `${MOBILITY}/flexion`,
position: 0,
},
{
op: 'move',
target: 'finding:hip/hip-dysfunctions/pain',
to: 'finding:neck/neck-dysfunctions',
position: { before: 'vet' },
},
{
op: 'move',
target: 'finding:hip/anamnesis/trauma',
to: 'finding:hip/anamnesis/surgery',
},
{
op: 'move',
target: `${MOBILITY}/adduction`,
to: MOBILITY,
position: { after: 'abduction' },
},
]),
},
{
name: 'cite',
changes_file: one([
{
op: 'cite',
target: MOBILITY,
reference_ids: ['paper', 'atlas', 'paper'],
},
{ op: 'cite', target: MOBILITY, reference_ids: ['atlas'] },
{ op: 'cite', target: 'zone:hip', reference_ids: ['atlas'] },
{
op: 'cite',
target: 'zone:neck',
reference_ids: ['paper'],
scope: 'subtree',
},
{
op: 'cite',
target: 'finding:hip/anamnesis',
reference_ids: ['paper'],
scope: 'subtree',
},
]),
},
{
name: 'review-rule-b',
changes_file: one(
[
{
op: 'set',
target: MOBILITY,
field: 'name',
value: { fr: 'Mobilité', en: 'Mobility' },
},
{
op: 'add',
target: 'finding:hip/anamnesis',
value: {
...leaf('new'),
children: [
leaf('child'),
{ ...leaf('confirmed'), review_status: 'to_confirm' },
],
},
},
{
op: 'cite',
target: 'zone:neck',
reference_ids: ['atlas'],
scope: 'subtree',
},
{
op: 'rename',
target: 'finding:neck/neck-dysfunctions/stiffness',
id: 'rigidity',
},
{
op: 'set',
target: 'annotation:neck',
field: 'name.en',
value: 'Cervical',
},
{ op: 'set', target: 'view:head', field: 'name.fr', value: 'Crâne' },
{ op: 'set', target: 'zone:hip', field: 'name.en', value: 'Hip joint' },
{
op: 'move',
target: `${MOBILITY}/flexion`,
to: 'finding:hip/anamnesis',
},
{
op: 'set',
target: 'finding:hip/anamnesis/flexion',
field: 'description.fr',
value: 'Déplacée',
},
],
{
level: 'B',
kind: 'enrichment',
reference_ids: ['atlas'],
priority: 'high',
},
),
},
{
name: 'review-rule-c',
changes_file: one(
[
{
op: 'set',
target: 'annotation:hip',
field: 'laterality',
value: 'left',
},
{
op: 'set',
target: 'finding:neck/neck-dysfunctions',
field: 'description.fr',
value: 'À confirmer',
},
{
op: 'set',
target: 'finding:neck/neck-dysfunctions/vet',
field: 'review_status',
value: 'inherited',
},
],
{
level: 'C',
kind: 'correction',
question: 'Latéralité ?',
priority: 'low',
},
),
},
{
name: 'rehome',
changes_file: one([
{
op: 'add',
target: 'zone:hip',
value: {
id: 'hip-new',
kind: 'dysfunction',
name: { fr: 'N', en: 'N' },
children: [
{
id: 'mobility',
name: { fr: 'M', en: 'M' },
children: [{ id: 'flexion', name: { fr: 'F', en: 'F' } }],
},
],
},
},
{ op: 'remove', target: 'finding:hip/hip-dysfunctions' },
{ op: 'rename', target: 'annotation:hip', id: 'coxofemoral' },
]),
},
{
// TypeScript: `z.unknown()` accepts the missing `value`, the set writes `undefined` and
// JSON.stringify drops the key. The Rust port refuses this op (declared difference).
name: 'declared-set-without-value-drops-key',
changes_file: one([
{
op: 'set',
target: `${MOBILITY}/flexion`,
field: 'description.en',
value: 'x',
},
{ op: 'set', target: `${MOBILITY}/flexion`, field: 'description.en' },
]),
},
{
name: 'two-changes',
changes_file: {
changes: [
change([{ op: 'rename', target: 'zone:neck', id: 'cervical' }], {
id: 'CHG-010',
kind: 'terminology',
}),
change(
[
{
op: 'set',
target: 'finding:cervical/neck-dysfunctions/stiffness',
field: 'name.fr',
value: 'Raideur',
},
{ op: 'cite', target: 'zone:cervical', reference_ids: ['paper'] },
],
{
id: 'CHG-0011',
level: 'C',
kind: 'translation',
question: 'Terme exact ?',
reference_ids: ['paper'],
},
),
],
},
},
]
const REFUSED: Case[] = [
{
name: 'schema-unknown-change-key',
changes_file: one([{ op: 'remove', target: `${MOBILITY}/flexion` }], {
extra: 1,
}),
},
{
name: 'schema-unknown-op-key',
changes_file: one([
{ op: 'remove', target: `${MOBILITY}/flexion`, note: 'x' },
]),
},
{
name: 'schema-bad-id',
changes_file: one([{ op: 'remove', target: `${MOBILITY}/flexion` }], {
id: 'CHG-12',
}),
},
{
name: 'schema-level-c-without-question',
changes_file: one([{ op: 'remove', target: `${MOBILITY}/flexion` }], {
level: 'C',
}),
},
{
name: 'schema-duplicate-change-ids',
changes_file: {
changes: [
change([{ op: 'remove', target: `${MOBILITY}/flexion` }]),
change([{ op: 'remove', target: `${MOBILITY}/extension` }]),
],
},
},
{ name: 'schema-empty-ops', changes_file: one([]) },
{
name: 'schema-empty-cite',
changes_file: one([{ op: 'cite', target: MOBILITY, reference_ids: [] }]),
},
{
name: 'schema-unknown-op',
changes_file: one([{ op: 'swap', target: MOBILITY }]),
},
{
name: 'schema-negative-position',
changes_file: one([
{ op: 'add', target: MOBILITY, value: leaf('x'), position: -1 },
]),
},
{
name: 'schema-fraction-position',
changes_file: one([
{ op: 'add', target: MOBILITY, value: leaf('x'), position: 1.5 },
]),
},
{
name: 'schema-two-anchors',
changes_file: one([
{
op: 'add',
target: MOBILITY,
value: leaf('x'),
position: { after: 'flexion', before: 'extension' },
},
]),
},
{
name: 'schema-title-missing-en',
changes_file: one([{ op: 'remove', target: `${MOBILITY}/flexion` }], {
title: { fr: 'Essai' },
}),
},
{
name: 'schema-empty-rationale',
changes_file: one([{ op: 'remove', target: `${MOBILITY}/flexion` }], {
rationale: '',
}),
},
{
name: 'schema-bad-priority',
changes_file: one([{ op: 'remove', target: `${MOBILITY}/flexion` }], {
priority: 'urgent',
}),
},
{
name: 'schema-empty-field',
changes_file: one([{ op: 'set', target: MOBILITY, field: '', value: 1 }]),
},
{
name: 'schema-set-without-value',
changes_file: one([{ op: 'set', target: MOBILITY, field: 'name.fr' }]),
},
{
name: 'schema-null-scope',
changes_file: one([
{ op: 'cite', target: MOBILITY, reference_ids: ['atlas'], scope: null },
]),
},
{
name: 'schema-null-position',
changes_file: one([
{ op: 'add', target: MOBILITY, value: leaf('x'), position: null },
]),
},
{
name: 'schema-null-field',
changes_file: one([
{ op: 'add', target: MOBILITY, field: null, value: leaf('x') },
]),
},
{
name: 'apply-unknown-target',
changes_file: one([
{ op: 'set', target: 'zone:nope', field: 'name', value: {} },
]),
},
{
name: 'apply-malformed-target',
changes_file: one([
{ op: 'set', target: 'finding:hip', field: 'name', value: {} },
]),
},
{
name: 'apply-unknown-kind',
changes_file: one([
{ op: 'set', target: 'galaxy:x', field: 'name', value: {} },
]),
},
{
name: 'apply-unknown-field',
changes_file: one([
{ op: 'set', target: 'zone:hip', field: 'colour', value: 1 },
]),
},
{
name: 'apply-set-id',
changes_file: one([
{ op: 'set', target: 'zone:hip', field: 'id', value: 'x' },
]),
},
{
name: 'apply-malformed-field',
changes_file: one([
{ op: 'set', target: 'zone:hip', field: 'name..fr', value: 'x' },
]),
},
{
name: 'apply-add-existing',
changes_file: one([
{ op: 'add', target: MOBILITY, value: leaf('flexion') },
]),
},
{
name: 'apply-add-unknown-sibling',
changes_file: one([
{
op: 'add',
target: MOBILITY,
value: leaf('x'),
position: { after: 'nope' },
},
]),
},
{
name: 'apply-add-out-of-range',
changes_file: one([
{ op: 'add', target: MOBILITY, value: leaf('x'), position: 3 },
]),
},
{
name: 'apply-add-non-object',
changes_file: one([{ op: 'add', target: MOBILITY, value: 'x' }]),
},
{
name: 'apply-add-document-without-field',
changes_file: one([{ op: 'add', target: 'document', value: leaf('x') }]),
},
{
name: 'apply-add-field-off-document',
changes_file: one([
{ op: 'add', target: 'zone:hip', field: 'categories', value: leaf('x') },
]),
},
{
name: 'apply-add-without-id',
changes_file: one([
{ op: 'add', target: MOBILITY, value: { name: { fr: 'a', en: 'a' } } },
]),
},
{
name: 'apply-add-to-annotation',
changes_file: one([
{ op: 'add', target: 'annotation:hip', value: leaf('x') },
]),
},
{
name: 'apply-rename-taken',
changes_file: one([{ op: 'rename', target: 'zone:hip', id: 'neck' }]),
},
{
name: 'apply-rename-document',
changes_file: one([{ op: 'rename', target: 'document', id: 'x' }]),
},
{
name: 'apply-cite-undeclared',
changes_file: one([
{ op: 'cite', target: MOBILITY, reference_ids: ['ghost'] },
]),
},
{
name: 'apply-cite-annotation',
changes_file: one([
{ op: 'cite', target: 'annotation:hip', reference_ids: ['atlas'] },
]),
},
{
name: 'apply-change-undeclared',
changes_file: one([{ op: 'remove', target: `${MOBILITY}/flexion` }], {
reference_ids: ['ghost'],
}),
},
{
name: 'apply-move-into-itself',
changes_file: one([
{ op: 'move', target: MOBILITY, to: `${MOBILITY}/flexion` },
]),
},
{
name: 'apply-move-non-finding',
changes_file: one([{ op: 'move', target: 'zone:hip', to: 'zone:neck' }]),
},
{
name: 'apply-move-node-to-zone',
changes_file: one([
{ op: 'move', target: `${MOBILITY}/flexion`, to: 'zone:neck' },
]),
},
{
name: 'apply-remove-document',
changes_file: one([{ op: 'remove', target: 'document' }]),
},
{
name: 'apply-remove-zone-integrity',
changes_file: one([{ op: 'remove', target: 'zone:hip' }]),
},
{
name: 'apply-shape-error',
changes_file: one([
{ op: 'set', target: 'region:C', field: 'count', value: 'seven' },
]),
},
{
name: 'apply-subtree-on-zone',
changes_file: one([
{
op: 'set',
target: 'zone:hip',
field: 'name',
value: {},
scope: 'subtree',
},
]),
},
{
name: 'apply-subtree-no-field',
changes_file: one([
{
op: 'set',
target: MOBILITY,
field: 'kind',
value: 'x',
scope: 'subtree',
},
]),
},
{
name: 'apply-b-sets-reviewed',
changes_file: one(
[
{
op: 'set',
target: MOBILITY,
field: 'review_status',
value: 'reviewed',
},
],
{ level: 'B' },
),
},
{
name: 'apply-second-change-fails',
changes_file: {
changes: [
change([{ op: 'rename', target: 'zone:neck', id: 'cervical' }]),
change(
[{ op: 'remove', target: 'finding:neck/neck-dysfunctions/vet' }],
{ id: 'CHG-002' },
),
],
},
},
]
const REFERENCE_FILES: Array<{ name: string; file: unknown }> = [
{
name: 'references-duplicate-id',
file: { references: [...MINI_REFERENCES, MINI_REFERENCES[0]] },
},
{
name: 'references-unknown-key',
file: { references: [{ ...MINI_REFERENCES[0], note: 'x' }] },
},
{
name: 'references-bad-type',
file: { references: [{ ...MINI_REFERENCES[0], type: 'blog' }] },
},
{
name: 'references-extra-top-key',
file: { references: MINI_REFERENCES, extra: true },
},
{
name: 'references-bad-url',
file: { references: [{ ...MINI_REFERENCES[0], url: 'ftp://x' }] },
},
]
async function run(c: Case, ok: boolean) {
try {
const changeset = ChangesetSchema.parse(c.changes_file)
const baseline = miniSheet()
const result = applyChangeset(baseline, MINI_REFERENCES, changeset)
const document = await formatJson(result.document)
const migration = buildReportMigrationMap({
baseline: miniSheet() as never,
final: result.document as never,
legacyIdMap: LEGACY_ID_MAP,
records: result.records,
sheet: SHEET,
mainViewId: 'main',
})
if (!ok) throw new Error(`${c.name}: expected a refusal`)
return {
...c,
ok: true,
document,
records: result.records,
stats: documentStats(result.document),
migration_map: await formatJson(migration.map),
migration_stats: migration.stats,
seed_sql: renderDraftSeedSql({
sheetId: SHEET.sheetId,
assets: SEED_ASSETS,
documentJson: document,
changeNote: "Essai 'quoted'",
}),
}
} catch (error) {
if (ok) throw error
return {
...c,
ok: false,
error: error instanceof Error ? error.message : String(error),
}
}
}
function referenceCases() {
return REFERENCE_FILES.map(({ name, file }) => {
const parsed = ReferencesFileSchema.safeParse(file)
expect(parsed.success, name).toBe(false)
return { name, file, ok: false }
})
}
describe('R5.3 synthetic curation oracle', () => {
it('curation-synthetic.json', async () => {
const rows = []
for (const c of ACCEPTED) rows.push(await run(c, true))
for (const c of REFUSED) rows.push(await run(c, false))
const oracle = {
baseline: miniSheet(),
references_file: REFERENCES_FILE,
cases: rows,
reference_cases: referenceCases(),
}
const text = `${JSON.stringify(oracle)}\n`
const file = resolve(OUT, 'curation-synthetic.json')
if (MAKE) {
mkdirSync(OUT, { recursive: true })
writeFileSync(file, text)
}
expect(text).toBe(readFileSync(file, 'utf8'))
})
})
+15
View File
@@ -0,0 +1,15 @@
#!/usr/bin/env bash
# `pnpm r5:import-equine --check | --out <dir>`: the anatomical pipeline on the core (R5.2,
# R5.3), the `import-equine` binary of crates/core behind its `curation` feature, on the native
# toolchain of the development machine (scripts/rust-env.sh). The first run compiles it
# (release profile); the next ones start at once. `--check` compares the produced folders with
# the committed seed and writes nothing; `--out <dir>` writes `<dir>/legacy-import` and
# `<dir>/draft` into an absent or empty folder (a relative path is typed from the folder pnpm
# was run in, INIT_CWD). The TypeScript `pnpm import:equine` stays the writer of the seed until
# the R5 switch.
set -euo pipefail
cd "$(dirname "$0")/.."
. scripts/rust-env.sh
exec cargo run --release --locked -q -p faunosteo-core --features curation --bin import-equine -- "$@"