diff --git a/Cargo.lock b/Cargo.lock
index 703b5d09..ee718fd5 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -364,6 +364,7 @@ checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f"
name = "faunosteo-core"
version = "0.1.0"
dependencies = [
+ "icu_normalizer",
"lopdf",
"miniz_oxide 0.9.1",
"roxmltree",
diff --git a/Cargo.toml b/Cargo.toml
index 76e87343..2cd511f5 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -45,6 +45,9 @@ sha2 = "0.11"
# Shortest round-trip float digits with ECMAScript's tie rule (round half to even), the
# formatter serde_json already uses (R5.1b, F1): the version serde_json locks.
zmij = "1.0.23"
+# NFD for the pipeline's slugs and region roots (R5.2): the normaliser `url` already locks
+# through idna, its compiled data only.
+icu_normalizer = { version = "2.3.0", default-features = false, features = ["compiled_data"] }
# The library crates deny unwrap/expect so that no panic path depends on content;
# the worker binary relaxes disallowed_methods (it owns I/O) in its own [lints].
diff --git a/crates/core/Cargo.toml b/crates/core/Cargo.toml
index 4b8d63fc..b446b3e4 100644
--- a/crates/core/Cargo.toml
+++ b/crates/core/Cargo.toml
@@ -22,6 +22,9 @@ pdf-write = ["pdf-read"]
# The archive check tool (R4.7, plan §B.1.7): `pdf::compare` and the `report-compare` binary,
# the one file of the crate that reads the file system. Never enabled by the wasm facade.
cli = ["pdf-read", "pdf-write"]
+# The anatomical pipeline (R5): legacy importer, curation, generated files and their
+# formatting, the `import-equine` tool. Never enabled by the wasm facade or the worker.
+curation = ["dep:sha2", "dep:icu_normalizer"]
[dependencies]
serde.workspace = true
@@ -31,12 +34,19 @@ roxmltree.workspace = true
zmij.workspace = true
ts-rs = { workspace = true, optional = true }
miniz_oxide = { workspace = true, optional = true }
+sha2 = { workspace = true, optional = true }
+icu_normalizer = { workspace = true, optional = true }
[[bin]]
name = "report-compare"
path = "src/bin/report_compare.rs"
required-features = ["cli"]
+[[bin]]
+name = "import-equine"
+path = "src/bin/import_equine.rs"
+required-features = ["curation"]
+
[dev-dependencies]
# Reads the PDFs the tests compare (the R3 fixtures and the R4 output): objects, streams,
# annotations; never in the library.
@@ -99,3 +109,12 @@ required-features = ["cli"]
[lints]
workspace = true
+
+# The pipeline suites (R5) compile only with the feature.
+[[test]]
+name = "curation_format"
+required-features = ["curation"]
+
+[[test]]
+name = "curation_legacy"
+required-features = ["curation"]
diff --git a/crates/core/src/bin/import_equine.rs b/crates/core/src/bin/import_equine.rs
new file mode 100644
index 00000000..ece36064
--- /dev/null
+++ b/crates/core/src/bin/import_equine.rs
@@ -0,0 +1,152 @@
+//! `import-equine` (R5.2): the legacy importer on the core. Reads the historical equine files
+//! of the repository and produces the `legacy-import/` folder the TypeScript
+//! `pnpm import:equine` generates (the draft folder joins at R5.3).
+//!
+//! import-equine --check compare with the committed folder, write nothing
+//! import-equine --out
write the folder into (absent or empty)
+//!
+//! The repository is found from this crate's manifest folder. Nothing is deleted: `--out`
+//! refuses a folder that already holds files. Like `report-compare`, this file is the only
+//! one of its tool allowed the file system.
+#![allow(clippy::disallowed_methods)]
+
+use std::path::{Path, PathBuf};
+use std::{env, fs, process};
+
+use faunosteo_core::curation::legacy::{
+ EquineLegacyInput, SvgSource, import_equine_legacy, render_artifacts,
+};
+
+const SEED: &str = concat!(
+ env!("CARGO_MANIFEST_DIR"),
+ "/../../supabase/seed/anatomical-sheets/equine"
+);
+
+/// A file of the folder: its name and its bytes.
+type FolderFile = (String, Vec);
+
+fn read(path: &Path) -> Result, String> {
+ fs::read(path).map_err(|error| format!("{}: {error}", path.display()))
+}
+
+fn svg(name: &str) -> Result {
+ let bytes = read(&Path::new(SEED).join("legacy-sources").join(name))?;
+ Ok(SvgSource {
+ file_name: name.to_owned(),
+ bytes,
+ })
+}
+
+fn input() -> Result {
+ let sources = Path::new(SEED).join("legacy-sources");
+ Ok(EquineLegacyInput {
+ main_svg: svg("Horse.svg")?,
+ head_svg: svg("Horse_head.svg")?,
+ catalog: read(&sources.join("pathologies.json"))?,
+ vertebrae: read(&sources.join("vertebrae.ts"))?,
+ })
+}
+
+/// Every file of `legacy-import/`: the four texts, then the two SVG copies.
+fn folder() -> Result, String> {
+ let input = input()?;
+ let result = import_equine_legacy(&input).map_err(|error| error.to_string())?;
+ let mut files: Vec = render_artifacts(&result)
+ .map_err(|error| error.to_string())?
+ .into_iter()
+ .map(|(name, text)| (name, text.into_bytes()))
+ .collect();
+ for source in [input.main_svg, input.head_svg] {
+ files.push((format!("assets/{}", source.file_name), source.bytes));
+ }
+ Ok(files)
+}
+
+/// Relative paths of every file under `dir`.
+fn listed(dir: &Path, prefix: &str, out: &mut Vec) -> Result<(), String> {
+ let entries = fs::read_dir(dir).map_err(|error| format!("{}: {error}", dir.display()))?;
+ for entry in entries {
+ let entry = entry.map_err(|error| error.to_string())?;
+ let name = format!("{prefix}{}", entry.file_name().to_string_lossy());
+ if entry.path().is_dir() {
+ listed(&entry.path(), &format!("{name}/"), out)?;
+ } else {
+ out.push(name);
+ }
+ }
+ Ok(())
+}
+
+fn check(files: &[FolderFile]) -> Result, String> {
+ let target = Path::new(SEED).join("legacy-import");
+ let mut problems = Vec::new();
+ for (name, bytes) in files {
+ match fs::read(target.join(name)) {
+ Ok(committed) if committed == *bytes => {}
+ Ok(_) => problems.push(format!("differs: {name}")),
+ Err(_) => problems.push(format!("missing: {name}")),
+ }
+ }
+ let mut present = Vec::new();
+ listed(&target, "", &mut present)?;
+ for name in present {
+ if !files.iter().any(|(produced, _)| *produced == name) {
+ problems.push(format!("not produced: {name}"));
+ }
+ }
+ Ok(problems)
+}
+
+fn write(files: &[FolderFile], out: &Path) -> Result<(), String> {
+ if out.exists()
+ && fs::read_dir(out)
+ .map_err(|e| e.to_string())?
+ .next()
+ .is_some()
+ {
+ return Err(format!("{}: not empty, nothing written", out.display()));
+ }
+ for (name, bytes) in files {
+ let path = out.join(name);
+ if let Some(parent) = path.parent() {
+ fs::create_dir_all(parent).map_err(|error| error.to_string())?;
+ }
+ fs::write(&path, bytes).map_err(|error| format!("{}: {error}", path.display()))?;
+ }
+ Ok(())
+}
+
+fn run(args: &[String]) -> Result {
+ let files = folder()?;
+ match args {
+ [flag] if flag == "--check" => {
+ let problems = check(&files)?;
+ for problem in &problems {
+ eprintln!("{problem}");
+ }
+ println!(
+ "legacy-import: {} files, {} differences",
+ files.len(),
+ problems.len()
+ );
+ Ok(i32::from(!problems.is_empty()))
+ }
+ [flag, dir] if flag == "--out" => {
+ write(&files, &PathBuf::from(dir))?;
+ println!("legacy-import: {} files written to {dir}", files.len());
+ Ok(0)
+ }
+ _ => Err("usage: import-equine --check | --out ".to_owned()),
+ }
+}
+
+fn main() {
+ let args: Vec = env::args().skip(1).collect();
+ match run(&args) {
+ Ok(code) => process::exit(code),
+ Err(message) => {
+ eprintln!("{message}");
+ process::exit(2);
+ }
+ }
+}
diff --git a/crates/core/src/curation/ids.rs b/crates/core/src/curation/ids.rs
new file mode 100644
index 00000000..6f52ce1c
--- /dev/null
+++ b/crates/core/src/curation/ids.rs
@@ -0,0 +1,40 @@
+//! Deterministic identifiers of `packages/anatomy/src/legacy/ids.ts`: nothing random or
+//! time-based, so the importer emits the same bytes on every run.
+
+use sha2::{Digest, Sha256};
+
+/// Lowercase hex sha256 of bytes (a string hashes as its UTF-8 bytes, as `createHash` does).
+pub fn sha256_hex(data: &[u8]) -> String {
+ Sha256::digest(data)
+ .iter()
+ .map(|byte| format!("{byte:02x}"))
+ .collect()
+}
+
+/// RFC 4122 version 5 style with SHA-256 in place of SHA-1: the first 16 bytes of
+/// sha256(name), the version nibble forced to 5, the variant bits to 10xx.
+pub fn deterministic_uuid(name: &str) -> String {
+ let digest = Sha256::digest(name.as_bytes());
+ let mut bytes = [0u8; 16];
+ bytes.copy_from_slice(&digest[..16]);
+ bytes[6] = (bytes[6] & 0x0f) | 0x50;
+ bytes[8] = (bytes[8] & 0x3f) | 0x80;
+ let hex: String = bytes.iter().map(|byte| format!("{byte:02x}")).collect();
+ format!(
+ "{}-{}-{}-{}-{}",
+ &hex[0..8],
+ &hex[8..12],
+ &hex[12..16],
+ &hex[16..20],
+ &hex[20..32]
+ )
+}
+
+/// Same bytes, same asset id.
+pub fn asset_id_from_sha256(file_sha256: &str) -> String {
+ deterministic_uuid(&format!("faunosteo:asset:{file_sha256}"))
+}
+
+pub fn sheet_id_for(species_id: &str, version: u32) -> String {
+ deterministic_uuid(&format!("faunosteo:sheet:{species_id}:v{version}"))
+}
diff --git a/crates/core/src/curation/legacy/annotations.rs b/crates/core/src/curation/legacy/annotations.rs
new file mode 100644
index 00000000..6eeae7aa
--- /dev/null
+++ b/crates/core/src/curation/legacy/annotations.rs
@@ -0,0 +1,212 @@
+//! Legacy SVG annotation → canonical annotation (`packages/anatomy/src/legacy/annotations.ts`).
+//! The finding zone link is resolved here, once, by exact FR + EN equality with the catalog;
+//! the document then carries `finding_zone_id` and nothing matches labels at runtime.
+
+use std::collections::HashMap;
+
+use serde_json::{Value, json};
+
+use super::categories::normalise_category_id;
+use super::regions::infer_region_code;
+use super::svg::{LegacyAnnotation, LegacyGeometry};
+use super::types::{AnnotationRow, Anomaly, RegionInference};
+use crate::curation::text::slugify;
+
+pub struct AnnotationContext<'a> {
+ pub view_id: &'a str,
+ /// `${name_fr}\n${name_en}` → catalog zone key (a JavaScript `Map`: the last zone wins).
+ pub zone_by_names: &'a HashMap,
+}
+
+pub struct MappedAnnotation {
+ pub annotation: Value,
+ pub row: AnnotationRow,
+ pub region: Option,
+ pub anomalies: Vec,
+}
+
+pub fn zone_name_key(fr: &str, en: &str) -> String {
+ format!("{fr}\n{en}")
+}
+
+fn laterality(a: &LegacyAnnotation, out: &mut Vec) -> &'static str {
+ match a.lateral.as_deref() {
+ Some("bilateral") => "bilateral",
+ None | Some("unpaired") => "unpaired",
+ Some(other) => {
+ out.push(Anomaly::to_validate(
+ "annotation.laterality.unknown",
+ a.id.clone(),
+ format!("Latéralité « {other} » inconnue, importée en « unpaired »."),
+ ));
+ "unpaired"
+ }
+ }
+}
+
+fn region_of(a: &LegacyAnnotation, out: &mut Vec) -> Option {
+ let special = a.special.as_deref()?;
+ if special != "vertebral-precision" {
+ out.push(Anomaly::to_validate(
+ "annotation.special.unknown",
+ a.id.clone(),
+ format!("special « {special} » ignoré."),
+ ));
+ return None;
+ }
+ let found = infer_region_code(a.name_fr.as_deref(), a.name_en.as_deref());
+ if found.is_none() {
+ out.push(Anomaly::to_validate(
+ "annotation.region.unknown",
+ a.id.clone(),
+ "Région vertébrale non déduite.",
+ ));
+ }
+ let fallback = a
+ .name_fr
+ .clone()
+ .or_else(|| a.name_en.clone())
+ .unwrap_or_else(|| a.id.clone());
+ Some(RegionInference {
+ annotation_id: a.id.clone(),
+ label: found.as_ref().map_or(fallback, |m| m.label.clone()),
+ root: found.as_ref().map(|m| m.root.clone()),
+ code: found.map(|m| m.code),
+ })
+}
+
+fn geometry(a: &LegacyAnnotation) -> Value {
+ a.geometry
+ .as_ref()
+ .map_or(Value::Null, LegacyGeometry::to_value)
+}
+
+fn category_id(a: &LegacyAnnotation) -> String {
+ normalise_category_id(a.category.as_deref().unwrap_or(""))
+}
+
+fn trigger_name(a: &LegacyAnnotation, out: &mut Vec) -> Value {
+ match (a.name_fr.as_deref(), a.name_en.as_deref()) {
+ (Some(fr), Some(en)) if !fr.is_empty() && !en.is_empty() => json!({ "fr": fr, "en": en }),
+ _ => {
+ out.push(Anomaly::informative(
+ "annotation.trigger.unnamed",
+ a.id.clone(),
+ "Déclencheur de vue détail sans nom (name null), comme dans le SVG.",
+ ));
+ Value::Null
+ }
+ }
+}
+
+fn map_trigger(a: &LegacyAnnotation, ctx: &AnnotationContext, out: &mut Vec) -> Value {
+ let opens = a.opens_detail.clone().unwrap_or_default();
+ let laterality = laterality(a, out);
+ let name = trigger_name(a, out);
+ json!({
+ "id": format!("{opens}-trigger"),
+ "kind": "detail_trigger",
+ "view_id": ctx.view_id,
+ "category_id": category_id(a),
+ "laterality": laterality,
+ "geometry": geometry(a),
+ "name": name,
+ "opens_view_id": opens,
+ })
+}
+
+/// Missing labels fall back to the other language, never silently.
+fn observable_name(a: &LegacyAnnotation, out: &mut Vec) -> (String, String) {
+ let fr = a
+ .name_fr
+ .clone()
+ .or_else(|| a.name_en.clone())
+ .unwrap_or_else(|| a.id.clone());
+ if a.name_fr.is_none() {
+ out.push(Anomaly::to_validate(
+ "annotation.name_fr.missing",
+ a.id.clone(),
+ format!("data-anno-name-fr absent : nom FR repris de « {fr} »."),
+ ));
+ }
+ if a.name_en.is_none() {
+ out.push(Anomaly::to_validate(
+ "annotation.name_en.missing",
+ a.id.clone(),
+ format!("data-anno-name-en absent : nom EN copié du FR « {fr} », à traduire."),
+ ));
+ }
+ let en = a.name_en.clone().unwrap_or_else(|| fr.clone());
+ (fr, en)
+}
+
+fn map_observable(
+ a: &LegacyAnnotation,
+ ctx: &AnnotationContext,
+ out: &mut Vec,
+) -> (Value, Option) {
+ let (fr, en) = observable_name(a, out);
+ let zone_id = ctx.zone_by_names.get(&zone_name_key(&fr, &en)).cloned();
+ if zone_id.is_none() {
+ out.push(Anomaly::to_validate(
+ "annotation.unmapped",
+ a.id.clone(),
+ "Annotation non reliée à une zone de constats.",
+ ));
+ }
+ let region = region_of(a, out);
+ let laterality = laterality(a, out);
+ let annotation = json!({
+ "id": zone_id.clone().unwrap_or_else(|| slugify(&en)),
+ "kind": if a.tag_name == "circle" { "point" } else { "zone" },
+ "view_id": ctx.view_id,
+ "category_id": category_id(a),
+ "laterality": laterality,
+ "geometry": geometry(a),
+ "name": { "fr": fr, "en": en },
+ "finding_zone_id": zone_id,
+ "vertebral_region_code": region.as_ref().and_then(|r| r.code.clone()),
+ });
+ (annotation, region)
+}
+
+fn text(value: &Value, key: &str) -> Option {
+ value.get(key).and_then(Value::as_str).map(str::to_owned)
+}
+
+pub fn map_annotation(a: &LegacyAnnotation, ctx: &AnnotationContext) -> MappedAnnotation {
+ let mut anomalies = Vec::new();
+ let is_trigger = a
+ .opens_detail
+ .as_deref()
+ .is_some_and(|opens| !opens.is_empty());
+ let (annotation, region) = if is_trigger {
+ (map_trigger(a, ctx, &mut anomalies), None)
+ } else {
+ map_observable(a, ctx, &mut anomalies)
+ };
+ let row = AnnotationRow {
+ legacy_id: a.id.clone(),
+ new_id: text(&annotation, "id").unwrap_or_default(),
+ view_id: ctx.view_id.to_owned(),
+ kind: text(&annotation, "kind").unwrap_or_default(),
+ laterality: text(&annotation, "laterality").unwrap_or_default(),
+ region_code: if is_trigger {
+ None
+ } else {
+ text(&annotation, "vertebral_region_code")
+ },
+ finding_zone_id: if is_trigger {
+ None
+ } else {
+ text(&annotation, "finding_zone_id")
+ },
+ geometry: LegacyGeometry::describe(a.geometry.as_ref()),
+ };
+ MappedAnnotation {
+ annotation,
+ row,
+ region,
+ anomalies,
+ }
+}
diff --git a/crates/core/src/curation/legacy/authored_labels.rs b/crates/core/src/curation/legacy/authored_labels.rs
new file mode 100644
index 00000000..ad018b91
--- /dev/null
+++ b/crates/core/src/curation/legacy/authored_labels.rs
@@ -0,0 +1,58 @@
+//! Every FR/EN text the importer writes itself, derived from the document so the list can
+//! never drift from what is written (`packages/anatomy/src/legacy/authored-labels.ts`).
+
+use serde_json::Value;
+
+use super::types::Anomaly;
+
+pub const AUTHORED_LABEL_CODE: &str = "label.authored";
+
+/// `${value}` of a JavaScript template literal for a JSON value read from the document.
+fn template(value: &Value) -> String {
+ match value {
+ Value::String(text) => text.clone(),
+ other => other.to_string(),
+ }
+}
+
+fn authored(subject: String, text: Option<&Value>) -> Anomaly {
+ let parts: Vec = [("FR", "fr"), ("EN", "en")]
+ .into_iter()
+ .filter_map(|(label, key)| {
+ text.and_then(|t| t.get(key))
+ .map(|value| format!("{label} « {} »", template(value)))
+ })
+ .collect();
+ Anomaly::to_validate(
+ AUTHORED_LABEL_CODE,
+ subject,
+ format!("Libellé écrit par l'importateur : {}.", parts.join(", ")),
+ )
+}
+
+pub fn region_label_subject(code: &str) -> String {
+ format!("vertebral_config.{code}")
+}
+
+fn items<'a>(document: &'a Value, pointer: &str) -> &'a [Value] {
+ document
+ .pointer(pointer)
+ .and_then(Value::as_array)
+ .map_or(&[], Vec::as_slice)
+}
+
+pub fn authored_label_anomalies(document: &Value) -> Vec {
+ let mut out = vec![
+ authored("name".into(), document.get("name")),
+ authored("description".into(), document.get("description")),
+ ];
+ for view in items(document, "/views") {
+ let id = view.get("id").map(template).unwrap_or_default();
+ out.push(authored(format!("views.{id}"), view.get("name")));
+ }
+ for region in items(document, "/vertebral_config/regions") {
+ let code = region.get("code").map(template).unwrap_or_default();
+ out.push(authored(region_label_subject(&code), region.get("name")));
+ }
+ out
+}
diff --git a/crates/core/src/curation/legacy/catalog.rs b/crates/core/src/curation/legacy/catalog.rs
new file mode 100644
index 00000000..d612af6c
--- /dev/null
+++ b/crates/core/src/curation/legacy/catalog.rs
@@ -0,0 +1,353 @@
+//! The legacy findings catalog (`pathologies.json`, schema 2.0) → canonical finding zones in
+//! the lean authoring form (`packages/anatomy/src/legacy/catalog.ts`): optional keys are
+//! written only when the source carries them.
+//!
+//! The catalog is read as `JSON.parse` reads it (`domain::js::parse_json`, zones in
+//! `Object.entries` order) and its values are copied as they are. Declared difference: where
+//! JavaScript would coerce or crash on a wrong type (an id or a `children` that is not what
+//! the format says), the port refuses the catalog with a message naming the node.
+
+use serde_json::{Map, Value, json};
+
+use super::types::{Anomaly, ZoneStats};
+use crate::curation::PipelineError;
+use crate::curation::text::{js_keys, js_object, slugify};
+use crate::domain::js::{is_stable_id, parse_json};
+
+/// A parsed catalog: the zones in `Object.entries` order, and the other top-level values.
+#[derive(Debug, Clone)]
+pub struct LegacyCatalog {
+ pub root: Map,
+ pub zones: Vec<(String, Map)>,
+}
+
+impl LegacyCatalog {
+ pub fn get(&self, key: &str) -> Option<&Value> {
+ self.root.get(key)
+ }
+}
+
+pub fn parse_legacy_catalog(text: &str) -> Result {
+ let source = |why: &str| PipelineError::Source(format!("pathologies.json: {why}"));
+ let value = parse_json(text).map_err(|error| source(&error.to_string()))?;
+ let Value::Object(root) = value else {
+ return Err(source("\"zones\" object missing"));
+ };
+ let Some(Value::Object(zones)) = root.get("zones") else {
+ return Err(source("\"zones\" object missing"));
+ };
+ let mut ordered = Vec::with_capacity(zones.len());
+ for key in js_keys(zones) {
+ let Some(Value::Object(zone)) = zones.get(key) else {
+ return Err(source(&format!("zone {key} is not an object")));
+ };
+ ordered.push((key.clone(), zone.clone()));
+ }
+ Ok(LegacyCatalog {
+ root,
+ zones: ordered,
+ })
+}
+
+/// A node's string field, refused when absent or not a string.
+pub fn text_field<'a>(node: &'a Map, key: &str) -> Result<&'a str, PipelineError> {
+ node.get(key).and_then(Value::as_str).ok_or_else(|| {
+ PipelineError::Source(format!("pathologies.json: \"{key}\" is not a string"))
+ })
+}
+
+/// A node's child list (`node.children ?? []`), refused when it is not an array.
+pub fn children(node: &Map) -> Result>, PipelineError> {
+ match node.get("children") {
+ None | Some(Value::Null) => Ok(Vec::new()),
+ Some(Value::Array(items)) => items.iter().map(as_node).collect(),
+ Some(_) => Err(PipelineError::Source(
+ "pathologies.json: \"children\" is not an array".into(),
+ )),
+ }
+}
+
+/// A zone's categories, refused when not an array of objects.
+pub fn categories(zone: &Map) -> Result>, PipelineError> {
+ match zone.get("categories") {
+ Some(Value::Array(items)) => items.iter().map(as_node).collect(),
+ _ => Err(PipelineError::Source(
+ "pathologies.json: \"categories\" is not an array".into(),
+ )),
+ }
+}
+
+fn as_node(value: &Value) -> Result<&Map, PipelineError> {
+ value
+ .as_object()
+ .ok_or_else(|| PipelineError::Source("pathologies.json: a node is not an object".into()))
+}
+
+pub fn category_kind(category_id: &str) -> &'static str {
+ if category_id.ends_with("-dysfunctions") {
+ "dysfunction"
+ } else if category_id.ends_with("-observations") {
+ "consultation_reason"
+ } else if category_id == "anamnesis" {
+ "anamnesis"
+ } else {
+ "custom"
+ }
+}
+
+/// JavaScript truthiness of a JSON value.
+pub 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 && !n.is_nan()),
+ Some(Value::String(text)) => !text.is_empty(),
+ Some(_) => true,
+ }
+}
+
+#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
+pub struct TreeCounts {
+ pub nodes: usize,
+ pub leaves: usize,
+ pub vet_referrals: usize,
+}
+
+/// Counts categories as nodes, as the audit does (792 = 78 + 714). Works on the legacy and
+/// on the canonical form: both say `children` and `requires_vet_referral`.
+pub fn count_tree(nodes: &[&Map]) -> Result {
+ let mut total = TreeCounts::default();
+ for node in nodes {
+ let kids = children(node)?;
+ let sub = count_tree(&kids)?;
+ total.nodes += 1 + sub.nodes;
+ total.leaves += sub.leaves + usize::from(kids.is_empty());
+ total.vet_referrals +=
+ sub.vet_referrals + usize::from(truthy(node.get("requires_vet_referral")));
+ }
+ Ok(total)
+}
+
+#[derive(Debug, Clone, PartialEq)]
+pub struct MappedCatalog {
+ pub zones: Vec,
+ pub zone_stats: Vec,
+ pub node_id_map: Map,
+ pub missing_description_en: usize,
+ pub sources: usize,
+ pub anomalies: Vec,
+}
+
+/// Tallies of one catalog walk.
+#[derive(Default)]
+struct WalkState {
+ node_ids: Vec<(String, Value)>,
+ anomalies: Vec,
+ missing_description_en: usize,
+ sources: usize,
+}
+
+/// `{ fr?, en? }` from `description_fr` / `description_en`, when either is present.
+fn description(node: &Map) -> Option {
+ let fr = node.get("description_fr");
+ let en = node.get("description_en");
+ if fr.is_none() && en.is_none() {
+ return None;
+ }
+ let mut text = Map::new();
+ if let Some(fr) = fr {
+ text.insert("fr".into(), fr.clone());
+ }
+ if let Some(en) = en {
+ text.insert("en".into(), en.clone());
+ }
+ Some(Value::Object(text))
+}
+
+/// `{ fr: name_fr, en: name_en }`, an absent key left out as `JSON.stringify` leaves out
+/// `undefined`.
+fn name(node: &Map) -> Value {
+ let mut text = Map::new();
+ for (from, to) in [("name_fr", "fr"), ("name_en", "en")] {
+ if let Some(value) = node.get(from) {
+ text.insert(to.into(), value.clone());
+ }
+ }
+ Value::Object(text)
+}
+
+fn stable_node_id(id: &str, path: &[&str], state: &mut WalkState) -> String {
+ if is_stable_id(id) {
+ return id.to_owned();
+ }
+ let slug = slugify(id);
+ let next = if slug.is_empty() {
+ "node".to_owned()
+ } else {
+ slug
+ };
+ let key = [path, &[id]].concat().join("/");
+ state
+ .node_ids
+ .push((key.clone(), Value::from(next.clone())));
+ state.anomalies.push(Anomaly::to_validate(
+ "finding.id.slugified",
+ key,
+ format!("Identifiant hors motif stable, remplacé par « {next} »."),
+ ));
+ next
+}
+
+fn tally_node(node: &Map, state: &mut WalkState) -> Result<(), PipelineError> {
+ if node.contains_key("description_fr") && !node.contains_key("description_en") {
+ state.missing_description_en += 1;
+ }
+ state.sources += match node.get("sources") {
+ None | Some(Value::Null) => 0,
+ Some(Value::Array(items)) => items.len(),
+ Some(_) => {
+ return Err(PipelineError::Source(
+ "pathologies.json: \"sources\" is not an array".into(),
+ ));
+ }
+ };
+ Ok(())
+}
+
+fn map_node(
+ node: &Map,
+ path: &[&str],
+ state: &mut WalkState,
+) -> Result {
+ tally_node(node, state)?;
+ let legacy_id = text_field(node, "id")?;
+ let id = stable_node_id(legacy_id, path, state);
+ let mut out = Map::new();
+ out.insert("id".into(), Value::from(id));
+ out.insert("name".into(), name(node));
+ if let Some(text) = description(node) {
+ out.insert("description".into(), text);
+ }
+ if node.get("requires_vet_referral") == Some(&Value::Bool(true)) {
+ out.insert("requires_vet_referral".into(), Value::Bool(true));
+ }
+ if truthy(node.get("children")) {
+ let child_path = [path, &[legacy_id]].concat();
+ let mapped: Result, PipelineError> = children(node)?
+ .into_iter()
+ .map(|child| map_node(child, &child_path, state))
+ .collect();
+ out.insert("children".into(), Value::Array(mapped?));
+ }
+ Ok(Value::Object(out))
+}
+
+fn map_category(
+ zone_id: &str,
+ category: &Map,
+ state: &mut WalkState,
+) -> Result {
+ tally_node(category, state)?;
+ let category_id = text_field(category, "id")?;
+ let kind = category_kind(category_id);
+ if kind == "custom" {
+ state.anomalies.push(Anomaly::to_validate(
+ "finding.category.kind_unknown",
+ format!("{zone_id}/{category_id}"),
+ "Type de catégorie non reconnu, importé en « custom ».",
+ ));
+ }
+ let id = stable_node_id(category_id, &[zone_id], state);
+ let path = [zone_id, category_id];
+ let mapped: Result, PipelineError> = children(category)?
+ .into_iter()
+ .map(|child| map_node(child, &path, state))
+ .collect();
+ let mut out = json!({ "id": id, "kind": kind, "name": name(category), "children": mapped? });
+ if let (Some(text), Some(map)) = (description(category), out.as_object_mut()) {
+ map.insert("description".into(), text);
+ }
+ Ok(out)
+}
+
+fn zone_stats_of(key: &str, zone: &Map) -> Result {
+ let cats = categories(zone)?;
+ let counts = count_tree(&cats)?;
+ let mut category_leaves = 0;
+ for category in &cats {
+ category_leaves += usize::from(children(category)?.is_empty());
+ }
+ let zone_id = zone.get("id").and_then(Value::as_str).unwrap_or(key);
+ Ok(ZoneStats {
+ zone_id: zone_id.to_owned(),
+ nodes: counts.nodes,
+ leaves: counts.leaves - category_leaves,
+ vet_referrals: counts.vet_referrals,
+ })
+}
+
+/// Fails when the output tree does not hold exactly the input tree.
+pub fn assert_nothing_dropped(
+ zone_id: &str,
+ before: TreeCounts,
+ after: TreeCounts,
+) -> Result<(), PipelineError> {
+ let pairs = [
+ ("nodes", before.nodes, after.nodes),
+ ("leaves", before.leaves, after.leaves),
+ ("vetReferrals", before.vet_referrals, after.vet_referrals),
+ ];
+ let detail: Vec = pairs
+ .iter()
+ .filter(|(_, b, a)| b != a)
+ .map(|(key, b, a)| format!("{key} {b} in, {a} out"))
+ .collect();
+ if detail.is_empty() {
+ return Ok(());
+ }
+ Err(PipelineError::Guard(format!(
+ "zone {zone_id}: {}",
+ detail.join("; ")
+ )))
+}
+
+fn map_zone(
+ key: &str,
+ zone: &Map,
+ state: &mut WalkState,
+) -> Result {
+ let zone_id = text_field(zone, "id")?;
+ if zone_id != key {
+ return Err(PipelineError::Guard(format!(
+ "zone key {key} ≠ id {zone_id}"
+ )));
+ }
+ let cats = categories(zone)?;
+ let mut mapped = Vec::with_capacity(cats.len());
+ for category in &cats {
+ mapped.push(map_category(key, category, state)?);
+ }
+ let out_nodes: Vec<&Map> = mapped.iter().filter_map(Value::as_object).collect();
+ assert_nothing_dropped(key, count_tree(&cats)?, count_tree(&out_nodes)?)?;
+ Ok(json!({ "id": key, "name": name(zone), "categories": mapped }))
+}
+
+pub fn map_catalog(catalog: &LegacyCatalog) -> Result {
+ let mut state = WalkState::default();
+ let mut zones = Vec::with_capacity(catalog.zones.len());
+ let mut zone_stats = Vec::with_capacity(catalog.zones.len());
+ for (key, zone) in &catalog.zones {
+ zones.push(map_zone(key, zone, &mut state)?);
+ }
+ for (key, zone) in &catalog.zones {
+ zone_stats.push(zone_stats_of(key, zone)?);
+ }
+ Ok(MappedCatalog {
+ zones,
+ zone_stats,
+ node_id_map: js_object(state.node_ids),
+ missing_description_en: state.missing_description_en,
+ sources: state.sources,
+ anomalies: state.anomalies,
+ })
+}
diff --git a/crates/core/src/curation/legacy/catalog_checks.rs b/crates/core/src/curation/legacy/catalog_checks.rs
new file mode 100644
index 00000000..2cb75332
--- /dev/null
+++ b/crates/core/src/curation/legacy/catalog_checks.rs
@@ -0,0 +1,140 @@
+//! Catalog-wide findings that need a business decision or an explanation
+//! (`packages/anatomy/src/legacy/catalog-checks.ts`). Nothing here alters the data.
+
+use std::collections::HashSet;
+
+use serde_json::{Map, Value};
+
+use super::catalog::{LegacyCatalog, categories, children, text_field};
+use super::types::Anomaly;
+use crate::curation::PipelineError;
+use crate::curation::text::utf16_cmp;
+
+/// `/^(.+)-(dysfunctions|observations)$/`: the prefix and the suffix (`.` stops at a line
+/// terminator, as in JavaScript).
+fn prefixed(id: &str) -> Option<(&str, &str)> {
+ ["dysfunctions", "observations"]
+ .into_iter()
+ .find_map(|suffix| {
+ let prefix = id.strip_suffix(suffix)?.strip_suffix('-')?;
+ let terminators = ['\n', '\r', '\u{2028}', '\u{2029}'];
+ (!prefix.is_empty() && !prefix.contains(terminators)).then_some((prefix, suffix))
+ })
+}
+
+/// A prefixed category id that names another zone (sphenoid → ssb-…).
+fn foreign_prefix_anomalies(catalog: &LegacyCatalog) -> Result, PipelineError> {
+ let zone_ids: Vec<&str> = catalog.zones.iter().map(|(key, _)| key.as_str()).collect();
+ let mut out = Vec::new();
+ for (zone_id, zone) in &catalog.zones {
+ for category in categories(zone)? {
+ let category_id = text_field(category, "id")?;
+ let Some((prefix, suffix)) = prefixed(category_id) else {
+ continue;
+ };
+ if prefix == zone_id || !zone_ids.contains(&prefix) {
+ continue;
+ }
+ out.push(Anomaly::to_validate(
+ "finding.category.foreign_prefix",
+ format!("{zone_id}/{category_id}"),
+ format!(
+ "Identifiant de catégorie dupliqué entre {zone_id} et {prefix} ; \
+ {zone_id}-{suffix} attendu ; décision ostéopathe requise. \
+ Importé tel quel (non renommé)."
+ ),
+ ));
+ }
+ }
+ Ok(out)
+}
+
+fn child_ids(category: &Map) -> Result {
+ let ids: Result, PipelineError> = children(category)?
+ .into_iter()
+ .map(|child| text_field(child, "id"))
+ .collect();
+ Ok(ids?.join(", "))
+}
+
+/// Same `anamnesis` id everywhere, different content per zone family.
+fn anamnesis_variants(catalog: &LegacyCatalog) -> Result, PipelineError> {
+ let mut variants: Vec<(String, Vec)> = Vec::new();
+ for (zone_id, zone) in &catalog.zones {
+ let found = categories(zone)?
+ .into_iter()
+ .find(|c| c.get("id").and_then(Value::as_str) == Some("anamnesis"));
+ let Some(category) = found else { continue };
+ let key = child_ids(category)?;
+ match variants.iter_mut().find(|(known, _)| *known == key) {
+ Some((_, zones)) => zones.push(zone_id.clone()),
+ None => variants.push((key, vec![zone_id.clone()])),
+ }
+ }
+ let total = variants.len();
+ Ok(variants
+ .into_iter()
+ .enumerate()
+ .map(|(index, (children, zones))| {
+ Anomaly::informative(
+ "finding.anamnesis.variant",
+ format!("anamnesis variante {}/{total}", index + 1),
+ format!("Zones {} : {children}.", zones.join(", ")),
+ )
+ })
+ .collect())
+}
+
+fn collect_ids(
+ nodes: &[&Map],
+ into: &mut Vec,
+ seen: &mut HashSet,
+) -> Result<(), PipelineError> {
+ for node in nodes {
+ let id = text_field(node, "id")?;
+ if seen.insert(id.to_owned()) {
+ into.push(id.to_owned());
+ }
+ collect_ids(&children(node)?, into, seen)?;
+ }
+ Ok(())
+}
+
+/// Node ids are unique among siblings only; a finding is (zone, path).
+fn cross_zone_duplicates(catalog: &LegacyCatalog) -> Result, PipelineError> {
+ let mut zones_by_id: Vec<(String, usize)> = Vec::new();
+ for (_, zone) in &catalog.zones {
+ let (mut ids, mut seen) = (Vec::new(), HashSet::new());
+ collect_ids(&categories(zone)?, &mut ids, &mut seen)?;
+ for id in ids {
+ match zones_by_id.iter_mut().find(|(known, _)| *known == id) {
+ Some((_, count)) => *count += 1,
+ None => zones_by_id.push((id, 1)),
+ }
+ }
+ }
+ let shared: Vec<(String, usize)> = zones_by_id.into_iter().filter(|(_, n)| *n > 1).collect();
+ let mut top = shared.clone();
+ top.sort_by(|a, b| b.1.cmp(&a.1).then_with(|| utf16_cmp(&a.0, &b.0)));
+ let listed: Vec = top
+ .iter()
+ .take(8)
+ .map(|(id, n)| format!("{id} ({n})"))
+ .collect();
+ Ok(vec![Anomaly::informative(
+ "finding.id.shared_across_zones",
+ format!("{} identifiants", shared.len()),
+ format!(
+ "Identifiants de nœuds réutilisés entre zones (unicité entre frères seulement, \
+ adressage par zone + chemin). Les plus fréquents : {}.",
+ listed.join(", ")
+ ),
+ )])
+}
+
+pub fn catalog_anomalies(catalog: &LegacyCatalog) -> Result, PipelineError> {
+ let mut out = foreign_prefix_anomalies(catalog)?;
+ out.extend(anamnesis_variants(catalog)?);
+ out.extend(cross_zone_duplicates(catalog)?);
+ Ok(out)
+}
diff --git a/crates/core/src/curation/legacy/categories.rs b/crates/core/src/curation/legacy/categories.rs
new file mode 100644
index 00000000..9e0ed755
--- /dev/null
+++ b/crates/core/src/curation/legacy/categories.rs
@@ -0,0 +1,96 @@
+//! SVG annotation categories → canonical categories (`packages/anatomy/src/legacy/categories.ts`).
+//! The legacy editor slugified labels byte-wise, so accented letters became `-`; those slugs
+//! are normalised here and every change is reported; labels the importer writes are reported
+//! as "to validate".
+
+use serde_json::{Map, Value, json};
+
+use super::svg::LegacyAnnotation;
+use super::types::Anomaly;
+use crate::curation::text::js_object;
+
+/// Degraded legacy slug → real slug; other slugs are kept.
+pub fn normalise_category_id(legacy: &str) -> String {
+ match legacy {
+ "charni-res-vert-brales" => "charnieres-vertebrales",
+ "cadrans-du-cr-ne" => "cadrans-du-crane",
+ other => other,
+ }
+ .to_owned()
+}
+
+/// Labels authored by the importer, keyed by normalised slug.
+fn label(id: &str) -> Option<(&'static str, &'static str)> {
+ Some(match id {
+ "articulations" => ("Articulations", "Joints"),
+ "os" => ("Os", "Bones"),
+ "zone-colonne" => ("Zone colonne", "Spine"),
+ "charnieres-vertebrales" => ("Charnières vertébrales", "Vertebral hinges"),
+ "cadrans-du-crane" => ("Cadrans du crâne", "Skull quadrants"),
+ _ => return None,
+ })
+}
+
+#[derive(Debug, Clone, PartialEq)]
+pub struct MappedCategories {
+ pub categories: Vec,
+ pub id_map: Map,
+ pub anomalies: Vec,
+}
+
+/// `data-anno-category-name` is used only when it is a real label (it repeats the slug in
+/// the equine SVGs).
+fn french_name(legacy: &str, category_name: Option<&str>) -> String {
+ let id = normalise_category_id(legacy);
+ match category_name {
+ Some(name) if !name.is_empty() && name != legacy => name.to_owned(),
+ _ => label(&id).map_or(id, |(fr, _)| fr.to_owned()),
+ }
+}
+
+/// Category → its first non-null name, in order of first appearance (a JavaScript `Map`).
+fn first_seen(annotations: &[&LegacyAnnotation]) -> Vec<(String, Option)> {
+ let mut seen: Vec<(String, Option)> = Vec::new();
+ for a in annotations {
+ let Some(category) = &a.category else {
+ continue;
+ };
+ match seen.iter_mut().find(|(key, _)| key == category) {
+ Some((_, name @ None)) => name.clone_from(&a.category_name),
+ Some(_) => {}
+ None => seen.push((category.clone(), a.category_name.clone())),
+ }
+ }
+ seen
+}
+
+/// Categories in order of first appearance across the SVGs.
+pub fn map_categories(annotations: &[&LegacyAnnotation]) -> MappedCategories {
+ let mut categories = Vec::new();
+ let mut ids = Vec::new();
+ let mut anomalies = Vec::new();
+ for (legacy, category_name) in first_seen(annotations) {
+ let id = normalise_category_id(&legacy);
+ let fr = french_name(&legacy, category_name.as_deref());
+ let en = label(&id).map_or_else(|| id.clone(), |(_, en)| en.to_owned());
+ categories.push(json!({ "id": id, "name": { "fr": fr, "en": en }, "color": null }));
+ ids.push((legacy.clone(), Value::from(id.clone())));
+ if id != legacy {
+ anomalies.push(Anomaly::to_validate(
+ "category.slug.normalised",
+ legacy,
+ format!("Slug technique dégradé (accents perdus) normalisé en « {id} »."),
+ ));
+ }
+ anomalies.push(Anomaly::to_validate(
+ "category.label.authored",
+ id,
+ format!("Libellé écrit par l'importateur : FR « {fr} », EN « {en} »."),
+ ));
+ }
+ MappedCategories {
+ categories,
+ id_map: js_object(ids),
+ anomalies,
+ }
+}
diff --git a/crates/core/src/curation/legacy/importer.rs b/crates/core/src/curation/legacy/importer.rs
new file mode 100644
index 00000000..69e71a0c
--- /dev/null
+++ b/crates/core/src/curation/legacy/importer.rs
@@ -0,0 +1,370 @@
+//! The one-off, deterministic import of the historical equine assets into the first
+//! canonical sheet document (`packages/anatomy/src/legacy/importer.ts`). The result is a
+//! DRAFT for business review: every correction is listed in `anomalies`, nothing is
+//! published.
+
+use std::collections::HashMap;
+
+use serde_json::{Map, Value, json};
+
+use super::annotations::{AnnotationContext, MappedAnnotation, map_annotation, zone_name_key};
+use super::authored_labels::authored_label_anomalies;
+use super::catalog::{LegacyCatalog, MappedCatalog, map_catalog, parse_legacy_catalog};
+use super::catalog_checks::catalog_anomalies;
+use super::categories::{MappedCategories, map_categories};
+use super::svg::{ParsedSvg, parse_annotated_svg};
+use super::types::{
+ AnnotationsByView, Anomaly, AssetRecord, EquineLegacyInput, ImportResult, ImportStats,
+ SourceDigest, SvgSource,
+};
+use super::vertebrae::{VertebralFormula, parse_vertebral_formula, vertebral_regions};
+use crate::curation::PipelineError;
+use crate::curation::ids::{asset_id_from_sha256, sha256_hex, sheet_id_for};
+use crate::curation::text::js_object;
+use crate::domain::sheet::{
+ AnatomicalSheetDocument, SHEET_DOCUMENT_SCHEMA_VERSION, validate_sheet_document,
+};
+
+pub const EQUINE_SPECIES_ID: &str = "equine";
+pub const EQUINE_SHEET_VERSION: u32 = 1;
+pub const MAIN_VIEW_ID: &str = "main";
+pub const HEAD_VIEW_ID: &str = "head";
+
+struct ParsedSources {
+ assets: Vec,
+ main: ParsedSvg,
+ head: ParsedSvg,
+ catalog: LegacyCatalog,
+ formula: VertebralFormula,
+}
+
+struct Mapped {
+ annotations: Vec,
+ categories: MappedCategories,
+ catalog: MappedCatalog,
+}
+
+fn asset_record(source: &SvgSource) -> AssetRecord {
+ let sha256 = sha256_hex(&source.bytes);
+ AssetRecord {
+ file: source.file_name.clone(),
+ svg_asset_id: asset_id_from_sha256(&sha256),
+ sha256,
+ byte_size: source.bytes.len(),
+ }
+}
+
+/// `Buffer.toString('utf8')`: invalid bytes become U+FFFD, a BOM is kept.
+fn lossy_text(bytes: &[u8]) -> String {
+ String::from_utf8_lossy(bytes).into_owned()
+}
+
+fn text_digest(file: &str, text: &str) -> SourceDigest {
+ SourceDigest {
+ file: file.to_owned(),
+ sha256: sha256_hex(text.as_bytes()),
+ byte_size: text.len(),
+ }
+}
+
+/// `new TextDecoder('utf-8', { fatal: true })`: invalid UTF-8 refused, one BOM removed.
+fn svg_text(source: &SvgSource) -> Result<&str, PipelineError> {
+ let text = std::str::from_utf8(&source.bytes)
+ .map_err(|_| PipelineError::Source(format!("{}: invalid UTF-8", source.file_name)))?;
+ Ok(text.strip_prefix('\u{FEFF}').unwrap_or(text))
+}
+
+fn parse_sources(input: &EquineLegacyInput) -> Result {
+ Ok(ParsedSources {
+ assets: vec![asset_record(&input.main_svg), asset_record(&input.head_svg)],
+ main: parse_annotated_svg(svg_text(&input.main_svg)?, &input.main_svg.file_name)?,
+ head: parse_annotated_svg(svg_text(&input.head_svg)?, &input.head_svg.file_name)?,
+ catalog: parse_legacy_catalog(&lossy_text(&input.catalog))?,
+ formula: parse_vertebral_formula(&lossy_text(&input.vertebrae))?,
+ })
+}
+
+/// `${value}` of a zone name part.
+fn template(value: Option<&Value>) -> String {
+ match value {
+ None => "undefined".to_owned(),
+ Some(Value::String(text)) => text.clone(),
+ Some(other) => other.to_string(),
+ }
+}
+
+fn zone_names(zones: &[Value]) -> HashMap {
+ let mut names = HashMap::new();
+ for zone in zones {
+ let fr = template(zone.pointer("/name/fr"));
+ let en = template(zone.pointer("/name/en"));
+ let id = zone.get("id").and_then(Value::as_str).unwrap_or_default();
+ names.insert(zone_name_key(&fr, &en), id.to_owned());
+ }
+ names
+}
+
+fn map_sources(sources: &ParsedSources) -> Result {
+ let catalog = map_catalog(&sources.catalog)?;
+ let zone_by_names = zone_names(&catalog.zones);
+ let views = [(MAIN_VIEW_ID, &sources.main), (HEAD_VIEW_ID, &sources.head)];
+ let mut annotations = Vec::new();
+ for (view_id, svg) in views {
+ let ctx = AnnotationContext {
+ view_id,
+ zone_by_names: &zone_by_names,
+ };
+ annotations.extend(svg.annotations.iter().map(|a| map_annotation(a, &ctx)));
+ }
+ let legacy: Vec<_> = sources
+ .main
+ .annotations
+ .iter()
+ .chain(&sources.head.annotations)
+ .collect();
+ Ok(Mapped {
+ annotations,
+ categories: map_categories(&legacy),
+ catalog,
+ })
+}
+
+fn build_views(assets: &[AssetRecord]) -> Value {
+ json!([
+ {
+ "id": MAIN_VIEW_ID, "kind": "main",
+ "name": { "fr": "Vue générale", "en": "Overview" },
+ "svg_asset_id": assets[0].svg_asset_id, "view_box": null,
+ },
+ {
+ "id": HEAD_VIEW_ID, "kind": "detail", "parent_view_id": MAIN_VIEW_ID,
+ "name": { "fr": "Tête", "en": "Head" },
+ "svg_asset_id": assets[1].svg_asset_id, "view_box": null,
+ },
+ ])
+}
+
+fn build_document(sources: &ParsedSources, mapped: &Mapped) -> Value {
+ let disclaimer = sources.catalog.get("scope_disclaimer");
+ let notes = format!(
+ "Import des données historiques: Horse.svg, Horse_head.svg, pathologies.json (schema {})",
+ template(sources.catalog.get("schema_version"))
+ );
+ let annotations: Vec<&Value> = mapped.annotations.iter().map(|m| &m.annotation).collect();
+ json!({
+ "schema_version": SHEET_DOCUMENT_SCHEMA_VERSION,
+ "species_id": EQUINE_SPECIES_ID,
+ "name": { "fr": "Cheval", "en": "Horse" },
+ "description": {
+ "fr": "Fiche anatomique officielle Faunosteo",
+ "en": "Official Faunosteo anatomical sheet",
+ },
+ "scope_disclaimer": disclaimer.map_or_else(|| json!({}), |text| json!({ "fr": text })),
+ "attribution": { "authors": ["Faunosteo"], "license": null, "notes": notes },
+ "vertebral_config": { "regions": vertebral_regions(&sources.formula) },
+ "references": [],
+ "categories": mapped.categories.categories,
+ "views": build_views(&sources.assets),
+ "annotations": annotations,
+ "finding_zones": mapped.catalog.zones,
+ })
+}
+
+fn build_id_map(sources: &ParsedSources, mapped: &Mapped) -> Map {
+ let annotations = mapped
+ .annotations
+ .iter()
+ .map(|m| (m.row.legacy_id.clone(), Value::from(m.row.new_id.clone())));
+ let assets = sources
+ .assets
+ .iter()
+ .map(|a| (a.file.clone(), Value::from(a.svg_asset_id.clone())));
+ let mut map = Map::new();
+ map.insert("views".into(), json!({ "head": "head" }));
+ map.insert("annotations".into(), Value::Object(js_object(annotations)));
+ map.insert(
+ "categories".into(),
+ Value::Object(mapped.categories.id_map.clone()),
+ );
+ map.insert(
+ "finding_nodes".into(),
+ Value::Object(mapped.catalog.node_id_map.clone()),
+ );
+ map.insert("assets".into(), Value::Object(js_object(assets)));
+ map
+}
+
+fn count(annotations: &[Value], test: impl Fn(&Value) -> bool) -> usize {
+ annotations.iter().filter(|a| test(a)).count()
+}
+
+fn is(annotation: &Value, key: &str, value: &str) -> bool {
+ annotation.get(key).and_then(Value::as_str) == Some(value)
+}
+
+fn present(annotation: &Value, key: &str) -> bool {
+ !is(annotation, "kind", "detail_trigger") && annotation.get(key).is_some_and(|v| !v.is_null())
+}
+
+fn build_stats(sources: &ParsedSources, document: &Value, mapped: &Mapped) -> ImportStats {
+ let annotations = document["annotations"]
+ .as_array()
+ .map_or(&[][..], Vec::as_slice);
+ let totals = &mapped.catalog.zone_stats;
+ ImportStats {
+ legacy_annotations: sources.main.annotations.len() + sources.head.annotations.len(),
+ annotations: annotations.len(),
+ annotations_by_view: AnnotationsByView {
+ main: count(annotations, |a| is(a, "view_id", MAIN_VIEW_ID)),
+ head: count(annotations, |a| is(a, "view_id", HEAD_VIEW_ID)),
+ },
+ linked_annotations: count(annotations, |a| present(a, "finding_zone_id")),
+ triggers: count(annotations, |a| is(a, "kind", "detail_trigger")),
+ views: document["views"].as_array().map_or(0, Vec::len),
+ vertebral_annotations: count(annotations, |a| present(a, "vertebral_region_code")),
+ bilateral: count(annotations, |a| is(a, "laterality", "bilateral")),
+ finding_zones: document["finding_zones"].as_array().map_or(0, Vec::len),
+ nodes: totals.iter().map(|z| z.nodes).sum(),
+ leaves: totals.iter().map(|z| z.leaves).sum(),
+ vet_referrals: totals.iter().map(|z| z.vet_referrals).sum(),
+ missing_description_en: mapped.catalog.missing_description_en,
+ sources: mapped.catalog.sources,
+ }
+}
+
+/// Every `data-anno-id` read must be written once; no inline legacy source may be dropped.
+fn assert_guards(stats: &ImportStats) -> Result<(), PipelineError> {
+ if stats.legacy_annotations != stats.annotations {
+ return Err(PipelineError::Guard(format!(
+ "{} annotations read, {} written",
+ stats.legacy_annotations, stats.annotations
+ )));
+ }
+ if stats.sources != 0 {
+ return Err(PipelineError::Guard(format!(
+ "{} inline legacy sources found: map them to references",
+ stats.sources
+ )));
+ }
+ Ok(())
+}
+
+fn source_anomalies(sources: &ParsedSources, stats: &ImportStats) -> Vec {
+ let mut out = Vec::new();
+ if stats.missing_description_en > 0 {
+ out.push(Anomaly::to_validate(
+ "finding.description_en.missing",
+ "pathologies.json",
+ format!(
+ "{} descriptions sans traduction anglaise (clé « en » absente, rien d'inventé).",
+ stats.missing_description_en
+ ),
+ ));
+ }
+ if sources.catalog.get("scope_disclaimer").is_some() {
+ out.push(Anomaly::to_validate(
+ "document.scope_disclaimer.no_en",
+ "scope_disclaimer",
+ "Texte de périmètre légal en français seulement : traduction anglaise à rédiger.",
+ ));
+ }
+ out.push(capped_anomaly(&sources.formula));
+ if stats.sources == 0 {
+ out.push(Anomaly::to_validate(
+ "finding.sources.none",
+ "pathologies.json",
+ "Aucune source bibliographique sur les nœuds, alors que la règle projet en demande \
+ au moins une par entrée.",
+ ));
+ }
+ out
+}
+
+fn capped_anomaly(formula: &VertebralFormula) -> Anomaly {
+ let range = formula.varies_range.as_deref().unwrap_or("non précisée");
+ let caudal = formula.counts.get("Cd").copied().unwrap_or(f64::NAN);
+ Anomaly::to_validate(
+ "vertebral.cd.capped",
+ "Cd",
+ format!(
+ "Caudales plafonnées à {} pour le sélecteur, alors que l'espèce varie ({range}) \
+ selon le commentaire source.",
+ super::svg::js_text(caudal)
+ ),
+ )
+}
+
+fn informative_notes() -> Anomaly {
+ Anomaly::informative(
+ "annotation.geometry.hitbox",
+ "géométrie",
+ "Géométrie lue sur le premier élément de chaque annotation (zone de clic), le marqueur \
+ visuel n'est pas importé.",
+ )
+}
+
+fn collect_anomalies(
+ sources: &ParsedSources,
+ mapped: &Mapped,
+ stats: &ImportStats,
+ document: &Value,
+) -> Result, PipelineError> {
+ let mut out = authored_label_anomalies(document);
+ out.extend(sources.main.anomalies.iter().cloned());
+ out.extend(sources.head.anomalies.iter().cloned());
+ out.extend(
+ mapped
+ .annotations
+ .iter()
+ .flat_map(|m| m.anomalies.iter().cloned()),
+ );
+ out.extend(mapped.categories.anomalies.iter().cloned());
+ out.extend(mapped.catalog.anomalies.iter().cloned());
+ out.extend(catalog_anomalies(&sources.catalog)?);
+ out.extend(source_anomalies(sources, stats));
+ out.push(informative_notes());
+ Ok(out)
+}
+
+/// `AnatomicalSheetDocumentSchema.parse`: the document must be a valid sheet document.
+fn parse_document(document: &Value) -> Result {
+ serde_json::from_value(document.clone())
+ .map_err(|error| PipelineError::Guard(format!("document schema: {error}")))
+}
+
+pub fn import_equine_legacy(input: &EquineLegacyInput) -> Result {
+ let sources = parse_sources(input)?;
+ let mapped = map_sources(&sources)?;
+ let document = build_document(&sources, &mapped);
+ let parsed = parse_document(&document)?;
+ let stats = build_stats(&sources, &document, &mapped);
+ assert_guards(&stats)?;
+ Ok(ImportResult {
+ legacy_id_map: build_id_map(&sources, &mapped),
+ source_digests: vec![
+ text_digest("pathologies.json", &lossy_text(&input.catalog)),
+ text_digest("vertebrae.ts", &lossy_text(&input.vertebrae)),
+ ],
+ sheet_id: sheet_id_for(EQUINE_SPECIES_ID, EQUINE_SHEET_VERSION),
+ view_boxes: view_boxes(&sources),
+ annotation_rows: mapped.annotations.iter().map(|m| m.row.clone()).collect(),
+ region_inferences: mapped
+ .annotations
+ .iter()
+ .filter_map(|m| m.region.clone())
+ .collect(),
+ zone_stats: mapped.catalog.zone_stats.clone(),
+ anomalies: collect_anomalies(&sources, &mapped, &stats, &document)?,
+ issues: validate_sheet_document(&parsed),
+ assets: sources.assets,
+ stats,
+ document,
+ })
+}
+
+fn view_boxes(sources: &ParsedSources) -> Map {
+ let mut map = Map::new();
+ map.insert(MAIN_VIEW_ID.into(), json!(sources.main.view_box));
+ map.insert(HEAD_VIEW_ID.into(), json!(sources.head.view_box));
+ map
+}
diff --git a/crates/core/src/curation/legacy/mod.rs b/crates/core/src/curation/legacy/mod.rs
new file mode 100644
index 00000000..71e61201
--- /dev/null
+++ b/crates/core/src/curation/legacy/mod.rs
@@ -0,0 +1,42 @@
+//! The legacy importer (R5.2): the historical equine files → the generated
+//! `legacy-import/` folder (`packages/anatomy/src/legacy`). Parity with the TypeScript is
+//! proven by `tests/curation_legacy.rs` against the committed folder and `oracles/r5`.
+
+pub mod annotations;
+pub mod authored_labels;
+pub mod catalog;
+pub mod catalog_checks;
+pub mod categories;
+pub mod importer;
+pub mod regions;
+pub mod report;
+pub mod svg;
+pub mod types;
+pub mod vertebrae;
+
+use serde_json::Value;
+
+use super::PipelineError;
+use super::markdown::format_markdown;
+use super::prettier_json::format_json;
+pub use importer::{HEAD_VIEW_ID, MAIN_VIEW_ID, import_equine_legacy};
+pub use types::{EquineLegacyInput, ImportResult, SvgSource};
+
+/// The text files of `legacy-import/`, in the order the TypeScript wrote them
+/// (`renderArtifacts`); the two SVG copies are byte copies the caller makes.
+pub fn render_artifacts(result: &ImportResult) -> Result, PipelineError> {
+ let assets = serde_json::to_value(&result.assets)
+ .map_err(|error| PipelineError::Source(format!("assets.json: {error}")))?;
+ Ok(vec![
+ ("document.json".into(), format_json(&result.document)?),
+ (
+ "legacy-id-map.json".into(),
+ format_json(&Value::Object(result.legacy_id_map.clone()))?,
+ ),
+ ("assets.json".into(), format_json(&assets)?),
+ (
+ "IMPORT-REPORT.md".into(),
+ format_markdown(&report::render_import_report(result)?)?,
+ ),
+ ])
+}
diff --git a/crates/core/src/curation/legacy/regions.rs b/crates/core/src/curation/legacy/regions.rs
new file mode 100644
index 00000000..80a20480
--- /dev/null
+++ b/crates/core/src/curation/legacy/regions.rs
@@ -0,0 +1,43 @@
+//! Spine region inference from legacy labels (`packages/anatomy/src/legacy/regions.ts`),
+//! kept in the importer only: FR label first, then EN, first root in test order wins.
+
+use crate::curation::text::fold;
+
+const REGION_ROOTS: [(&str, &[&str]); 5] = [
+ ("C", &["cervic"]),
+ ("T", &["thorac", "dors"]),
+ ("L", &["lombair", "lumbar"]),
+ ("S", &["sacral", "sacre"]),
+ ("Cd", &["caudal", "coccyg"]),
+];
+
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct RegionMatch {
+ pub code: String,
+ pub root: String,
+ pub label: String,
+}
+
+fn match_label(label: &str) -> Option {
+ let text = fold(label);
+ REGION_ROOTS.iter().find_map(|(code, roots)| {
+ roots
+ .iter()
+ .find(|root| text.contains(*root))
+ .map(|root| RegionMatch {
+ code: (*code).to_owned(),
+ root: (*root).to_owned(),
+ label: label.to_owned(),
+ })
+ })
+}
+
+/// `None` when neither label carries a known root; an empty label is skipped, as `Boolean`
+/// filters it.
+pub fn infer_region_code(name_fr: Option<&str>, name_en: Option<&str>) -> Option {
+ [name_fr, name_en]
+ .into_iter()
+ .flatten()
+ .filter(|label| !label.is_empty())
+ .find_map(match_label)
+}
diff --git a/crates/core/src/curation/legacy/report.rs b/crates/core/src/curation/legacy/report.rs
new file mode 100644
index 00000000..83fa96f8
--- /dev/null
+++ b/crates/core/src/curation/legacy/report.rs
@@ -0,0 +1,518 @@
+//! The French Markdown import report (`packages/anatomy/src/legacy/report.ts`), written for
+//! an osteopath (what to validate) and for the developer (what was mapped and how). No dates
+//! and no environment data: a committed, byte-stable artifact. Returns the raw text; the
+//! artifact step aligns its tables as Prettier did.
+
+use std::collections::{HashMap, HashSet};
+
+use serde_json::{Map, Value};
+
+use super::authored_labels::{AUTHORED_LABEL_CODE, region_label_subject};
+use super::catalog::count_tree;
+use super::types::{Anomaly, ImportResult};
+use crate::curation::PipelineError;
+use crate::curation::markdown::{Cell, code, table};
+use crate::domain::sheet::{SHEET_DOCUMENT_SCHEMA_VERSION, SheetIssueSeverity};
+
+fn intro(result: &ImportResult) -> String {
+ let species = result.document["species_id"].as_str().unwrap_or_default();
+ [
+ "# Rapport d'import : fiche anatomique équine v1".to_owned(),
+ String::new(),
+ "Généré par `pnpm import:equine` à partir des données historiques. Ne pas modifier à \
+ la main : relancer la commande."
+ .to_owned(),
+ String::new(),
+ format!(
+ "Statut : **brouillon** (version 1 en `draft`), jamais publié par le seed. Fiche \
+ `{}`, espèce `{species}`.",
+ result.sheet_id
+ ),
+ String::new(),
+ "Les éléments de la section « Anomalies à valider » demandent une décision métier \
+ avant toute publication."
+ .to_owned(),
+ ]
+ .join("\n")
+}
+
+fn sources_section(result: &ImportResult) -> String {
+ let mut rows: Vec> = result
+ .assets
+ .iter()
+ .map(|a| {
+ vec![
+ code(Some(&a.file)).into(),
+ a.byte_size.into(),
+ code(Some(&a.sha256)).into(),
+ code(Some(&a.svg_asset_id)).into(),
+ ]
+ })
+ .collect();
+ rows.extend(result.source_digests.iter().map(|d| {
+ vec![
+ code(Some(&d.file)).into(),
+ d.byte_size.into(),
+ code(Some(&d.sha256)).into(),
+ "-".into(),
+ ]
+ }));
+ let mut lines = vec![
+ "## 1. Sources et empreintes".to_owned(),
+ String::new(),
+ table(&["Fichier", "Octets", "sha256", "Id d'asset"], &rows),
+ String::new(),
+ "Les ids d'asset sont dérivés du sha256 du fichier (UUID déterministe), les deux SVG \
+ sont copiés à l'identique dans `assets/`."
+ .to_owned(),
+ String::new(),
+ ];
+ for (view, view_box) in &result.view_boxes {
+ let view_box = view_box.as_str().unwrap_or("absent");
+ lines.push(format!(
+ "- vue `{view}` : viewBox `{view_box}` (view_box du document laissé à null, le SVG \
+ porte le sien)"
+ ));
+ }
+ lines.join("\n")
+}
+
+fn categories_of(document: &Value) -> Vec<&Map> {
+ let zones = document["finding_zones"]
+ .as_array()
+ .map_or(&[][..], Vec::as_slice);
+ zones
+ .iter()
+ .filter_map(|zone| zone["categories"].as_array())
+ .flatten()
+ .filter_map(Value::as_object)
+ .collect()
+}
+
+fn volumes_section(result: &ImportResult) -> Result {
+ let s = &result.stats;
+ let out = count_tree(&categories_of(&result.document))?;
+ let rows: Vec> = [
+ ("Annotations", s.annotations),
+ ("dont vue `main` (Horse.svg)", s.annotations_by_view.main),
+ (
+ "dont vue `head` (Horse_head.svg)",
+ s.annotations_by_view.head,
+ ),
+ (
+ "Annotations reliées à une zone de constats",
+ s.linked_annotations,
+ ),
+ ("Déclencheurs de vue détail", s.triggers),
+ ("Vues", s.views),
+ (
+ "Zones vertébrales avec code de région",
+ s.vertebral_annotations,
+ ),
+ ("Annotations bilatérales", s.bilateral),
+ ("Zones de constats (catalogue)", s.finding_zones),
+ ("Nœuds (catégories incluses)", s.nodes),
+ ("Feuilles", s.leaves),
+ ("Orientations vétérinaires", s.vet_referrals),
+ ]
+ .into_iter()
+ .map(|(label, value)| vec![label.into(), value.into()])
+ .collect();
+ let checks: Vec> = [
+ (
+ "Annotations (data-anno-id distincts)",
+ s.legacy_annotations,
+ s.annotations,
+ ),
+ ("Nœuds (catégories incluses)", s.nodes, out.nodes),
+ ("Feuilles", s.leaves, out.leaves),
+ (
+ "Orientations vétérinaires",
+ s.vet_referrals,
+ out.vet_referrals,
+ ),
+ ]
+ .into_iter()
+ .map(|(label, read, written)| vec![label.into(), read.into(), written.into()])
+ .collect();
+ Ok([
+ "## 2. Volumes".to_owned(),
+ String::new(),
+ table(&["Mesure", "Valeur"], &rows),
+ String::new(),
+ "Contrôle entrée = sortie, compté des deux côtés (sources lues, document écrit). \
+ L'importateur s'arrête au moindre écart :"
+ .to_owned(),
+ String::new(),
+ table(&["Contrôle", "Lu (sources)", "Écrit (document)"], &checks),
+ ]
+ .join("\n"))
+}
+
+fn annotations_section(result: &ImportResult) -> String {
+ let rows: Vec> = result
+ .annotation_rows
+ .iter()
+ .map(|r| {
+ vec![
+ code(Some(&r.legacy_id)).into(),
+ code(Some(&r.new_id)).into(),
+ r.view_id.as_str().into(),
+ r.kind.as_str().into(),
+ r.laterality.as_str().into(),
+ r.region_code.clone().into(),
+ code(r.finding_zone_id.as_deref()).into(),
+ r.geometry.as_str().into(),
+ ]
+ })
+ .collect();
+ let head = [
+ "Id historique",
+ "Nouvel id",
+ "Vue",
+ "Type",
+ "Latéralité",
+ "Région",
+ "Zone de constats",
+ "Géométrie",
+ ];
+ [
+ "## 3. Annotations".to_owned(),
+ String::new(),
+ table(&head, &rows),
+ ]
+ .join("\n")
+}
+
+fn authored_subjects<'a>(anomalies: &'a [Anomaly], code: &str) -> HashSet<&'a str> {
+ anomalies
+ .iter()
+ .filter(|a| a.code == code)
+ .map(|a| a.subject.as_str())
+ .collect()
+}
+
+fn text_at<'a>(value: &'a Value, pointer: &str) -> &'a str {
+ value
+ .pointer(pointer)
+ .and_then(Value::as_str)
+ .unwrap_or_default()
+}
+
+fn categories_section(result: &ImportResult) -> String {
+ let legacy_of: HashMap<&str, &str> = result.legacy_id_map["categories"]
+ .as_object()
+ .map(|map| {
+ map.iter()
+ .filter_map(|(k, v)| Some((v.as_str()?, k.as_str())))
+ .collect()
+ })
+ .unwrap_or_default();
+ let authored = authored_subjects(&result.anomalies, "category.label.authored");
+ let categories = result.document["categories"]
+ .as_array()
+ .map_or(&[][..], Vec::as_slice);
+ let rows: Vec> = categories
+ .iter()
+ .map(|c| {
+ let id = text_at(c, "/id");
+ vec![
+ code(Some(legacy_of.get(id).copied().unwrap_or(id))).into(),
+ code(Some(id)).into(),
+ text_at(c, "/name/fr").into(),
+ text_at(c, "/name/en").into(),
+ if authored.contains(id) {
+ "importateur, à valider"
+ } else {
+ "SVG"
+ }
+ .into(),
+ ]
+ })
+ .collect();
+ [
+ "## 4. Catégories d'annotation".to_owned(),
+ String::new(),
+ table(
+ &[
+ "Id SVG",
+ "Id",
+ "Libellé FR",
+ "Libellé EN",
+ "Origine du libellé",
+ ],
+ &rows,
+ ),
+ String::new(),
+ "Couleur : `null` partout (les couleurs du SVG restent dans l'asset). Dans les SVG, \
+ `data-anno-category-name` répète le slug (`articulations`, `os`) : ce n'est pas un \
+ libellé, le libellé lisible de la table de l'importateur est utilisé."
+ .to_owned(),
+ ]
+ .join("\n")
+}
+
+fn region_rows(result: &ImportResult) -> Vec> {
+ let authored = authored_subjects(&result.anomalies, AUTHORED_LABEL_CODE);
+ let regions = result
+ .document
+ .pointer("/vertebral_config/regions")
+ .and_then(Value::as_array)
+ .map_or(&[][..], Vec::as_slice);
+ regions
+ .iter()
+ .map(|r| {
+ let code_text = text_at(r, "/code");
+ let count = r["count"].as_f64().map_or(Cell::Null, Cell::Number);
+ let origin = authored.contains(region_label_subject(code_text).as_str());
+ vec![
+ code(Some(code_text)).into(),
+ text_at(r, "/name/fr").into(),
+ text_at(r, "/name/en").into(),
+ count,
+ if origin {
+ "importateur, à valider"
+ } else {
+ "source"
+ }
+ .into(),
+ ]
+ })
+ .collect()
+}
+
+fn vertebral_section(result: &ImportResult) -> String {
+ let inferences: Vec> = result
+ .region_inferences
+ .iter()
+ .map(|i| {
+ let new_id = result.legacy_id_map["annotations"][&i.annotation_id].as_str();
+ vec![
+ code(Some(new_id.unwrap_or(&i.annotation_id))).into(),
+ i.label.as_str().into(),
+ code(i.root.as_deref()).into(),
+ code(i.code.as_deref()).into(),
+ ]
+ })
+ .collect();
+ [
+ "## 5. Configuration vertébrale".to_owned(),
+ String::new(),
+ table(
+ &[
+ "Code",
+ "Nom FR",
+ "Nom EN",
+ "Vertèbres",
+ "Origine du libellé",
+ ],
+ ®ion_rows(result),
+ ),
+ String::new(),
+ "Nombres de vertèbres : `vertebrae.ts`. Noms de région : écrits par l'importateur \
+ (aucune source ne les porte)."
+ .to_owned(),
+ String::new(),
+ "Codes de région déduits des libellés (racines `cervic`, `thorac`/`dors`, \
+ `lombair`/`lumbar`, `sacral`/`sacre`, `caudal`/`coccyg`, FR puis EN). Déduction faite \
+ une fois ici, le document porte le code :"
+ .to_owned(),
+ String::new(),
+ table(&["Annotation", "Libellé lu", "Racine", "Code"], &inferences),
+ ]
+ .join("\n")
+}
+
+fn findings_section(result: &ImportResult) -> String {
+ let zones = result.document["finding_zones"]
+ .as_array()
+ .map_or(&[][..], Vec::as_slice);
+ let names: HashMap<&str, &str> = zones
+ .iter()
+ .map(|z| (text_at(z, "/id"), text_at(z, "/name/fr")))
+ .collect();
+ let mut rows: Vec> = result
+ .zone_stats
+ .iter()
+ .map(|z| {
+ vec![
+ code(Some(&z.zone_id)).into(),
+ names.get(z.zone_id.as_str()).copied().unwrap_or("-").into(),
+ z.nodes.into(),
+ z.leaves.into(),
+ z.vet_referrals.into(),
+ ]
+ })
+ .collect();
+ let s = &result.stats;
+ rows.push(vec![
+ "**Total**".into(),
+ "".into(),
+ s.nodes.into(),
+ s.leaves.into(),
+ s.vet_referrals.into(),
+ ]);
+ [
+ "## 6. Arbre de constats par zone".to_owned(),
+ String::new(),
+ "Copie fidèle du catalogue : ids, libellés, descriptions présentes, orientations \
+ vétérinaires et sources. Ordre des zones et des catégories conservé. Nœuds = \
+ catégories incluses."
+ .to_owned(),
+ String::new(),
+ table(
+ &["Zone", "Nom", "Nœuds", "Feuilles", "Orientations vét."],
+ &rows,
+ ),
+ ]
+ .join("\n")
+}
+
+fn anomaly_rows(anomalies: &[&Anomaly]) -> Vec> {
+ anomalies
+ .iter()
+ .map(|a| {
+ vec![
+ code(Some(&a.code)).into(),
+ a.subject.as_str().into(),
+ a.message.as_str().into(),
+ ]
+ })
+ .collect()
+}
+
+fn anomalies_section(result: &ImportResult) -> String {
+ let of_level = |level: &str| -> Vec<&Anomaly> {
+ result
+ .anomalies
+ .iter()
+ .filter(|a| a.level == level)
+ .collect()
+ };
+ let (to_validate, informative) = (of_level("a_valider"), of_level("informatif"));
+ let head = ["Code", "Objet", "Détail"];
+ [
+ "## 7. Anomalies à valider".to_owned(),
+ String::new(),
+ format!(
+ "{} points demandent une décision, {} sont informatifs.",
+ to_validate.len(),
+ informative.len()
+ ),
+ String::new(),
+ "### À valider".to_owned(),
+ String::new(),
+ table(&head, &anomaly_rows(&to_validate)),
+ String::new(),
+ "### Informatif".to_owned(),
+ String::new(),
+ table(&head, &anomaly_rows(&informative)),
+ ]
+ .join("\n")
+}
+
+/// Source keys the document does not carry, and where they live now.
+fn not_carried_over() -> [String; 4] {
+ [
+ format!(
+ "`schema_version` du catalogue (`2.0`) : version du format historique, remplacée \
+ par `schema_version: {SHEET_DOCUMENT_SCHEMA_VERSION}` du document canonique ; la \
+ valeur d'origine est citée dans `attribution.notes`."
+ ),
+ "bloc `species` du catalogue (`equine`, Cheval / Horse) : porté par `species_id` du \
+ document et par la table `public.species` ; le nom de la fiche est écrit séparément \
+ (voir anomalies `label.authored`)."
+ .to_owned(),
+ "second élément graphique de chaque annotation (marqueur visuel) : reste dans l'asset \
+ SVG copié à l'identique, le document ne garde que la zone de clic."
+ .to_owned(),
+ "`data-anno-category-name` : répète le slug technique (`articulations`, `os`), ce \
+ n'est pas un libellé ; le libellé de catégorie vient de la table de l'importateur \
+ (section 4)."
+ .to_owned(),
+ ]
+}
+
+fn not_imported_section(result: &ImportResult) -> String {
+ let dropped: Vec = result
+ .annotation_rows
+ .iter()
+ .filter(|r| r.geometry == "aucune")
+ .map(|r| {
+ format!(
+ "- géométrie de `{}` (forme non prise en charge)",
+ r.legacy_id
+ )
+ })
+ .collect();
+ let mut lines = vec!["## 8. Données non importées".to_owned(), String::new()];
+ if dropped.is_empty() {
+ lines.push("Aucune donnée métier écartée.".to_owned());
+ }
+ lines.extend(dropped);
+ lines.extend([
+ String::new(),
+ "Non repris par construction :".to_owned(),
+ String::new(),
+ ]);
+ lines.extend(not_carried_over().iter().map(|line| format!("- {line}")));
+ lines.join("\n")
+}
+
+fn validation_section(result: &ImportResult) -> String {
+ let count = |severity: SheetIssueSeverity| {
+ result
+ .issues
+ .iter()
+ .filter(|i| i.severity == severity)
+ .count()
+ };
+ let rows: Vec> = result
+ .issues
+ .iter()
+ .map(|i| {
+ let severity = match i.severity {
+ SheetIssueSeverity::Error => "error",
+ SheetIssueSeverity::Warning => "warning",
+ };
+ vec![
+ severity.into(),
+ code(Some(&i.code)).into(),
+ code(Some(&i.path)).into(),
+ i.message.as_str().into(),
+ ]
+ })
+ .collect();
+ let mut lines = vec![
+ "## 9. Résultat de validation".to_owned(),
+ String::new(),
+ "- Forme (`AnatomicalSheetDocumentSchema`) : conforme.".to_owned(),
+ format!(
+ "- Intégrité (`validateSheetDocument`) : {} erreur(s), {} avertissement(s).",
+ count(SheetIssueSeverity::Error),
+ count(SheetIssueSeverity::Warning)
+ ),
+ ];
+ if !rows.is_empty() {
+ lines.push(String::new());
+ lines.push(table(&["Gravité", "Code", "Chemin", "Message"], &rows));
+ }
+ lines.join("\n")
+}
+
+/// `renderImportReport`: the raw report, before Prettier aligns its tables.
+pub fn render_import_report(result: &ImportResult) -> Result {
+ let sections = [
+ intro(result),
+ sources_section(result),
+ volumes_section(result)?,
+ annotations_section(result),
+ categories_section(result),
+ vertebral_section(result),
+ findings_section(result),
+ anomalies_section(result),
+ not_imported_section(result),
+ validation_section(result),
+ ];
+ Ok(format!("{}\n", sections.join("\n\n")))
+}
diff --git a/crates/core/src/curation/legacy/svg.rs b/crates/core/src/curation/legacy/svg.rs
new file mode 100644
index 00000000..d47ff801
--- /dev/null
+++ b/crates/core/src/curation/legacy/svg.rs
@@ -0,0 +1,192 @@
+//! The annotation layer of a legacy SVG (`packages/anatomy/src/legacy/svg.ts`): the
+//! `data-anno-*` attributes of the historical files. Each annotation is drawn by one or two
+//! elements sharing a `data-anno-id`; one record per id, attributes and geometry from the
+//! first element, document order kept.
+//!
+//! Declared difference: xmldom parsed leniently; the text is checked here by the core's SVG
+//! caps (no DTD, bounded depth and size) and parsed by roxmltree, so a malformed file is
+//! refused instead of read partially.
+
+use std::collections::HashMap;
+
+use serde_json::{Value, json};
+
+use super::types::Anomaly;
+use crate::anatomy::svg::number::{js_number_of, js_split_list};
+use crate::anatomy::svg::prescan::{SVG_CAPS, prescan};
+use crate::curation::PipelineError;
+use crate::json::js_number;
+
+/// A legacy geometry: circle and polygon are the only shapes the legacy editor produces.
+#[derive(Debug, Clone, PartialEq)]
+pub enum LegacyGeometry {
+ Circle { cx: f64, cy: f64, r: f64 },
+ Polygon { points: Vec<(f64, f64)> },
+}
+
+impl LegacyGeometry {
+ /// The document form; a value JSON cannot hold (NaN, ±Infinity) becomes `null`, as
+ /// `JSON.stringify` writes it.
+ pub fn to_value(&self) -> Value {
+ match self {
+ Self::Circle { cx, cy, r } => json!({
+ "type": "circle", "cx": number(*cx), "cy": number(*cy), "r": number(*r)
+ }),
+ Self::Polygon { points } => {
+ let pairs: Vec = points
+ .iter()
+ .map(|&(x, y)| Value::Array(vec![number(x), number(y)]))
+ .collect();
+ json!({ "type": "polygon", "points": pairs })
+ }
+ }
+ }
+
+ /// `describeGeometry` of the annotation rows.
+ pub fn describe(geometry: Option<&Self>) -> String {
+ match geometry {
+ None => "aucune".to_owned(),
+ Some(Self::Circle { cx, cy, r }) => {
+ format!(
+ "cercle ({}, {}) r {}",
+ js_text(*cx),
+ js_text(*cy),
+ js_text(*r)
+ )
+ }
+ Some(Self::Polygon { points }) => format!("polygone {} points", points.len()),
+ }
+ }
+}
+
+fn number(value: f64) -> Value {
+ serde_json::Number::from_f64(value).map_or(Value::Null, Value::Number)
+}
+
+/// `String(number)`, NaN and the infinities included.
+pub fn js_text(value: f64) -> String {
+ match value {
+ v if v.is_nan() => "NaN".to_owned(),
+ v if v == f64::INFINITY => "Infinity".to_owned(),
+ v if v == f64::NEG_INFINITY => "-Infinity".to_owned(),
+ v => js_number(v),
+ }
+}
+
+#[derive(Debug, Clone, PartialEq)]
+pub struct LegacyAnnotation {
+ pub id: String,
+ pub tag_name: String,
+ pub element_count: usize,
+ pub name_fr: Option,
+ pub name_en: Option,
+ pub category: Option,
+ pub category_name: Option,
+ pub lateral: Option,
+ pub special: Option,
+ pub opens_detail: Option,
+ pub geometry: Option,
+}
+
+#[derive(Debug, Clone, PartialEq)]
+pub struct ParsedSvg {
+ pub view_box: Option,
+ pub annotations: Vec,
+ pub anomalies: Vec,
+}
+
+/// `parseAnnotatedSvg`: every element in document order, the first of each id kept.
+pub fn parse_annotated_svg(text: &str, file_name: &str) -> Result {
+ let refuse = |why: String| PipelineError::Source(format!("{file_name}: {why}"));
+ prescan(text, &SVG_CAPS).map_err(|error| refuse(format!("{error:?}")))?;
+ let document = roxmltree::Document::parse(text).map_err(|error| refuse(error.to_string()))?;
+ let mut index_of: HashMap = HashMap::new();
+ let mut records: Vec = Vec::new();
+ for element in document.descendants().filter(roxmltree::Node::is_element) {
+ let Some(id) = element.attribute("data-anno-id") else {
+ continue;
+ };
+ if let Some(&index) = index_of.get(id) {
+ records[index].element_count += 1;
+ } else {
+ index_of.insert(id.to_owned(), records.len());
+ records.push(read_annotation(&element, id, text));
+ }
+ }
+ let anomalies = records
+ .iter()
+ .filter(|a| a.geometry.is_none())
+ .map(|a| geometry_anomaly(file_name, a))
+ .collect();
+ let view_box = document
+ .root_element()
+ .attribute("viewBox")
+ .map(str::to_owned);
+ Ok(ParsedSvg {
+ view_box,
+ annotations: records,
+ anomalies,
+ })
+}
+
+fn read_annotation(element: &roxmltree::Node, id: &str, text: &str) -> LegacyAnnotation {
+ let attr = |name: &str| element.attribute(name).map(str::to_owned);
+ let tag_name = qualified_name(element, text);
+ LegacyAnnotation {
+ id: id.to_owned(),
+ geometry: read_geometry(element, &tag_name),
+ tag_name,
+ element_count: 1,
+ name_fr: attr("data-anno-name-fr"),
+ name_en: attr("data-anno-name-en"),
+ category: attr("data-anno-category"),
+ category_name: attr("data-anno-category-name"),
+ lateral: attr("data-anno-lateral"),
+ special: attr("data-anno-special"),
+ opens_detail: attr("data-anno-opens-detail"),
+ }
+}
+
+/// `element.tagName`: the name as written, prefix included.
+fn qualified_name(element: &roxmltree::Node, text: &str) -> String {
+ let start = element.range().start + 1;
+ text[start..]
+ .split(|c: char| c.is_whitespace() || c == '/' || c == '>')
+ .next()
+ .unwrap_or_default()
+ .to_owned()
+}
+
+fn read_geometry(element: &roxmltree::Node, tag_name: &str) -> Option {
+ // `Number(null)` is 0: a missing attribute reads as 0, as in JavaScript.
+ let number_of = |name: &str| js_number_of(element.attribute(name).unwrap_or(""));
+ match tag_name {
+ "circle" => Some(LegacyGeometry::Circle {
+ cx: number_of("cx"),
+ cy: number_of("cy"),
+ r: number_of("r"),
+ }),
+ "polygon" => Some(LegacyGeometry::Polygon {
+ points: parse_points(element.attribute("points").unwrap_or("")),
+ }),
+ _ => None,
+ }
+}
+
+/// `raw.trim().split(/[\s,]+/).map(Number)`, read by pairs; an odd last number is dropped.
+fn parse_points(raw: &str) -> Vec<(f64, f64)> {
+ let numbers: Vec = js_split_list(raw).into_iter().map(js_number_of).collect();
+ let (pairs, _odd) = numbers.as_chunks::<2>();
+ pairs.iter().map(|&[x, y]| (x, y)).collect()
+}
+
+fn geometry_anomaly(file_name: &str, a: &LegacyAnnotation) -> Anomaly {
+ Anomaly::to_validate(
+ "svg.geometry.unsupported",
+ format!("{file_name} {}", a.id),
+ format!(
+ "Élément <{}> non pris en charge : géométrie non importée (null).",
+ a.tag_name
+ ),
+ )
+}
diff --git a/crates/core/src/curation/legacy/types.rs b/crates/core/src/curation/legacy/types.rs
new file mode 100644
index 00000000..a7dea447
--- /dev/null
+++ b/crates/core/src/curation/legacy/types.rs
@@ -0,0 +1,140 @@
+//! Shapes of `packages/anatomy/src/legacy/types.ts`. Field order is the TypeScript object
+//! order, so the serialised values compare with the frozen `oracles/r5/pipeline.json`.
+
+use serde::Serialize;
+use serde_json::{Map, Value};
+
+#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
+pub struct Anomaly {
+ pub code: String,
+ pub level: &'static str,
+ pub subject: String,
+ pub message: String,
+}
+
+impl Anomaly {
+ /// An anomaly the business must decide on (`a_valider`).
+ pub fn to_validate(code: &str, subject: impl Into, message: impl Into) -> Self {
+ Self::new(code, "a_valider", subject.into(), message.into())
+ }
+
+ /// An informative note (`informatif`).
+ pub fn informative(code: &str, subject: impl Into, message: impl Into) -> Self {
+ Self::new(code, "informatif", subject.into(), message.into())
+ }
+
+ fn new(code: &str, level: &'static str, subject: String, message: String) -> Self {
+ Self {
+ code: code.to_owned(),
+ level,
+ subject,
+ message,
+ }
+ }
+}
+
+/// One SVG source: its file name and bytes.
+#[derive(Debug, Clone)]
+pub struct SvgSource {
+ pub file_name: String,
+ pub bytes: Vec,
+}
+
+/// The four historical files, as bytes (the binary reads them).
+#[derive(Debug, Clone)]
+pub struct EquineLegacyInput {
+ pub main_svg: SvgSource,
+ pub head_svg: SvgSource,
+ pub catalog: Vec,
+ pub vertebrae: Vec,
+}
+
+#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
+pub struct AssetRecord {
+ pub file: String,
+ pub sha256: String,
+ pub byte_size: usize,
+ pub svg_asset_id: String,
+}
+
+#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
+pub struct SourceDigest {
+ pub file: String,
+ pub sha256: String,
+ pub byte_size: usize,
+}
+
+#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
+#[serde(rename_all = "camelCase")]
+pub struct RegionInference {
+ pub annotation_id: String,
+ pub label: String,
+ pub root: Option,
+ pub code: Option,
+}
+
+#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
+#[serde(rename_all = "camelCase")]
+pub struct AnnotationRow {
+ pub legacy_id: String,
+ pub new_id: String,
+ pub view_id: String,
+ pub kind: String,
+ pub laterality: String,
+ pub region_code: Option,
+ pub finding_zone_id: Option,
+ pub geometry: String,
+}
+
+#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
+#[serde(rename_all = "camelCase")]
+pub struct ZoneStats {
+ pub zone_id: String,
+ pub nodes: usize,
+ pub leaves: usize,
+ pub vet_referrals: usize,
+}
+
+#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
+pub struct AnnotationsByView {
+ pub main: usize,
+ pub head: usize,
+}
+
+#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
+#[serde(rename_all = "camelCase")]
+pub struct ImportStats {
+ pub legacy_annotations: usize,
+ pub annotations: usize,
+ pub annotations_by_view: AnnotationsByView,
+ pub linked_annotations: usize,
+ pub triggers: usize,
+ pub views: usize,
+ pub vertebral_annotations: usize,
+ pub bilateral: usize,
+ pub finding_zones: usize,
+ pub nodes: usize,
+ pub leaves: usize,
+ pub vet_referrals: usize,
+ pub missing_description_en: usize,
+ pub sources: usize,
+}
+
+/// `ImportResult`: the document in its input form (the JSON value the TypeScript built, key
+/// order kept) and everything the reports and the draft need.
+#[derive(Debug, Clone, Serialize)]
+#[serde(rename_all = "camelCase")]
+pub struct ImportResult {
+ pub document: Value,
+ pub legacy_id_map: Map,
+ pub assets: Vec,
+ pub source_digests: Vec,
+ pub sheet_id: String,
+ pub view_boxes: Map,
+ pub annotation_rows: Vec,
+ pub region_inferences: Vec,
+ pub zone_stats: Vec,
+ pub stats: ImportStats,
+ pub anomalies: Vec,
+ pub issues: Vec,
+}
diff --git a/crates/core/src/curation/legacy/vertebrae.rs b/crates/core/src/curation/legacy/vertebrae.rs
new file mode 100644
index 00000000..8fbaa111
--- /dev/null
+++ b/crates/core/src/curation/legacy/vertebrae.rs
@@ -0,0 +1,127 @@
+//! The legacy vertebral formula, read from the source text of `vertebrae.ts`
+//! (`packages/anatomy/src/legacy/vertebrae.ts`): the object after the first `= {`, its
+//! `name: digits` pairs, and the "varies A–B" range of the comment. The JavaScript regular
+//! expressions are reproduced by hand: `\s` is the JavaScript space set, `\w` and `\d` ASCII.
+
+use std::collections::HashMap;
+
+use serde_json::{Value, json};
+
+use crate::anatomy::svg::number::is_js_space;
+use crate::curation::PipelineError;
+
+pub const REGION_ORDER: [&str; 5] = ["C", "T", "L", "S", "Cd"];
+
+#[derive(Debug, Clone, PartialEq)]
+pub struct VertebralFormula {
+ pub counts: HashMap,
+ /// "varies A–B" range from the source comment, when stated.
+ pub varies_range: Option,
+}
+
+pub fn parse_vertebral_formula(source: &str) -> Result {
+ let body = formula_body(source)
+ .ok_or_else(|| PipelineError::Source("vertebrae.ts: formula object not found".into()))?;
+ let mut counts = HashMap::new();
+ for (key, digits) in pairs(body) {
+ counts.insert(key.to_owned(), digits.parse::().unwrap_or(f64::NAN));
+ }
+ for code in REGION_ORDER {
+ if !counts.contains_key(code) {
+ return Err(PipelineError::Source(format!(
+ "vertebrae.ts: region {code} missing"
+ )));
+ }
+ }
+ Ok(VertebralFormula {
+ counts,
+ varies_range: varies_range(source),
+ })
+}
+
+/// `/=\s*\{([^}]*)\}/`: the first `=` followed by spaces, a brace, and a closing brace.
+fn formula_body(source: &str) -> Option<&str> {
+ source.match_indices('=').find_map(|(at, _)| {
+ let rest = source[at + 1..].trim_start_matches(is_js_space);
+ let inner = rest.strip_prefix('{')?;
+ inner.find('}').map(|end| &inner[..end])
+ })
+}
+
+/// `/(\w+)\s*:\s*(\d+)/g`: the name and the digits of every match, left to right.
+fn pairs(body: &str) -> Vec<(&str, &str)> {
+ let bytes = body.as_bytes();
+ let mut found = Vec::new();
+ let mut at = 0;
+ while at < bytes.len() {
+ match pair_at(body, at) {
+ Some((key, digits, end)) => {
+ found.push((key, digits));
+ at = end;
+ }
+ None => at += body[at..].chars().next().map_or(1, char::len_utf8),
+ }
+ }
+ found
+}
+
+fn pair_at(body: &str, start: usize) -> Option<(&str, &str, usize)> {
+ let word_end = start + run(&body[start..], |c| c.is_ascii_alphanumeric() || c == '_');
+ if word_end == start {
+ return None;
+ }
+ let after = body[word_end..].trim_start_matches(is_js_space);
+ let after = after.strip_prefix(':')?.trim_start_matches(is_js_space);
+ let digits = run(after, |c| c.is_ascii_digit());
+ if digits == 0 {
+ return None;
+ }
+ let end = body.len() - after.len() + digits;
+ Some((&body[start..word_end], &after[..digits], end))
+}
+
+fn run(text: &str, accept: impl Fn(char) -> bool) -> usize {
+ text.char_indices()
+ .find(|&(_, c)| !accept(c))
+ .map_or(text.len(), |(at, _)| at)
+}
+
+/// `/varies\s+(\d+)\s*[–-]\s*(\d+)/`, formatted `A–B`.
+fn varies_range(source: &str) -> Option {
+ source.match_indices("varies").find_map(|(at, word)| {
+ let rest = &source[at + word.len()..];
+ let spaced = rest.trim_start_matches(is_js_space);
+ if spaced.len() == rest.len() {
+ return None;
+ }
+ let low = run(spaced, |c| c.is_ascii_digit());
+ let rest = spaced[low..].trim_start_matches(is_js_space);
+ let rest = rest
+ .strip_prefix(['–', '-'])?
+ .trim_start_matches(is_js_space);
+ let high = run(rest, |c| c.is_ascii_digit());
+ (low > 0 && high > 0).then(|| format!("{}–{}", &spaced[..low], &rest[..high]))
+ })
+}
+
+fn region_name(code: &str) -> (&'static str, &'static str) {
+ match code {
+ "C" => ("Cervicales", "Cervical"),
+ "T" => ("Thoraciques", "Thoracic"),
+ "L" => ("Lombaires", "Lumbar"),
+ "S" => ("Sacrales", "Sacral"),
+ _ => ("Caudales", "Caudal"),
+ }
+}
+
+/// The regions C → Cd, the order of the legacy picker tabs.
+pub fn vertebral_regions(formula: &VertebralFormula) -> Vec {
+ REGION_ORDER
+ .iter()
+ .map(|&code| {
+ let (fr, en) = region_name(code);
+ let count = formula.counts.get(code).copied().unwrap_or(f64::NAN);
+ json!({ "code": code, "name": { "fr": fr, "en": en }, "count": count })
+ })
+ .collect()
+}
diff --git a/crates/core/src/curation/markdown.rs b/crates/core/src/curation/markdown.rs
new file mode 100644
index 00000000..7cdad68a
--- /dev/null
+++ b/crates/core/src/curation/markdown.rs
@@ -0,0 +1,166 @@
+//! Markdown of the generated French reports: the building blocks of
+//! `packages/anatomy/src/markdown.ts` and the one change Prettier makes to those reports,
+//! the alignment of the tables (measured on the three reports: no other line changes).
+//! Pinned by `oracles/r5/markdown-tables.json` and the committed reports.
+
+use super::PipelineError;
+use super::text::display_width;
+use crate::json::js_number;
+
+/// A table cell value: text, a number (printed as `String(number)`) or nothing (`-`).
+#[derive(Debug, Clone, PartialEq)]
+pub enum Cell {
+ Text(String),
+ Number(f64),
+ Null,
+}
+
+impl From<&str> for Cell {
+ fn from(text: &str) -> Self {
+ Self::Text(text.to_owned())
+ }
+}
+
+impl From for Cell {
+ fn from(text: String) -> Self {
+ Self::Text(text)
+ }
+}
+
+impl From for Cell {
+ fn from(count: usize) -> Self {
+ Self::Number(count as f64)
+ }
+}
+
+impl From