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

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

No files matched your search

Generated
+1
View File
@@ -364,6 +364,7 @@ checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f"
name = "faunosteo-core"
version = "0.1.0"
dependencies = [
"icu_normalizer",
"lopdf",
"miniz_oxide 0.9.1",
"roxmltree",
+3
View File
@@ -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].
+19
View File
@@ -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"]
+152
View File
@@ -0,0 +1,152 @@
//! `import-equine` (R5.2): the legacy importer on the core. Reads the historical equine files
//! of the repository and produces the `legacy-import/` folder the TypeScript
//! `pnpm import:equine` generates (the draft folder joins at R5.3).
//!
//! import-equine --check compare with the committed folder, write nothing
//! import-equine --out <dir> write the folder into <dir> (absent or empty)
//!
//! The repository is found from this crate's manifest folder. Nothing is deleted: `--out`
//! refuses a folder that already holds files. Like `report-compare`, this file is the only
//! one of its tool allowed the file system.
#![allow(clippy::disallowed_methods)]
use std::path::{Path, PathBuf};
use std::{env, fs, process};
use faunosteo_core::curation::legacy::{
EquineLegacyInput, SvgSource, import_equine_legacy, render_artifacts,
};
const SEED: &str = concat!(
env!("CARGO_MANIFEST_DIR"),
"/../../supabase/seed/anatomical-sheets/equine"
);
/// A file of the folder: its name and its bytes.
type FolderFile = (String, Vec<u8>);
fn read(path: &Path) -> Result<Vec<u8>, String> {
fs::read(path).map_err(|error| format!("{}: {error}", path.display()))
}
fn svg(name: &str) -> Result<SvgSource, String> {
let bytes = read(&Path::new(SEED).join("legacy-sources").join(name))?;
Ok(SvgSource {
file_name: name.to_owned(),
bytes,
})
}
fn input() -> Result<EquineLegacyInput, String> {
let sources = Path::new(SEED).join("legacy-sources");
Ok(EquineLegacyInput {
main_svg: svg("Horse.svg")?,
head_svg: svg("Horse_head.svg")?,
catalog: read(&sources.join("pathologies.json"))?,
vertebrae: read(&sources.join("vertebrae.ts"))?,
})
}
/// Every file of `legacy-import/`: the four texts, then the two SVG copies.
fn folder() -> Result<Vec<FolderFile>, String> {
let input = input()?;
let result = import_equine_legacy(&input).map_err(|error| error.to_string())?;
let mut files: Vec<FolderFile> = render_artifacts(&result)
.map_err(|error| error.to_string())?
.into_iter()
.map(|(name, text)| (name, text.into_bytes()))
.collect();
for source in [input.main_svg, input.head_svg] {
files.push((format!("assets/{}", source.file_name), source.bytes));
}
Ok(files)
}
/// Relative paths of every file under `dir`.
fn listed(dir: &Path, prefix: &str, out: &mut Vec<String>) -> Result<(), String> {
let entries = fs::read_dir(dir).map_err(|error| format!("{}: {error}", dir.display()))?;
for entry in entries {
let entry = entry.map_err(|error| error.to_string())?;
let name = format!("{prefix}{}", entry.file_name().to_string_lossy());
if entry.path().is_dir() {
listed(&entry.path(), &format!("{name}/"), out)?;
} else {
out.push(name);
}
}
Ok(())
}
fn check(files: &[FolderFile]) -> Result<Vec<String>, String> {
let target = Path::new(SEED).join("legacy-import");
let mut problems = Vec::new();
for (name, bytes) in files {
match fs::read(target.join(name)) {
Ok(committed) if committed == *bytes => {}
Ok(_) => problems.push(format!("differs: {name}")),
Err(_) => problems.push(format!("missing: {name}")),
}
}
let mut present = Vec::new();
listed(&target, "", &mut present)?;
for name in present {
if !files.iter().any(|(produced, _)| *produced == name) {
problems.push(format!("not produced: {name}"));
}
}
Ok(problems)
}
fn write(files: &[FolderFile], out: &Path) -> Result<(), String> {
if out.exists()
&& fs::read_dir(out)
.map_err(|e| e.to_string())?
.next()
.is_some()
{
return Err(format!("{}: not empty, nothing written", out.display()));
}
for (name, bytes) in files {
let path = out.join(name);
if let Some(parent) = path.parent() {
fs::create_dir_all(parent).map_err(|error| error.to_string())?;
}
fs::write(&path, bytes).map_err(|error| format!("{}: {error}", path.display()))?;
}
Ok(())
}
fn run(args: &[String]) -> Result<i32, String> {
let files = folder()?;
match args {
[flag] if flag == "--check" => {
let problems = check(&files)?;
for problem in &problems {
eprintln!("{problem}");
}
println!(
"legacy-import: {} files, {} differences",
files.len(),
problems.len()
);
Ok(i32::from(!problems.is_empty()))
}
[flag, dir] if flag == "--out" => {
write(&files, &PathBuf::from(dir))?;
println!("legacy-import: {} files written to {dir}", files.len());
Ok(0)
}
_ => Err("usage: import-equine --check | --out <dir>".to_owned()),
}
}
fn main() {
let args: Vec<String> = env::args().skip(1).collect();
match run(&args) {
Ok(code) => process::exit(code),
Err(message) => {
eprintln!("{message}");
process::exit(2);
}
}
}
+40
View File
@@ -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}"))
}
@@ -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<String, String>,
}
pub struct MappedAnnotation {
pub annotation: Value,
pub row: AnnotationRow,
pub region: Option<RegionInference>,
pub anomalies: Vec<Anomaly>,
}
pub fn zone_name_key(fr: &str, en: &str) -> String {
format!("{fr}\n{en}")
}
fn laterality(a: &LegacyAnnotation, out: &mut Vec<Anomaly>) -> &'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<Anomaly>) -> Option<RegionInference> {
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<Anomaly>) -> 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<Anomaly>) -> 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<Anomaly>) -> (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<Anomaly>,
) -> (Value, Option<RegionInference>) {
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<String> {
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,
}
}
@@ -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<String> = [("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<Anomaly> {
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
}
+353
View File
@@ -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<String, Value>,
pub zones: Vec<(String, Map<String, Value>)>,
}
impl LegacyCatalog {
pub fn get(&self, key: &str) -> Option<&Value> {
self.root.get(key)
}
}
pub fn parse_legacy_catalog(text: &str) -> Result<LegacyCatalog, PipelineError> {
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<String, Value>, 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<String, Value>) -> Result<Vec<&Map<String, Value>>, 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<String, Value>) -> Result<Vec<&Map<String, Value>>, 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<String, Value>, 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<String, Value>]) -> Result<TreeCounts, PipelineError> {
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<Value>,
pub zone_stats: Vec<ZoneStats>,
pub node_id_map: Map<String, Value>,
pub missing_description_en: usize,
pub sources: usize,
pub anomalies: Vec<Anomaly>,
}
/// Tallies of one catalog walk.
#[derive(Default)]
struct WalkState {
node_ids: Vec<(String, Value)>,
anomalies: Vec<Anomaly>,
missing_description_en: usize,
sources: usize,
}
/// `{ fr?, en? }` from `description_fr` / `description_en`, when either is present.
fn description(node: &Map<String, Value>) -> Option<Value> {
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<String, Value>) -> 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<String, Value>, 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<String, Value>,
path: &[&str],
state: &mut WalkState,
) -> Result<Value, PipelineError> {
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<Vec<Value>, 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<String, Value>,
state: &mut WalkState,
) -> Result<Value, PipelineError> {
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<Vec<Value>, 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<String, Value>) -> Result<ZoneStats, PipelineError> {
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<String> = 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<String, Value>,
state: &mut WalkState,
) -> Result<Value, PipelineError> {
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<String, Value>> = 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<MappedCatalog, PipelineError> {
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,
})
}
@@ -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<Vec<Anomaly>, 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<String, Value>) -> Result<String, PipelineError> {
let ids: Result<Vec<&str>, 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<Vec<Anomaly>, PipelineError> {
let mut variants: Vec<(String, Vec<String>)> = 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<String, Value>],
into: &mut Vec<String>,
seen: &mut HashSet<String>,
) -> 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<Vec<Anomaly>, 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<String> = 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<Vec<Anomaly>, PipelineError> {
let mut out = foreign_prefix_anomalies(catalog)?;
out.extend(anamnesis_variants(catalog)?);
out.extend(cross_zone_duplicates(catalog)?);
Ok(out)
}
@@ -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<Value>,
pub id_map: Map<String, Value>,
pub anomalies: Vec<Anomaly>,
}
/// `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<String>)> {
let mut seen: Vec<(String, Option<String>)> = 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,
}
}
+370
View File
@@ -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<AssetRecord>,
main: ParsedSvg,
head: ParsedSvg,
catalog: LegacyCatalog,
formula: VertebralFormula,
}
struct Mapped {
annotations: Vec<MappedAnnotation>,
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<ParsedSources, PipelineError> {
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<String, String> {
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<Mapped, PipelineError> {
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<String, Value> {
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<Anomaly> {
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<Vec<Anomaly>, 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<AnatomicalSheetDocument, PipelineError> {
serde_json::from_value(document.clone())
.map_err(|error| PipelineError::Guard(format!("document schema: {error}")))
}
pub fn import_equine_legacy(input: &EquineLegacyInput) -> Result<ImportResult, PipelineError> {
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<String, Value> {
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
}
+42
View File
@@ -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<Vec<(String, String)>, 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)?)?,
),
])
}
@@ -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<RegionMatch> {
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<RegionMatch> {
[name_fr, name_en]
.into_iter()
.flatten()
.filter(|label| !label.is_empty())
.find_map(match_label)
}
+518
View File
@@ -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<Vec<Cell>> = 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<String, Value>> {
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<String, PipelineError> {
let s = &result.stats;
let out = count_tree(&categories_of(&result.document))?;
let rows: Vec<Vec<Cell>> = [
("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<Vec<Cell>> = [
(
"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<Vec<Cell>> = 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<Vec<Cell>> = 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<Vec<Cell>> {
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<Vec<Cell>> = 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é",
],
&region_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<Vec<Cell>> = 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<Vec<Cell>> {
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<String> = 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<Vec<Cell>> = 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<String, PipelineError> {
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")))
}
+192
View File
@@ -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<Value> = 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<String>,
pub name_en: Option<String>,
pub category: Option<String>,
pub category_name: Option<String>,
pub lateral: Option<String>,
pub special: Option<String>,
pub opens_detail: Option<String>,
pub geometry: Option<LegacyGeometry>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct ParsedSvg {
pub view_box: Option<String>,
pub annotations: Vec<LegacyAnnotation>,
pub anomalies: Vec<Anomaly>,
}
/// `parseAnnotatedSvg`: every element in document order, the first of each id kept.
pub fn parse_annotated_svg(text: &str, file_name: &str) -> Result<ParsedSvg, PipelineError> {
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<String, usize> = HashMap::new();
let mut records: Vec<LegacyAnnotation> = 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<LegacyGeometry> {
// `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<f64> = 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
),
)
}
+140
View File
@@ -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<String>, message: impl Into<String>) -> Self {
Self::new(code, "a_valider", subject.into(), message.into())
}
/// An informative note (`informatif`).
pub fn informative(code: &str, subject: impl Into<String>, message: impl Into<String>) -> 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<u8>,
}
/// 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<u8>,
pub vertebrae: Vec<u8>,
}
#[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<String>,
pub code: Option<String>,
}
#[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<String>,
pub finding_zone_id: Option<String>,
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<String, Value>,
pub assets: Vec<AssetRecord>,
pub source_digests: Vec<SourceDigest>,
pub sheet_id: String,
pub view_boxes: Map<String, Value>,
pub annotation_rows: Vec<AnnotationRow>,
pub region_inferences: Vec<RegionInference>,
pub zone_stats: Vec<ZoneStats>,
pub stats: ImportStats,
pub anomalies: Vec<Anomaly>,
pub issues: Vec<crate::domain::sheet::SheetIssue>,
}
@@ -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<String, f64>,
/// "varies A–B" range from the source comment, when stated.
pub varies_range: Option<String>,
}
pub fn parse_vertebral_formula(source: &str) -> Result<VertebralFormula, PipelineError> {
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::<f64>().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<String> {
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<Value> {
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()
}
+166
View File
@@ -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<String> for Cell {
fn from(text: String) -> Self {
Self::Text(text)
}
}
impl From<usize> for Cell {
fn from(count: usize) -> Self {
Self::Number(count as f64)
}
}
impl From<Option<String>> for Cell {
fn from(value: Option<String>) -> Self {
value.map_or(Self::Null, Self::Text)
}
}
/// `cell()`: `-` for null; pipes escaped, newlines turned into spaces.
pub fn cell(value: &Cell) -> String {
match value {
Cell::Null => "-".to_owned(),
Cell::Number(number) => js_number(*number),
Cell::Text(text) => text.replace('|', "\\|").replace('\n', " "),
}
}
/// `table()`: the raw table the TypeScript wrote before Prettier aligned it.
pub fn table(head: &[&str], rows: &[Vec<Cell>]) -> String {
let mut lines = vec![head.join(" | "), vec!["---"; head.len()].join(" | ")];
for row in rows {
let cells: Vec<String> = row.iter().map(cell).collect();
lines.push(cells.join(" | "));
}
lines
.iter()
.map(|line| format!("| {line} |"))
.collect::<Vec<_>>()
.join("\n")
}
/// `code()`: an inline code span, `-` for nothing.
pub fn code(value: Option<&str>) -> String {
value.map_or_else(|| "-".to_owned(), |text| format!("`{text}`"))
}
/// What `formatMarkdown` does to a report written by `table()`: every table aligned (cells
/// padded to the widest of their column, three at least, separator dashes to the same
/// width), every other line kept.
pub fn format_markdown(raw: &str) -> Result<String, PipelineError> {
let lines: Vec<&str> = raw.split('\n').collect();
let mut out: Vec<String> = Vec::with_capacity(lines.len());
let mut index = 0;
while index < lines.len() {
if is_table_start(&lines, index) {
let end = table_end(&lines, index);
out.extend(align(&lines[index..end])?);
index = end;
} else {
out.push(lines[index].to_owned());
index += 1;
}
}
Ok(out.join("\n"))
}
fn is_table_start(lines: &[&str], index: usize) -> bool {
lines[index].starts_with('|')
&& lines
.get(index + 1)
.is_some_and(|next| split_row(next).iter().all(|c| is_separator(c)))
}
fn is_separator(cell: &str) -> bool {
cell.len() >= 3 && cell.bytes().all(|b| b == b'-')
}
fn table_end(lines: &[&str], start: usize) -> usize {
let mut end = start;
while end < lines.len() && lines[end].starts_with('|') {
end += 1;
}
end
}
/// The cells of a row: split on the pipes no backslash escapes, trimmed as GFM trims them.
fn split_row(line: &str) -> Vec<String> {
let inner = line.trim_end_matches([' ', '\t']);
let inner = inner.strip_prefix('|').unwrap_or(inner);
let inner = inner.strip_suffix('|').unwrap_or(inner);
let mut cells = Vec::new();
let mut current = String::new();
let mut escaped = false;
for c in inner.chars() {
if c == '|' && !escaped {
cells.push(std::mem::take(&mut current));
} else {
current.push(c);
}
escaped = c == '\\' && !escaped;
}
cells.push(current);
cells
.into_iter()
.map(|cell| cell.trim_matches([' ', '\t']).to_owned())
.collect()
}
fn align(lines: &[&str]) -> Result<Vec<String>, PipelineError> {
let rows: Vec<Vec<String>> = lines.iter().map(|line| split_row(line)).collect();
let columns = rows.first().map_or(0, Vec::len);
let mut widths = vec![3; columns];
let content = rows.iter().enumerate().filter(|&(index, _)| index != 1);
for (_, row) in content {
for (column, text) in row.iter().enumerate().take(columns) {
widths[column] = widths[column].max(display_width(text)?);
}
}
let mut out = Vec::with_capacity(rows.len());
for (index, row) in rows.iter().enumerate() {
out.push(if index == 1 {
separator(&widths)
} else {
padded(row, &widths)?
});
}
Ok(out)
}
fn separator(widths: &[usize]) -> String {
let cells: Vec<String> = widths.iter().map(|&width| "-".repeat(width)).collect();
format!("| {} |", cells.join(" | "))
}
fn padded(row: &[String], widths: &[usize]) -> Result<String, PipelineError> {
let mut cells = Vec::with_capacity(widths.len());
for (column, &width) in widths.iter().enumerate() {
let text = row.get(column).map_or("", String::as_str);
let pad = width.saturating_sub(display_width(text)?);
cells.push(format!("{text}{}", " ".repeat(pad)));
}
Ok(format!("| {} |", cells.join(" | ")))
}
+40
View File
@@ -0,0 +1,40 @@
//! 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.
//!
//! 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 ids;
pub mod legacy;
pub mod markdown;
pub mod prettier_json;
pub mod text;
/// Why the pipeline refuses to produce a file. The TypeScript accepted some of these inputs
/// and wrote whatever JavaScript made of them; the port refuses instead (declared in
/// `docs/development/RUST.md` §11).
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum PipelineError {
/// A source cannot be read as the pipeline expects (message names the source).
Source(String),
/// A guard of the importer failed (nothing dropped, nothing invented).
Guard(String),
/// A text holds a character whose Prettier display width is not reproduced (emoji
/// sequences): the layout would differ, so nothing is written.
UnsupportedWidth(String),
}
impl std::fmt::Display for PipelineError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Source(message) | Self::Guard(message) => f.write_str(message),
Self::UnsupportedWidth(text) => {
write!(f, "display width not reproduced for {text:?} (emoji)")
}
}
}
}
+183
View File
@@ -0,0 +1,183 @@
//! `formatJson` of `packages/anatomy/src/format.ts`: Prettier 3 (parser `json`, print width
//! 80, two-space indent) over `JSON.stringify(value, null, 2)`, reproduced for the values the
//! pipeline writes. Pinned by `oracles/r5/prettier-json.json` and by every generated JSON
//! file of the seed.
//!
//! The rules, as Prettier applies them to that input:
//! - a non-empty object is always expanded (JSON.stringify put a newline after its brace),
//! and an expanded object breaks every array that holds it;
//! - an array breaks when it holds more than one element and every element is an array of
//! more than one element, when an element breaks, or when it does not fit flat on the line
//! (the trailing comma of its parent counted);
//! - a broken array of more than one number fills its lines; any other broken array puts one
//! element per line;
//! - strings print as JSON.stringify writes them, numbers as `String(number)`.
use serde_json::{Map, Value};
use super::PipelineError;
use super::text::display_width;
use crate::json::js_number;
const PRINT_WIDTH: usize = 80;
/// The file text: the formatted value and a final newline.
pub fn format_json(value: &Value) -> Result<String, PipelineError> {
let printer = Printer {
level: 0,
column: 0,
trailing: 0,
};
Ok(format!("{}\n", printer.value(value)?))
}
/// Where a value starts: its indentation level, its column, and how many characters follow
/// it on the same line (a comma, or nothing).
#[derive(Clone, Copy)]
struct Printer {
level: usize,
column: usize,
trailing: usize,
}
impl Printer {
fn value(self, value: &Value) -> Result<String, PipelineError> {
match value {
Value::Object(map) if !map.is_empty() => self.object(map),
Value::Array(items) if !items.is_empty() => self.array(items),
_ => scalar(value),
}
}
fn object(self, map: &Map<String, Value>) -> Result<String, PipelineError> {
let inner = indent(self.level + 1);
let mut lines = Vec::with_capacity(map.len());
for (index, (key, value)) in map.iter().enumerate() {
let key = string(key);
let child = Printer {
level: self.level + 1,
column: inner.len() + display_width(&key)? + 2,
trailing: usize::from(index + 1 < map.len()),
};
lines.push(format!("{inner}{key}: {}", child.value(value)?));
}
Ok(format!(
"{{\n{}\n{}}}",
lines.join(",\n"),
indent(self.level)
))
}
fn array(self, items: &[Value]) -> Result<String, PipelineError> {
if !must_break(items) {
let flat = flat(&Value::Array(items.to_vec()))?;
if self.column + display_width(&flat)? + self.trailing <= PRINT_WIDTH {
return Ok(flat);
}
}
let body = if is_concise(items) {
self.fill(items)?
} else {
self.one_per_line(items)?
};
Ok(format!("[\n{body}\n{}]", indent(self.level)))
}
/// Prettier's `fill`: an element joins the line when it and its separator still fit.
fn fill(self, items: &[Value]) -> Result<String, PipelineError> {
let inner = indent(self.level + 1);
let mut out = inner.clone();
let mut column = inner.len();
for (index, item) in items.iter().enumerate() {
let comma = if index + 1 < items.len() { "," } else { "" };
let text = format!("{}{comma}", scalar(item)?);
let width = display_width(&text)?;
if index > 0 && column + 1 + width <= PRINT_WIDTH {
out.push(' ');
column += 1;
} else if index > 0 {
out.push('\n');
out.push_str(&inner);
column = inner.len();
}
out.push_str(&text);
column += width;
}
Ok(out)
}
fn one_per_line(self, items: &[Value]) -> Result<String, PipelineError> {
let inner = indent(self.level + 1);
let mut lines = Vec::with_capacity(items.len());
for (index, item) in items.iter().enumerate() {
let child = Printer {
level: self.level + 1,
column: inner.len(),
trailing: usize::from(index + 1 < items.len()),
};
lines.push(format!("{inner}{}", child.value(item)?));
}
Ok(lines.join(",\n"))
}
}
/// A group that breaks whatever the width: an expanded object inside, or Prettier's rule for
/// an array of several arrays of several elements; a breaking element breaks its parent.
fn must_break(items: &[Value]) -> bool {
let all_long_arrays = items.len() > 1
&& items
.iter()
.all(|item| matches!(item, Value::Array(inner) if inner.len() > 1));
all_long_arrays || items.iter().any(breaks)
}
fn breaks(value: &Value) -> bool {
match value {
Value::Object(map) => !map.is_empty(),
Value::Array(items) => must_break(items),
_ => false,
}
}
/// Prettier's `isConciselyPrintedArray`: more than one element, all numbers.
fn is_concise(items: &[Value]) -> bool {
items.len() > 1 && items.iter().all(Value::is_number)
}
/// The flat form of a value that does not break (arrays of scalars and arrays, `{}`).
fn flat(value: &Value) -> Result<String, PipelineError> {
match value {
Value::Array(items) if !items.is_empty() => {
let parts: Result<Vec<String>, PipelineError> = items.iter().map(flat).collect();
Ok(format!("[{}]", parts?.join(", ")))
}
_ => scalar(value),
}
}
fn scalar(value: &Value) -> Result<String, PipelineError> {
Ok(match value {
Value::Null => "null".to_owned(),
Value::Bool(flag) => flag.to_string(),
Value::Number(number) => prettier_number(number.as_f64().unwrap_or(f64::NAN)),
Value::String(text) => string(text),
Value::Array(_) => "[]".to_owned(),
Value::Object(_) => "{}".to_owned(),
})
}
/// JSON.parse reads every number as a double, JSON.stringify prints it as `String(number)`,
/// then Prettier's `printNumber` drops the `+` of an exponent (`1e+21` → `1e21`); its other
/// rewrites (case, zeros, dots) never apply to a JavaScript-printed number.
fn prettier_number(value: f64) -> String {
js_number(value).replacen("e+", "e", 1)
}
/// `JSON.stringify` of a string (serde_json escapes the same set).
fn string(text: &str) -> String {
serde_json::to_string(text).unwrap_or_default()
}
fn indent(level: usize) -> String {
" ".repeat(level)
}
+315
View File
@@ -0,0 +1,315 @@
//! Text rules of the TypeScript pipeline: Prettier's display width, the NFD folding of
//! `slugify` and of the region roots, UTF-16 ordering, `Object.entries` key order.
use std::cmp::Ordering;
use icu_normalizer::DecomposingNormalizerBorrowed;
use serde_json::{Map, Value};
use super::PipelineError;
/// Prettier 3's `getStringWidth` (the markdown and JSON printers): ASCII counts its length;
/// otherwise controls, U+0300 to U+036F and U+FE00 to U+FE0F count 0, East Asian wide and
/// fullwidth characters 2, everything else 1. Prettier first replaces RGI emoji sequences by
/// one or two spaces; that table is not reproduced, so a text holding an emoji candidate is
/// refused rather than measured differently.
pub fn display_width(text: &str) -> Result<usize, PipelineError> {
// Prettier's fast path: every character in U+0020 to U+007F.
if text.bytes().all(|byte| (0x20..=0x7F).contains(&byte)) {
return Ok(text.len());
}
let mut width = 0;
for c in text.chars() {
if is_emoji_candidate(c) {
return Err(PipelineError::UnsupportedWidth(text.to_owned()));
}
width += char_width(u32::from(c));
}
Ok(width)
}
fn char_width(code: u32) -> usize {
if code <= 0x1F || (0x7F..=0x9F).contains(&code) || (0x300..=0x36F).contains(&code) {
return 0;
}
if (0xFE00..=0xFE0F).contains(&code) {
return 0;
}
if is_fullwidth(code) || is_wide(code) {
2
} else {
1
}
}
/// Prettier's `isFullWidth`.
fn is_fullwidth(code: u32) -> bool {
code == 0x3000 || (0xFF01..=0xFF60).contains(&code) || (0xFFE0..=0xFFE6).contains(&code)
}
/// Prettier 3.8.3's `isWide` (get-east-asian-width), transcribed row for row from
/// `prettier/index.mjs` (the markdown plugin carries the same table).
const WIDE: &[(u32, u32)] = &[
(0x1100, 0x115F),
(0x231A, 0x231A),
(0x231B, 0x231B),
(0x2329, 0x2329),
(0x232A, 0x232A),
(0x23E9, 0x23EC),
(0x23F0, 0x23F0),
(0x23F3, 0x23F3),
(0x25FD, 0x25FD),
(0x25FE, 0x25FE),
(0x2614, 0x2614),
(0x2615, 0x2615),
(0x2630, 0x2637),
(0x2648, 0x2653),
(0x267F, 0x267F),
(0x268A, 0x268F),
(0x2693, 0x2693),
(0x26A1, 0x26A1),
(0x26AA, 0x26AA),
(0x26AB, 0x26AB),
(0x26BD, 0x26BD),
(0x26BE, 0x26BE),
(0x26C4, 0x26C4),
(0x26C5, 0x26C5),
(0x26CE, 0x26CE),
(0x26D4, 0x26D4),
(0x26EA, 0x26EA),
(0x26F2, 0x26F2),
(0x26F3, 0x26F3),
(0x26F5, 0x26F5),
(0x26FA, 0x26FA),
(0x26FD, 0x26FD),
(0x2705, 0x2705),
(0x270A, 0x270A),
(0x270B, 0x270B),
(0x2728, 0x2728),
(0x274C, 0x274C),
(0x274E, 0x274E),
(0x2753, 0x2755),
(0x2757, 0x2757),
(0x2795, 0x2797),
(0x27B0, 0x27B0),
(0x27BF, 0x27BF),
(0x2B1B, 0x2B1B),
(0x2B1C, 0x2B1C),
(0x2B50, 0x2B50),
(0x2B55, 0x2B55),
(0x2E80, 0x2E99),
(0x2E9B, 0x2EF3),
(0x2F00, 0x2FD5),
(0x2FF0, 0x2FFF),
(0x3001, 0x303E),
(0x3041, 0x3096),
(0x3099, 0x30FF),
(0x3105, 0x312F),
(0x3131, 0x318E),
(0x3190, 0x31E5),
(0x31EF, 0x321E),
(0x3220, 0x3247),
(0x3250, 0xA48C),
(0xA490, 0xA4C6),
(0xA960, 0xA97C),
(0xAC00, 0xD7A3),
(0xF900, 0xFAFF),
(0xFE10, 0xFE19),
(0xFE30, 0xFE52),
(0xFE54, 0xFE66),
(0xFE68, 0xFE6B),
(0x16FE0, 0x16FE4),
(0x16FF0, 0x16FF6),
(0x17000, 0x18CD5),
(0x18CFF, 0x18D1E),
(0x18D80, 0x18DF2),
(0x1AFF0, 0x1AFF3),
(0x1AFF5, 0x1AFFB),
(0x1AFFD, 0x1AFFD),
(0x1AFFE, 0x1AFFE),
(0x1B000, 0x1B122),
(0x1B132, 0x1B132),
(0x1B150, 0x1B152),
(0x1B155, 0x1B155),
(0x1B164, 0x1B167),
(0x1B170, 0x1B2FB),
(0x1D300, 0x1D356),
(0x1D360, 0x1D376),
(0x1F004, 0x1F004),
(0x1F0CF, 0x1F0CF),
(0x1F18E, 0x1F18E),
(0x1F191, 0x1F19A),
(0x1F200, 0x1F202),
(0x1F210, 0x1F23B),
(0x1F240, 0x1F248),
(0x1F250, 0x1F250),
(0x1F251, 0x1F251),
(0x1F260, 0x1F265),
(0x1F300, 0x1F320),
(0x1F32D, 0x1F335),
(0x1F337, 0x1F37C),
(0x1F37E, 0x1F393),
(0x1F3A0, 0x1F3CA),
(0x1F3CF, 0x1F3D3),
(0x1F3E0, 0x1F3F0),
(0x1F3F4, 0x1F3F4),
(0x1F3F8, 0x1F43E),
(0x1F440, 0x1F440),
(0x1F442, 0x1F4FC),
(0x1F4FF, 0x1F53D),
(0x1F54B, 0x1F54E),
(0x1F550, 0x1F567),
(0x1F57A, 0x1F57A),
(0x1F595, 0x1F595),
(0x1F596, 0x1F596),
(0x1F5A4, 0x1F5A4),
(0x1F5FB, 0x1F64F),
(0x1F680, 0x1F6C5),
(0x1F6CC, 0x1F6CC),
(0x1F6D0, 0x1F6D2),
(0x1F6D5, 0x1F6D8),
(0x1F6DC, 0x1F6DF),
(0x1F6EB, 0x1F6EB),
(0x1F6EC, 0x1F6EC),
(0x1F6F4, 0x1F6FC),
(0x1F7E0, 0x1F7EB),
(0x1F7F0, 0x1F7F0),
(0x1F90C, 0x1F93A),
(0x1F93C, 0x1F945),
(0x1F947, 0x1F9FF),
(0x1FA70, 0x1FA7C),
(0x1FA80, 0x1FA8A),
(0x1FA8E, 0x1FAC6),
(0x1FAC8, 0x1FAC8),
(0x1FACD, 0x1FADC),
(0x1FADF, 0x1FAEA),
(0x1FAEF, 0x1FAF8),
(0x20000, 0x2FFFD),
(0x30000, 0x3FFFD),
];
fn is_wide(code: u32) -> bool {
WIDE.iter().any(|&(low, high)| (low..=high).contains(&code))
}
/// Characters that start, join or modify an emoji sequence in Prettier's emoji pattern, and
/// the pictographs Prettier's wide table lists.
fn is_emoji_candidate(c: char) -> bool {
let code = u32::from(c);
matches!(
code,
0xA9 | 0xAE
| 0x200D
| 0x203C
| 0x2049
| 0x20E3
| 0x2122
| 0x2139
| 0x2194..=0x21AA
| 0x231A..=0x23FF
| 0x24C2
| 0x25AA..=0x25FE
| 0x2600..=0x27BF
| 0x2934
| 0x2935
| 0x2B05..=0x2B55
| 0x3030
| 0x303D
| 0x3297
| 0x3299
| 0x1F000..=0x1FAFF
| 0xE0020..=0xE007F
)
}
/// `label.normalize('NFD').replace(/[̀-ͯ]/g, '').toLowerCase()`.
pub fn fold(label: &str) -> String {
let decomposed = DecomposingNormalizerBorrowed::new_nfd().normalize(label);
let stripped: String = decomposed
.chars()
.filter(|c| !('\u{300}'..='\u{36F}').contains(c))
.collect();
stripped.to_lowercase()
}
/// `slugify` of `packages/anatomy/src/legacy/ids.ts`: folded, every run outside `[a-z0-9]`
/// replaced by `-`, dashes trimmed at both ends, then cut to 64 characters (the cut can leave
/// a trailing dash, as in JavaScript).
pub fn slugify(label: &str) -> String {
let mut slug = String::new();
let mut in_run = false;
for c in fold(label).chars() {
if c.is_ascii_lowercase() || c.is_ascii_digit() {
slug.push(c);
in_run = false;
} else if !in_run {
slug.push('-');
in_run = true;
}
}
let trimmed = slug.trim_matches('-');
trimmed.chars().take(64).collect()
}
/// JavaScript's default string comparison: UTF-16 code units.
pub fn utf16_cmp(a: &str, b: &str) -> Ordering {
a.encode_utf16().cmp(b.encode_utf16())
}
/// An array index key (`"0"` to `"4294967294"`, canonical form): JavaScript lists these
/// first, in ascending order, whatever the insertion order.
fn array_index(key: &str) -> Option<u32> {
let index: u32 = key.parse().ok()?;
(index != u32::MAX && index.to_string() == key).then_some(index)
}
/// The keys of a parsed object in `Object.keys` order.
pub fn js_keys(map: &Map<String, Value>) -> Vec<&String> {
let mut indexed: Vec<(u32, &String)> = map
.keys()
.filter_map(|key| array_index(key).map(|index| (index, key)))
.collect();
indexed.sort_by_key(|&(index, _)| index);
let named = map.keys().filter(|key| array_index(key).is_none());
indexed
.into_iter()
.map(|(_, key)| key)
.chain(named)
.collect()
}
/// An object built from entries the way JavaScript would hold it: a repeated key keeps its
/// first position and its last value, array index keys come first.
pub fn js_object<I: IntoIterator<Item = (String, Value)>>(entries: I) -> Map<String, Value> {
let mut map = Map::new();
for (key, value) in entries {
map.insert(key, value);
}
let order: Vec<String> = js_keys(&map).into_iter().cloned().collect();
let mut sorted = Map::new();
for key in order {
if let Some(value) = map.remove(&key) {
sorted.insert(key, value);
}
}
sorted
}
/// `Math.round`: the nearest integer, a half rounding toward +∞; −0 for −0 and for values in
/// [−0.5, 0).
pub fn js_round(value: f64) -> f64 {
if !value.is_finite() || value == value.trunc() {
return value;
}
let floor = value.floor();
let rounded = if value - floor >= 0.5 {
floor + 1.0
} else {
floor
};
if rounded == 0.0 && value < 0.0 {
-0.0
} else {
rounded
}
}
+2
View File
@@ -6,6 +6,8 @@
//! first contract types; the business rules arrive with R2.
pub mod anatomy;
#[cfg(feature = "curation")]
pub mod curation;
pub mod domain;
pub mod facade;
pub mod json;
+6
View File
@@ -12,6 +12,12 @@ pub mod svg_tree;
use faunosteo_core::domain::js::{find_lone_surrogate, parse_json};
use serde_json::Value;
/// The absolute path of a repository file.
#[allow(dead_code)]
pub fn repo_path(relative: &str) -> String {
format!("{}/../../{relative}", env!("CARGO_MANIFEST_DIR"))
}
/// A repository file, by path from the repository root.
pub fn repo_file(relative: &str) -> String {
let path = format!("{}/../../{relative}", env!("CARGO_MANIFEST_DIR"));
+175
View File
@@ -0,0 +1,175 @@
//! The pipeline's formatting and JavaScript semantics (R5.2) against the TypeScript tools
//! themselves: Prettier's JSON layout and table alignment, `slugify`, `deterministicUuid`,
//! `Number()`, `Math.round`, `Object.keys` order, `trim`. The expectations were written by
//! Node (`packages/anatomy/tests/r5.format-oracles.test.ts`, `r5.pipeline-oracles.test.ts`);
//! nothing here computes its own expected text.
#![allow(clippy::disallowed_methods, clippy::unwrap_used, clippy::expect_used)]
mod common;
use faunosteo_core::anatomy::svg::number::{is_js_space, js_number_of, js_split_list};
use faunosteo_core::curation::PipelineError;
use faunosteo_core::curation::ids::deterministic_uuid;
use faunosteo_core::curation::markdown::format_markdown;
use faunosteo_core::curation::prettier_json::format_json;
use faunosteo_core::curation::text::{js_keys, js_round, slugify};
use faunosteo_core::domain::js::parse_json;
use faunosteo_core::json::js_number;
use serde_json::Value;
fn oracle(name: &str) -> Value {
serde_json::from_str(&common::repo_file(&format!(
"crates/core/tests/oracles/r5/{name}"
)))
.unwrap()
}
fn rows<'a>(value: &'a Value, key: &str) -> &'a Vec<Value> {
value[key].as_array().unwrap()
}
/// Every case prints as Prettier printed it, except the declared refusal: a text holding an
/// emoji, whose Prettier width table is not reproduced (`PipelineError::UnsupportedWidth`).
#[test]
fn json_layout_matches_prettier_on_every_probe() {
let oracle = oracle("prettier-json.json");
let mut refused = Vec::new();
for (index, case) in rows(&oracle, "cases").iter().enumerate() {
let input = parse_json(case["input"].as_str().unwrap()).unwrap();
match format_json(&input) {
Ok(text) => assert_eq!(text, case["output"].as_str().unwrap(), "case {index}"),
Err(PipelineError::UnsupportedWidth(_)) => refused.push(index),
Err(other) => panic!("case {index}: {other}"),
}
}
assert_eq!(refused, vec![19], "only the emoji case is refused");
}
#[test]
fn every_generated_json_file_of_the_seed_prints_byte_for_byte() {
let seed = "supabase/seed/anatomical-sheets/equine";
for file in [
"legacy-import/document.json",
"legacy-import/legacy-id-map.json",
"legacy-import/assets.json",
"draft/document.json",
"draft/assets.json",
"draft/sheet-ref.json",
"draft/report-migration-map.json",
] {
let text = common::repo_file(&format!("{seed}/{file}"));
let value = parse_json(&text).unwrap();
assert_eq!(format_json(&value).unwrap(), text, "{file}");
}
}
#[test]
fn tables_align_as_prettier_aligns_them() {
let oracle = oracle("markdown-tables.json");
let mut refused = Vec::new();
for (index, case) in rows(&oracle, "cases").iter().enumerate() {
match format_markdown(case["raw"].as_str().unwrap()) {
Ok(text) => assert_eq!(text, case["output"].as_str().unwrap(), "case {index}"),
Err(PipelineError::UnsupportedWidth(_)) => refused.push(index),
Err(other) => panic!("case {index}: {other}"),
}
}
assert_eq!(refused, vec![5], "only the emoji table is refused");
}
#[test]
fn slugs_and_uuids_match_the_typescript() {
let semantics = oracle("js-semantics.json");
let ids = oracle("ids.json");
for row in rows(&semantics, "slugify").iter().chain(rows(&ids, "slug")) {
let label = row["label"].as_str().unwrap();
assert_eq!(slugify(label), row["slug"].as_str().unwrap(), "{label:?}");
}
let uuid_rows = rows(&semantics, "deterministicUuid")
.iter()
.chain(rows(&ids, "uuid"));
for row in uuid_rows {
let name = row["name"].as_str().unwrap();
assert_eq!(
deterministic_uuid(name),
row["uuid"].as_str().unwrap(),
"{name:?}"
);
}
}
#[test]
fn number_points_round_and_trim_follow_javascript() {
let semantics = oracle("js-semantics.json");
for row in rows(&semantics, "number") {
let value = js_number_of(row["text"].as_str().unwrap());
assert_eq!(js_string(value), row["string"].as_str().unwrap(), "{row}");
let negative_zero = value == 0.0 && value.is_sign_negative();
assert_eq!(
negative_zero,
row["negativeZero"].as_bool().unwrap(),
"{row}"
);
}
for row in rows(&semantics, "points") {
let numbers: Vec<String> = js_split_list(row["text"].as_str().unwrap())
.into_iter()
.map(|part| js_string(js_number_of(part)))
.collect();
let expected: Vec<&str> = rows(row, "numbers")
.iter()
.map(|n| n.as_str().unwrap())
.collect();
assert_eq!(numbers, expected, "{row}");
}
for row in rows(&semantics, "mathRound") {
let bits = u64::from_str_radix(row["bits"].as_str().unwrap(), 16).unwrap();
let value = f64::from_bits(bits);
let rounded = js_round(value);
assert_eq!(js_string(rounded), row["round"].as_str().unwrap(), "{row}");
let negative_zero = rounded == 0.0 && rounded.is_sign_negative();
assert_eq!(
negative_zero,
row["negativeZero"].as_bool().unwrap(),
"{row}"
);
}
for row in rows(&semantics, "trim") {
let text = row["text"].as_str().unwrap();
assert_eq!(
text.trim_matches(is_js_space),
row["trimmed"].as_str().unwrap()
);
let space = text.chars().next().unwrap();
assert_eq!(
is_js_space(space),
row["isSpace"].as_bool().unwrap(),
"{space:?}"
);
}
}
#[test]
fn object_keys_follow_javascript_order() {
let semantics = oracle("js-semantics.json");
for row in rows(&semantics, "objectEntriesOrder") {
let value = parse_json(row["text"].as_str().unwrap()).unwrap();
let keys: Vec<&String> = js_keys(value.as_object().unwrap());
let expected: Vec<&str> = rows(row, "keys")
.iter()
.map(|k| k.as_str().unwrap())
.collect();
assert_eq!(keys, expected, "{row}");
}
}
/// `String(number)`, NaN and the infinities included.
fn js_string(value: f64) -> String {
if value.is_nan() {
"NaN".to_owned()
} else if value.is_infinite() {
if value > 0.0 { "Infinity" } else { "-Infinity" }.to_owned()
} else {
js_number(value)
}
}
+169
View File
@@ -0,0 +1,169 @@
//! The Rust legacy importer against the TypeScript (R5.2): the committed `legacy-import/`
//! folder byte for byte, the intermediate values frozen in `oracles/r5/pipeline.json`, the
//! raw report against the pre-Prettier digest of the ledger, two runs byte-equal, and the
//! synthetic cases of `oracles/r5/legacy-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 common;
use faunosteo_core::curation::ids::sha256_hex;
use faunosteo_core::curation::legacy::report::render_import_report;
use faunosteo_core::curation::legacy::{
EquineLegacyInput, ImportResult, SvgSource, import_equine_legacy, render_artifacts,
};
use faunosteo_core::json::to_js_compact;
use serde_json::Value;
const SOURCES: &str = "supabase/seed/anatomical-sheets/equine/legacy-sources";
const LEGACY: &str = "supabase/seed/anatomical-sheets/equine/legacy-import";
fn bytes(relative: &str) -> Vec<u8> {
std::fs::read(common::repo_path(relative)).unwrap()
}
fn equine_input() -> EquineLegacyInput {
EquineLegacyInput {
main_svg: SvgSource {
file_name: "Horse.svg".into(),
bytes: bytes(&format!("{SOURCES}/Horse.svg")),
},
head_svg: SvgSource {
file_name: "Horse_head.svg".into(),
bytes: bytes(&format!("{SOURCES}/Horse_head.svg")),
},
catalog: bytes(&format!("{SOURCES}/pathologies.json")),
vertebrae: bytes(&format!("{SOURCES}/vertebrae.ts")),
}
}
fn oracle(name: &str) -> Value {
serde_json::from_str(&common::repo_file(&format!(
"crates/core/tests/oracles/r5/{name}"
)))
.unwrap()
}
/// The parts of a result the TypeScript `ImportResult` holds besides the two documents.
fn parts(result: &ImportResult) -> Value {
let mut value = serde_json::to_value(result).unwrap();
let map = value.as_object_mut().unwrap();
map.remove("document");
map.remove("legacyIdMap");
value
}
fn assert_same(actual: &Value, expected: &Value, what: &str) {
assert_eq!(
to_js_compact(actual).unwrap(),
to_js_compact(expected).unwrap(),
"{what}"
);
}
#[test]
fn the_committed_legacy_import_folder_is_reproduced_byte_for_byte() {
let result = import_equine_legacy(&equine_input()).unwrap();
let files = render_artifacts(&result).unwrap();
let names: Vec<&str> = files.iter().map(|(name, _)| name.as_str()).collect();
assert_eq!(
names,
[
"document.json",
"legacy-id-map.json",
"assets.json",
"IMPORT-REPORT.md"
]
);
for (name, text) in &files {
assert!(
*text == common::repo_file(&format!("{LEGACY}/{name}")),
"{name} differs"
);
}
for asset in &result.assets {
let copy = bytes(&format!("{LEGACY}/assets/{}", asset.file));
assert_eq!(copy, bytes(&format!("{SOURCES}/{}", asset.file)));
assert_eq!(sha256_hex(&copy), asset.sha256);
}
}
#[test]
fn the_intermediate_values_match_the_frozen_typescript_run() {
let result = import_equine_legacy(&equine_input()).unwrap();
let frozen = oracle("pipeline.json");
let legacy = frozen["legacy"].as_object().unwrap();
let actual = parts(&result);
for (key, expected) in legacy {
assert_same(&actual[key], expected, key);
}
assert_eq!(result.sheet_id, legacy["sheetId"].as_str().unwrap());
}
#[test]
fn the_raw_report_is_the_text_prettier_received() {
let result = import_equine_legacy(&equine_input()).unwrap();
let raw = render_import_report(&result).unwrap();
let frozen = oracle("pipeline.json");
let row = frozen["artifacts"]
.as_array()
.unwrap()
.iter()
.find(|row| row["folder"] == LEGACY && row["file"] == "IMPORT-REPORT.md")
.unwrap();
assert_eq!(
sha256_hex(raw.as_bytes()),
row["before_prettier_sha256"].as_str().unwrap()
);
}
#[test]
fn two_runs_emit_the_same_bytes() {
let first = render_artifacts(&import_equine_legacy(&equine_input()).unwrap()).unwrap();
let second = render_artifacts(&import_equine_legacy(&equine_input()).unwrap()).unwrap();
assert_eq!(first, second);
}
fn synthetic_input(case: &Value) -> EquineLegacyInput {
let text = |key: &str| case[key].as_str().unwrap().as_bytes().to_vec();
EquineLegacyInput {
main_svg: SvgSource {
file_name: "Main.svg".into(),
bytes: text("main"),
},
head_svg: SvgSource {
file_name: "Head.svg".into(),
bytes: text("head"),
},
catalog: text("catalog"),
vertebrae: text("vertebrae"),
}
}
#[test]
fn the_synthetic_cases_match_the_typescript_or_are_refused_alike() {
let frozen = oracle("legacy-synthetic.json");
for case in frozen["cases"].as_array().unwrap() {
let name = case["name"].as_str().unwrap();
let outcome = import_equine_legacy(&synthetic_input(case));
if !case["ok"].as_bool().unwrap() {
assert!(
outcome.is_err(),
"{name}: the TypeScript refused, the port accepted"
);
continue;
}
let result = outcome.unwrap_or_else(|error| panic!("{name}: {error}"));
for (file, text) in render_artifacts(&result).unwrap() {
assert_eq!(
text,
case["files"][&file].as_str().unwrap(),
"{name} {file}"
);
}
let actual = parts(&result);
for (key, expected) in case["parts"].as_object().unwrap() {
assert_same(&actual[key], expected, &format!("{name} {key}"));
}
}
}
@@ -0,0 +1 @@
{"slugify":[{"label":"Cheval","slug":"cheval"},{"label":"Épaule","slug":"epaule"},{"label":"İstanbul","slug":"istanbul"},{"label":"KK","slug":"kk"},{"label":"ΣΑΣ ΟΔΟΣ","slug":""},{"label":"Straße","slug":"stra-e"},{"label":"fin","slug":"n"},{"label":"full","slug":""},{"label":"Džemal","slug":"emal"},{"label":"Ⅻ","slug":""},{"label":"x̧́y","slug":"xy"},{"label":"①②","slug":""},{"label":"a—b–c","slug":"a-b-c"},{"label":"__a__","slug":"a"},{"label":"œuvre Æther","slug":"uvre-ther"},{"label":"Ünïcödé","slug":"unicode"},{"label":"😀 smile","slug":"smile"},{"label":"한국어","slug":""},{"label":"ğ ş ı","slug":"g-s"},{"label":"1/2 et 3/4","slug":"1-2-et-3-4"},{"label":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaéb","slug":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaae"},{"label":"----------------------------------------------------------------------z","slug":"z"}],"deterministicUuid":[{"name":"faunosteo:sheet:canine:v1","uuid":"f565d6c8-9376-5f0c-8a88-b5f20c91bb17"},{"name":"faunosteo:sheet:feline:v12","uuid":"6e2061b2-68aa-50a3-bb17-dbf6b62cd192"},{"name":"faunosteo:asset:ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff","uuid":"51cf4b04-8520-57c8-99ba-561abfab37f3"},{"name":"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx","uuid":"44f83544-94a5-5a03-ba17-92a8d3e9c534"},{"name":"é","uuid":"4a99557e-4033-5353-9de2-eb65472017ca"},{"name":"😀","uuid":"f0443a34-2c5e-5547-83a1-11b51ba56c93"},{"name":"\u0000","uuid":"6e340b9c-ffb3-5a98-9ca5-44e6bb780a2c"}],"inferRegionCode":[{"fr":"Cervicales","en":null,"match":{"code":"C","root":"cervic","label":"Cervicales"}},{"fr":null,"en":"Thoracic","match":{"code":"T","root":"thorac","label":"Thoracic"}},{"fr":"DORSALES","en":"Lumbar","match":{"code":"T","root":"dors","label":"DORSALES"}},{"fr":"Région sacrée","en":"Sacral","match":{"code":"S","root":"sacre","label":"Région sacrée"}},{"fr":"Coccygiennes","en":null,"match":{"code":"Cd","root":"coccyg","label":"Coccygiennes"}},{"fr":"Lombaires","en":"x","match":{"code":"L","root":"lombair","label":"Lombaires"}},{"fr":"Épaule","en":"Shoulder","match":null},{"fr":null,"en":null,"match":null},{"fr":"","en":"caudal","match":{"code":"Cd","root":"caudal","label":"caudal"}},{"fr":"Thoracolombaire","en":"","match":{"code":"T","root":"thorac","label":"Thoracolombaire"}},{"fr":"CERVICALES","en":"CERVICAL","match":{"code":"C","root":"cervic","label":"CERVICALES"}},{"fr":"Sacrum","en":"Sacrum","match":null}],"number":[{"text":"","string":"0","negativeZero":false},{"text":" ","string":"0","negativeZero":false},{"text":"12","string":"12","negativeZero":false},{"text":" 12 ","string":"12","negativeZero":false},{"text":"0x1A","string":"26","negativeZero":false},{"text":"0X1a","string":"26","negativeZero":false},{"text":"0b11","string":"3","negativeZero":false},{"text":"0o17","string":"15","negativeZero":false},{"text":"1e3","string":"1000","negativeZero":false},{"text":"1E-3","string":"0.001","negativeZero":false},{"text":".5","string":"0.5","negativeZero":false},{"text":"5.","string":"5","negativeZero":false},{"text":"+5","string":"5","negativeZero":false},{"text":"-5","string":"-5","negativeZero":false},{"text":"Infinity","string":"Infinity","negativeZero":false},{"text":"-Infinity","string":"-Infinity","negativeZero":false},{"text":"+Infinity","string":"Infinity","negativeZero":false},{"text":"infinity","string":"NaN","negativeZero":false},{"text":"NaN","string":"NaN","negativeZero":false},{"text":"abc","string":"NaN","negativeZero":false},{"text":"1,5","string":"NaN","negativeZero":false},{"text":" 12 ","string":"12","negativeZero":false},{"text":"7","string":"7","negativeZero":false},{"text":"12px","string":"NaN","negativeZero":false},{"text":"1_000","string":"NaN","negativeZero":false},{"text":"00012","string":"12","negativeZero":false},{"text":"-0","string":"0","negativeZero":true},{"text":"0x","string":"NaN","negativeZero":false},{"text":"1e","string":"NaN","negativeZero":false},{"text":"1e+Line truncated
@@ -0,0 +1 @@
{"cases":[{"name":"generic","main":"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 100 50\">\n <circle cx=\"10.5\" cy=\" 20 \" r=\" 2 \" data-anno-id=\"a1\" data-anno-name-fr=\"Hanche\" data-anno-name-en=\"Hip\" data-anno-category=\"articulations\" data-anno-category-name=\"articulations\" data-anno-lateral=\"bilateral\"/>\n <circle cx=\"1\" cy=\"1\" r=\"1\" data-anno-id=\"a1\" data-anno-name-fr=\"Hanche\" data-anno-name-en=\"Hip\"/>\n <polygon points=\" 1,2 3,4 5,6 7 \" data-anno-id=\"p1\" data-anno-name-fr=\"Colonne\" data-anno-name-en=\"Spine\" data-anno-category=\"charni-res-vert-brales\" data-anno-special=\"vertebral-precision\" data-anno-lateral=\"left\"/>\n <polygon points=\"0x10,1e1 2,3 4,5\" data-anno-id=\"p2\" data-anno-name-fr=\"Région sacrée\" data-anno-category=\"zone-colonne\" data-anno-category-name=\"Colonne vertébrale\" data-anno-special=\"vertebral-precision\"/>\n <rect x=\"1\" y=\"2\" data-anno-id=\"r1\" data-anno-name-en=\"Rect only\" data-anno-category=\"os\" data-anno-special=\"other\"/>\n <circle cx=\"5\" cy=\"5\" r=\"2\" data-anno-id=\"t1\" data-anno-opens-detail=\"head\" data-anno-category=\"cadrans-du-cr-ne\"/>\n <circle cx=\"840.1692504882812\" cy=\"-0\" r=\"3\" data-anno-id=\"x1\" data-anno-name-fr=\"Dix\" data-anno-name-en=\"Ten\" data-anno-category=\"os\" data-anno-lateral=\"unpaired\"/>\n</svg>\n","head":"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<svg xmlns=\"http://www.w3.org/2000/svg\">\n <circle cx=\"3\" cy=\"4\" r=\"5\" data-anno-id=\"h1\" data-anno-name-fr=\"Deux\" data-anno-category=\"os\"/>\n <circle cx=\"3\" cy=\"4\" r=\"5\" data-anno-id=\"h2\" data-anno-name-fr=\"Deux\" data-anno-name-en=\"Two\" data-anno-category=\"os\"/>\n <circle cx=\"1\" cy=\"1\" r=\"1\" data-anno-id=\"a1\" data-anno-name-fr=\"Thoracique\" data-anno-name-en=\"Thoracic\" data-anno-category=\"os\" data-anno-special=\"vertebral-precision\"/>\n</svg>\n","catalog":"{\n \"schema_version\": 2.5,\n \"scope_disclaimer\": \"Périmètre « test »\",\n \"zones\": {\n \"2\": {\n \"id\": \"2\",\n \"name_fr\": \"Deux\",\n \"name_en\": \"Two\",\n \"categories\": [\n {\n \"id\": \"weird\",\n \"name_fr\": \"weird fr\",\n \"name_en\": \"weird en\"\n },\n {\n \"id\": \"anamnesis\",\n \"name_fr\": \"anamnesis fr\",\n \"name_en\": \"anamnesis en\",\n \"children\": [\n {\n \"id\": \"age\",\n \"name_fr\": \"age fr\",\n \"name_en\": \"age en\"\n },\n {\n \"id\": \"use\",\n \"name_fr\": \"use fr\",\n \"name_en\": \"use en\"\n }\n ]\n }\n ]\n },\n \"10\": {\n \"id\": \"10\",\n \"name_fr\": \"Dix\",\n \"name_en\": \"Ten\",\n \"categories\": [\n {\n \"id\": \"anamnesis\",\n \"name_fr\": \"anamnesis fr\",\n \"name_en\": \"anamnesis en\",\n \"children\": [\n {\n \"id\": \"age\",\n \"name_fr\": \"age fr\",\n \"name_en\": \"age en\"\n }\n ]\n }\n ]\n },\n \"hip\": {\n \"id\": \"hip\",\n \"name_fr\": \"Hanche\",\n \"name_en\": \"Hip\",\n \"categories\": [\n {\n \"id\": \"hip-dysfunctions\",\n \"name_fr\": \"hip-dysfunctions fr\",\n \"name_en\": \"hip-dysfunctions en\",\n \"children\": [\n {\n \"id\": \"Épaule Gauche\",\n \"name_fr\": \"Épaule Gauche fr\",\n \"name_en\": \"Épaule Gauche en\",\n \"description_fr\": \"seulement FR\"\n },\n {\n \"id\": \"ok\",\n \"name_fr\": \"ok fr\",\n \"name_en\": \"ok en\",\n \"requires_vet_referral\": true,\n \"children\": []\n },\n {\n \"id\": \"deep\",\n \"name_fr\": \"deep fr\",\n \"name_en\": \"deep en\",\n \"children\": [\n {\n \"id\": \"leaf\",\n \"name_fr\": \"leaf fr\",\n \"name_en\": \"leaf en\",\n \"description_en\": \"EN only\"\n }\n ]\n }\n ]\n },\n {\n \"id\": \"anamnesis\",\n \"name_fr\": \"anamnesis fr\",\n \"name_en\": \"anamnesis en\",\n \"children\": [\n {\n \"id\": \"age\",\n \"name_fr\": \"age fr\",\n \"name_en\": \"age en\"\n },\n {\n \"id\": \"use\",\n \"name_fr\": \"use fr\",\n \"name_en\": \"use en\"\n }\n ]\n },\n {\n \"id\": \"spine-observations\",\n \"name_fr\": \"spine-observaLine truncated
@@ -0,0 +1 @@
{"tool":"prettier formatMarkdown (packages/anatomy/src/format.ts)","cases":[{"raw":"# T\n\n| A | B |\n| --- | --- |\n| 1 | 2 |\n","output":"# T\n\n| A | B |\n| --- | --- |\n| 1 | 2 |\n"},{"raw":"# T\n\n| Mesure | Valeur |\n| --- | --- |\n| Nœuds | 792 |\n| x | - |\n","output":"# T\n\n| Mesure | Valeur |\n| ------ | ------ |\n| Nœuds | 792 |\n| x | - |\n"},{"raw":"# T\n\n| a |\n| --- |\n| |\n","output":"# T\n\n| a |\n| --- |\n| |\n"},{"raw":"# T\n\n| Code | Objet |\n| --- | --- |\n| `a\\|b` | x \\| y |\n| ligne suite | |\n","output":"# T\n\n| Code | Objet |\n| ----------- | ------ |\n| `a\\|b` | x \\| y |\n| ligne suite | |\n"},{"raw":"# T\n\n| é | é |\n| --- | --- |\n| àéîõü | ç |\n| –— | «» |\n","output":"# T\n\n| é | é |\n| ----- | --- |\n| àéîõü | ç |\n| –— | «» |\n"},{"raw":"# T\n\n| 漢字 | emoji |\n| --- | --- |\n| 漢字漢字 | 😀 |\n| full | 👍🏽 |\n","output":"# T\n\n| 漢字 | emoji |\n| -------- | ----- |\n| 漢字漢字 | 😀 |\n| full | 👍🏽 |\n"},{"raw":"# T\n\n| ctl | zw |\n| --- | --- |\n| a\u0001b | a​b |\n| ️ | x |\n","output":"# T\n\n| ctl | zw |\n| --- | --- |\n| a\u0001b | a​b |\n| ️ | x |\n"},{"raw":"# T\n\n| Long | Court |\n| --- | --- |\n| xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx | y |\n| `code` | **gras** |\n","output":"# T\n\n| Long | Court |\n| ------------------------------------------------------------------------------------------------------------------------------------------------------ | -------- |\n| xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx | y |\n| `code` | **gras** |\n"},{"raw":"# T\n\n| Total |\n| --- |\n| **Total** |\n| |\n","output":"# T\n\n| Total |\n| --------- |\n| **Total** |\n| |\n"},{"raw":"# T\n\n| n | m | o |\n| --- | --- | --- |\n| 1 | 22 | 333 |\n| 4444 | 5 | 66 |\n","output":"# T\n\n| n | m | o |\n| ---- | --- | --- |\n| 1 | 22 | 333 |\n| 4444 | 5 | 66 |\n"}]}
@@ -0,0 +1 @@
{"tool":"prettier formatJson (packages/anatomy/src/format.ts)","cases":[{"input":"{}","output":"{}\n"},{"input":"[]","output":"[]\n"},{"input":"{\"a\":{}}","output":"{\n \"a\": {}\n}\n"},{"input":"{\"a\":[]}","output":"{\n \"a\": []\n}\n"},{"input":"{\"a\":[{}]}","output":"{\n \"a\": [{}]\n}\n"},{"input":"{\"a\":[[]]}","output":"{\n \"a\": [[]]\n}\n"},{"input":"{\"a\":[null,true,false]}","output":"{\n \"a\": [null, true, false]\n}\n"},{"input":"{\"a\":[1e+21,0,1e-7,-5,0.5,123456789012345680000]}","output":"{\n \"a\": [1e21, 0, 1e-7, -5, 0.5, 123456789012345680000]\n}\n"},{"input":"{\"a\":[-1,-2,-3]}","output":"{\n \"a\": [-1, -2, -3]\n}\n"},{"input":"{\"a\":[1,\"b\",null]}","output":"{\n \"a\": [1, \"b\", null]\n}\n"},{"input":"{\"a\":[[1,2],[3]]}","output":"{\n \"a\": [[1, 2], [3]]\n}\n"},{"input":"{\"a\":[[1],[2]]}","output":"{\n \"a\": [[1], [2]]\n}\n"},{"input":"{\"a\":[[1,2],[3,4]]}","output":"{\n \"a\": [\n [1, 2],\n [3, 4]\n ]\n}\n"},{"input":"{\"a\":[[1,2],{\"b\":1}]}","output":"{\n \"a\": [\n [1, 2],\n {\n \"b\": 1\n }\n ]\n}\n"},{"input":"{\"a\":[{\"b\":1},{\"c\":2}]}","output":"{\n \"a\": [\n {\n \"b\": 1\n },\n {\n \"c\": 2\n }\n ]\n}\n"},{"input":"{\"a\":[{\"b\":1,\"c\":2},{\"d\":3,\"e\":4}]}","output":"{\n \"a\": [\n {\n \"b\": 1,\n \"c\": 2\n },\n {\n \"d\": 3,\n \"e\": 4\n }\n ]\n}\n"},{"input":"{\"a\":[[[1,2],[3,4]],[[5,6]]]}","output":"{\n \"a\": [\n [\n [1, 2],\n [3, 4]\n ],\n [[5, 6]]\n ]\n}\n"},{"input":"{\"a\":[[\"x\",\"y\"],[\"z\",\"w\"]]}","output":"{\n \"a\": [\n [\"x\", \"y\"],\n [\"z\", \"w\"]\n ]\n}\n"},{"input":"{\"a\":[[\"x\"],[\"y\",\"z\"]]}","output":"{\n \"a\": [[\"x\"], [\"y\", \"z\"]]\n}\n"},{"input":"{\"clé é\":[\"é\",\"à\",\"«»\"],\"漢字\":[\"漢字漢字\",\"😀\"]}","output":"{\n \"clé é\": [\"é\", \"à\", \"«»\"],\n \"漢字\": [\"漢字漢字\", \"😀\"]\n}\n"},{"input":"{\"a\":[\"é\",\"full\",\"​\"]}","output":"{\n \"a\": [\"é\", \"full\", \"​\"]\n}\n"},{"input":"{\"a\":\"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\",\"b\":[\"yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy\"]}","output":"{\n \"a\": \"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\",\n \"b\": [\n \"yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy\"\n ]\n}\n"},{"input":"{\"a\\\"b\":[\"\\\\\",\"\\\"\",\"\\n\",\"\\u0001\"]}","output":"{\n \"a\\\"b\": [\"\\\\\", \"\\\"\", \"\\n\", \"\\u0001\"]\n}\n"},{"input":"{\"k\":[3]}","output":"{\n \"k\": [3]\n}\n"},{"input":"{\"k\":[13]}","output":"{\n \"k\": [13]\n}\n"},{"input":"{\"k\":[13]}","output":"{\n \"k\": [13]\n}\n"},{"input":"{\"k\":[13]}","output":"{\n \"k\": [13]\n}\n"},{"input":"{\"k\":[\"a\"],\"z\":1}","output":"{\n \"k\": [\"a\"],\n \"z\": 1\n}\n"},{"input":"{\"k\":[\"aaaaa\"],\"z\":1}","output":"{\n \"k\": [\"aaaaa\"],\n \"z\": 1\n}\n"},{"input":"{\"k\":[\"aaaaaaaaaaaa\"],\"z\":1}","output":"{\n \"k\": [\"aaaaaaaaaaaa\"],\n \"z\": 1\n}\n"},{"input":"{\"k\":[3,2]}","output":"{\n \"k\": [3, 2]\n}\n"},{"input":"{\"k\":[13,932]}","output":"{\n \"k\": [13, 932]\n}\n"},{"input":"{\"k\":[13,7932]}","output":"{\n \"k\": [13, 7932]\n}\n"},{"input":"{\"k\":[13,7932]}","output":"{\n \"k\": [13, 7932]\n}\n"},{"input":"{\"k\":[\"a\",\"b\"],\"z\":1}","output":"{\n \"k\": [\"a\", \"b\"],\n \"z\": 1\n}\n"},{"input":"{\"k\":[\"aaaaa\",\"bbbbb\"],\"z\":1}","output":"{\n \"k\": [\"aaaaa\", \"bbbbb\"],\n \"z\": 1\n}\n"},{"input":"{\"k\":[\"aaaaaaaaaaaa\",\"bbbbbbbbbbbb\"],\"z\":1}","output":"{\n \"k\": [\"aaaaaaaaaaaa\", \"bbbbbbbbbbbb\"],\n \"z\": 1\n}\n"},{"input":"{\"k\":[3,2,1]}","output":"{\n \"k\": [3, 2, 1]\n}\n"},{"input":"{\"k\":[13,932,851]}","output":"{\n \"k\": [13, 932, 851]\n}\n"},{"input":"{\"k\":[13,7932,5851]}","output":"{\n \"k\": [13, 7932, 5851]\n}\n"},{"input":"{\"k\":[13,7932,15851]}","output":"{\n \"k\": [13, 7932, 15851]\n}\n"},{"input":"{\"k\":[\"a\",\"b\",\"c\"],\"z\":1}","output":"{\n \"k\": [\"a\", \"b\", \"c\"],\n \"z\": 1\n}\n"},{"input":"{\"k\":[\"aaaaa\",\"bbbbb\",\"ccccc\"],\"z\":1}","output":"{\n \"k\": [\"aaaaa\", \"bbbbb\", \"ccccc\"],\n \"z\": 1\n}\n"},{"input":"{\"k\":[\"aaaaaaaaaaaa\",\"bbbbbbbbbbbb\",\"cccccccccccc\"],\"z\":1}","output":"{\n \"k\": [\"aaaaaaaaaaaa\", \"bbbbbbbbbbbb\", \"cccccccccccc\"],\n \"z\": 1\n}\n"},{"input":"{\"k\":[3,2,1,0,9,8,7,6]}","output":"{\n \"k\": [3, 2, 1, 0, 9, 8, 7, 6]\n}\n"},{"input":"{\"k\":[13,932,851,770,689,608,527,446]}","output":"{\n \"k\": [13, 932, 851, 770, 689, 608, 527, 446]\Line truncated
@@ -0,0 +1,391 @@
/**
* R5.2 freeze (owner brief 2026-10-22 §9): the formatting and JavaScript
* semantics the Rust pipeline must reproduce, recorded from the tools
* themselves on generic inputs, not only on the equine data. Three oracles
* under crates/core/tests/oracles/r5/:
*
* - prettier-json.json: `formatJson` (Prettier 3, parser json, the
* repository options) over synthetic values probing the layout rules:
* short arrays collapsed, number arrays filled, the 80-column boundary at
* several depths and key widths, nested arrays, objects, empty
* containers, Unicode widths.
* - markdown-tables.json: `formatMarkdown` over raw tables as
* `markdown.ts` writes them (alignment, padding, Unicode widths, escaped
* pipes, empty cells).
* - js-semantics.json: `slugify`, `deterministicUuid`, `inferRegionCode`,
* `Number()`, the points split of the SVG reader, `Math.round`,
* `Object.entries` key order of `JSON.parse`, `toLowerCase`, NFD.
*
* `MAKE_R5_ORACLES=1` writes the files; otherwise the tools still match the
* freeze.
*/
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 { formatJson, formatMarkdown } from '../src/format'
import { table } from '../src/markdown'
import { deterministicUuid, inferRegionCode, slugify } from '../src'
const HERE = dirname(fileURLToPath(import.meta.url))
const OUT = resolve(HERE, '../../../crates/core/tests/oracles/r5')
const MAKE = process.env.MAKE_R5_ORACLES === '1'
/** Numbers of `digits` characters each, deterministic. */
function numbers(count: number, digits: number): number[] {
return Array.from({ length: count }, (_, i) =>
Number(String((i * 7919 + 13) % 10 ** digits).padStart(1, '0')),
)
}
function strings(count: number, width: number): string[] {
return Array.from({ length: count }, (_, i) =>
`${String.fromCharCode(97 + (i % 26))}`.repeat(width),
)
}
/** One value per layout rule, each nested so the boundary moves. */
function jsonCases(): unknown[] {
const cases: unknown[] = [
{},
[],
{ a: {} },
{ a: [] },
{ a: [{}] },
{ a: [[]] },
{ a: [null, true, false] },
{ a: [1e21, -0, 1e-7, -5, 0.5, 123456789012345680000] },
{ a: [-1, -2, -3] },
{ a: [1, 'b', null] },
{ a: [[1, 2], [3]] },
{ a: [[1], [2]] },
{
a: [
[1, 2],
[3, 4],
],
},
{ a: [[1, 2], { b: 1 }] },
{ a: [{ b: 1 }, { c: 2 }] },
{
a: [
{ b: 1, c: 2 },
{ d: 3, e: 4 },
],
},
{
a: [
[
[1, 2],
[3, 4],
],
[[5, 6]],
],
},
{
a: [
['x', 'y'],
['z', 'w'],
],
},
{ a: [['x'], ['y', 'z']] },
{ 'clé é': ['é', 'à', '«»'], 漢字: ['漢字漢字', '😀'] },
{ a: ['é', 'full', '​'] },
{ a: 'x'.repeat(200), b: ['y'.repeat(100)] },
{ 'a"b': ['\\', '"', '\n', '\u0001'] },
]
for (const depth of [0, 1, 3, 6]) {
for (const key of ['k', 'key_of_twenty_chars_', 'k'.repeat(40)]) {
for (const count of [1, 2, 3, 8, 12, 13, 14, 15, 16, 30]) {
for (const digits of [1, 3, 4, 6]) {
let value: unknown = { [key]: numbers(count, digits) }
for (let d = 0; d < depth; d++) value = { n: value }
cases.push(value)
}
for (const width of [1, 5, 12]) {
let value: unknown = { [key]: strings(count, width), z: 1 }
for (let d = 0; d < depth; d++) value = { n: [value] }
cases.push(value)
}
}
}
}
for (let total = 70; total <= 90; total++) {
const pad = 'p'.repeat(total - 20)
cases.push({ [pad]: [1, 2] }, { [pad]: [1, 2], last: true })
cases.push({ [pad]: ['ab', 'cd'] }, [{ [pad]: [1, 2, 3] }])
cases.push({ x: [[Number('1'.repeat(Math.max(1, total - 70))), 2]] })
}
return cases
}
type Row = Array<string | number | null>
function tableCases(): Array<{ head: string[]; rows: Row[] }> {
return [
{ head: ['A', 'B'], rows: [['1', '2']] },
{
head: ['Mesure', 'Valeur'],
rows: [
['Nœuds', 792],
['x', null],
],
},
{ head: ['a'], rows: [['']] },
{
head: ['Code', 'Objet'],
rows: [
['`a|b`', 'x | y'],
['ligne\nsuite', ''],
],
},
{
head: ['é', 'é'],
rows: [
['àéîõü', 'ç'],
['–—', '«»'],
],
},
{
head: ['漢字', 'emoji'],
rows: [
['漢字漢字', '😀'],
['full', '👍🏽'],
],
},
{
head: ['ctl', 'zw'],
rows: [
['a\u0001b', 'a​b'],
['️', 'x'],
],
},
{
head: ['Long', 'Court'],
rows: [
['x'.repeat(150), 'y'],
['`code`', '**gras**'],
],
},
{ head: ['Total'], rows: [['**Total**'], ['']] },
{
head: ['n', 'm', 'o'],
rows: [
[1, 22, 333],
[4444, 5, 66],
],
},
]
}
/** IEEE 754 bits in hex: `String(-0)` is `"0"`, the bits keep the sign. */
function bitsOf(value: number): string {
const view = new DataView(new ArrayBuffer(8))
view.setFloat64(0, value)
return view.getBigUint64(0).toString(16).padStart(16, '0')
}
const SPACES = [' ', '\t', '\n', ' ', '', '
', ' ', '​']
function semantics() {
const slugInputs = [
'Cheval',
'Épaule',
'İstanbul',
'KK',
'ΣΑΣ ΟΔΟΣ',
'Straße',
'fin',
'full',
'Džemal',
'Ⅻ',
'x̧́y',
'①②',
'a—b–c',
'__a__',
'œuvre Æther',
'Ünïcödé',
'😀 smile',
'한국어',
'ğ ş ı',
'1/2 et 3/4',
'a'.repeat(63) + 'é' + 'b',
'-'.repeat(70) + 'z',
]
const uuidNames = [
'faunosteo:sheet:canine:v1',
'faunosteo:sheet:feline:v12',
'faunosteo:asset:' + 'f'.repeat(64),
'x'.repeat(1000),
'é',
'😀',
'\u0000',
]
const regionLabels: Array<[string | null, string | null]> = [
['Cervicales', null],
[null, 'Thoracic'],
['DORSALES', 'Lumbar'],
['Région sacrée', 'Sacral'],
['Coccygiennes', null],
['Lombaires', 'x'],
['Épaule', 'Shoulder'],
[null, null],
['', 'caudal'],
['Thoracolombaire', ''],
['CERVICALES', 'CERVICAL'],
['Sacrum', 'Sacrum'],
]
const numberTexts = [
'',
' ',
'12',
' 12 ',
'0x1A',
'0X1a',
'0b11',
'0o17',
'1e3',
'1E-3',
'.5',
'5.',
'+5',
'-5',
'Infinity',
'-Infinity',
'+Infinity',
'infinity',
'NaN',
'abc',
'1,5',
' 12 ',
'7',
'12px',
'1_000',
'00012',
'-0',
'0x',
'1e',
'1e+',
' \n\t3 ',
'
3',
'1.7976931348623157e309',
'5e-325',
'0.1e1',
'-.5',
'--5',
'+-5',
'0x-1',
]
const pointTexts = [
' 1,2 3,4 ',
'1,,2',
',1,2',
'1 2 3',
'',
'1e3,2',
' 1 2',
'1.5,2.25 ',
'1,2,3,4,5,6',
'a,b',
' ',
'1\n2\t3 4',
]
const roundValues = [
0.5, 1.5, 2.5, -0.5, -1.5, -2.5, 0.49999999999999994, -0.49999999999999994,
4503599627370495.5, -4503599627370495.5, 1e21, -0.4, 3.7, -3.7, 0, -0,
2.4999999999999996, 1.0000000000000002,
]
const keyTexts = [
'{"b":1,"2":2,"a":3,"1":4,"01":5,"4294967295":6,"4294967294":7,"-1":8,"1.5":9}',
'{"z":1,"y":2,"10":3,"9":4,"0":5}',
'{"a":1,"a":2,"b":3}',
]
return {
slugify: slugInputs.map((label) => ({ label, slug: slugify(label) })),
deterministicUuid: uuidNames.map((name) => ({
name,
uuid: deterministicUuid(name),
})),
inferRegionCode: regionLabels.map(([fr, en]) => ({
fr,
en,
match: inferRegionCode(fr, en),
})),
number: numberTexts.map((text) => {
const value = Number(text)
return { text, string: String(value), negativeZero: Object.is(value, -0) }
}),
points: pointTexts.map((text) => ({
text,
numbers: text
.trim()
.split(/[\s,]+/)
.map((n) => String(Number(n))),
})),
mathRound: roundValues.map((value) => {
const r = Math.round(value)
return {
bits: bitsOf(value),
value: String(value),
round: String(r),
negativeZero: Object.is(r, -0),
}
}),
objectEntriesOrder: keyTexts.map((text) => ({
text,
keys: Object.keys(JSON.parse(text) as object),
})),
trim: SPACES.map((space) => ({
text: `${space}x${space}`,
trimmed: `${space}x${space}`.trim(),
isSpace: /\s/.test(space),
})),
}
}
async function prettierJson() {
const cases = jsonCases()
const out = []
for (const value of cases) {
out.push({ input: JSON.stringify(value), output: await formatJson(value) })
}
return {
tool: 'prettier formatJson (packages/anatomy/src/format.ts)',
cases: out,
}
}
async function markdownTables() {
const out = []
for (const { head, rows } of tableCases()) {
const raw = `# T\n\n${table(head, rows)}\n`
out.push({ raw, output: await formatMarkdown(raw) })
}
return {
tool: 'prettier formatMarkdown (packages/anatomy/src/format.ts)',
cases: out,
}
}
function check(name: string, value: unknown): void {
const text = `${JSON.stringify(value)}\n`
if (MAKE) {
mkdirSync(OUT, { recursive: true })
writeFileSync(resolve(OUT, name), text)
}
expect(text).toBe(readFileSync(resolve(OUT, name), 'utf8'))
}
describe('R5.2 format and semantics oracles', () => {
it('prettier-json.json', async () => {
check('prettier-json.json', await prettierJson())
})
it('markdown-tables.json', async () => {
check('markdown-tables.json', await markdownTables())
})
it('js-semantics.json', () => {
check('js-semantics.json', semantics())
})
})
@@ -0,0 +1,286 @@
/**
* R5.2 freeze (owner brief 2026-10-22 §9): the TypeScript legacy importer on synthetic
* inputs, so the Rust port is held to the generic contract and not only to the equine
* files. Each case is a full importer input (two SVG texts, the catalog text, the
* vertebrae text); the oracle records the four rendered files and the result's parts, or
* the refusal. `MAKE_R5_ORACLES=1` writes crates/core/tests/oracles/r5/legacy-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 { importEquineLegacy, renderArtifacts } from '../src'
const HERE = dirname(fileURLToPath(import.meta.url))
const OUT = resolve(HERE, '../../../crates/core/tests/oracles/r5')
const MAKE = process.env.MAKE_R5_ORACLES === '1'
interface Case {
name: string
main: string
head: string
catalog: string
vertebrae: string
}
const SVG_HEAD = '<?xml version="1.0" encoding="UTF-8"?>\n'
function svg(viewBox: string | null, body: string): string {
const box = viewBox === null ? '' : ` viewBox="${viewBox}"`
return `${SVG_HEAD}<svg xmlns="http://www.w3.org/2000/svg"${box}>\n${body}\n</svg>\n`
}
function anno(
tag: string,
attrs: Record<string, string>,
geometry: string,
): string {
const data = Object.entries(attrs)
.map(([k, v]) => ` data-anno-${k}="${v}"`)
.join('')
return ` <${tag}${geometry}${data}/>`
}
const VERTEBRAE = `// varies 18 - 22 caudal
export const FORMULA = { C: 7, T: 18, L: 6, S: 5, Cd: 18, Cd: 20 }
`
function node(id: string, extra: Record<string, unknown> = {}) {
return { id, name_fr: `${id} fr`, name_en: `${id} en`, ...extra }
}
function zone(id: string, fr: string, en: string, categories: unknown[]) {
return { id, name_fr: fr, name_en: en, categories }
}
const CATALOG = {
schema_version: 2.5,
scope_disclaimer: 'Périmètre « test »',
zones: {
hip: zone('hip', 'Hanche', 'Hip', [
node('hip-dysfunctions', {
children: [
node('Épaule Gauche', { description_fr: 'seulement FR' }),
node('ok', { requires_vet_referral: true, children: [] }),
node('deep', {
children: [node('leaf', { description_en: 'EN only' })],
}),
],
}),
node('anamnesis', { children: [node('age'), node('use')] }),
node('spine-observations', { children: [node('ok')] }),
]),
'10': zone('10', 'Dix', 'Ten', [
node('anamnesis', { children: [node('age')] }),
]),
'2': zone('2', 'Deux', 'Two', [
node('weird'),
node('anamnesis', { children: [node('age'), node('use')] }),
]),
spine: zone('spine', 'Colonne', 'Spine', [
node('spine-dysfunctions', { children: [node('ok'), node('x y')] }),
]),
},
}
const MAIN_BODY = [
anno(
'circle',
{
id: 'a1',
'name-fr': 'Hanche',
'name-en': 'Hip',
category: 'articulations',
'category-name': 'articulations',
lateral: 'bilateral',
},
' cx="10.5" cy=" 20 " r=" 2 "',
),
anno(
'circle',
{ id: 'a1', 'name-fr': 'Hanche', 'name-en': 'Hip' },
' cx="1" cy="1" r="1"',
),
anno(
'polygon',
{
id: 'p1',
'name-fr': 'Colonne',
'name-en': 'Spine',
category: 'charni-res-vert-brales',
special: 'vertebral-precision',
lateral: 'left',
},
' points=" 1,2 3,4 5,6 7 "',
),
anno(
'polygon',
{
id: 'p2',
'name-fr': 'Région sacrée',
category: 'zone-colonne',
'category-name': 'Colonne vertébrale',
special: 'vertebral-precision',
},
' points="0x10,1e1 2,3 4,5"',
),
anno(
'rect',
{ id: 'r1', 'name-en': 'Rect only', category: 'os', special: 'other' },
' x="1" y="2"',
),
anno(
'circle',
{ id: 't1', 'opens-detail': 'head', category: 'cadrans-du-cr-ne' },
' cx="5" cy="5" r="2"',
),
anno(
'circle',
{
id: 'x1',
'name-fr': 'Dix',
'name-en': 'Ten',
category: 'os',
lateral: 'unpaired',
},
' cx="840.1692504882812" cy="-0" r="3"',
),
].join('\n')
const HEAD_BODY = [
anno(
'circle',
{ id: 'h1', 'name-fr': 'Deux', category: 'os' },
' cx="3" cy="4" r="5"',
),
anno(
'circle',
{ id: 'h2', 'name-fr': 'Deux', 'name-en': 'Two', category: 'os' },
' cx="3" cy="4" r="5"',
),
anno(
'circle',
{
id: 'a1',
'name-fr': 'Thoracique',
'name-en': 'Thoracic',
category: 'os',
special: 'vertebral-precision',
},
' cx="1" cy="1" r="1"',
),
].join('\n')
function cases(): Case[] {
const base: Case = {
name: 'generic',
main: svg('0 0 100 50', MAIN_BODY),
head: svg(null, HEAD_BODY),
catalog: JSON.stringify(CATALOG, null, 2),
vertebrae: VERTEBRAE,
}
const minimal: Case = {
name: 'minimal',
main:
'' +
svg(
'0 0 1 1',
anno(
'circle',
{ id: 'm', 'name-fr': 'Hanche', 'name-en': 'Hip', category: 'os' },
' cx="1" cy="1" r="1"',
),
),
head: svg('0 0 1 1', ''),
catalog: JSON.stringify({
schema_version: '2.0',
zones: {
hip: zone('hip', 'Hanche', 'Hip', [
node('hip-dysfunctions', { children: [node('a')] }),
]),
},
}),
vertebrae: 'const V = {C:7,T:18,L:6,S:5,Cd:21}',
}
const refusals: Case[] = [
{
...base,
name: 'schema',
main: base.main.replace('r="3"', 'r="Infinity"'),
},
{ ...base, name: 'no-zones', catalog: '{"schema_version":"2.0"}' },
{ ...base, name: 'catalog-bom', catalog: '' + base.catalog },
{
...base,
name: 'no-cd',
vertebrae: 'const V = { C: 7, T: 18, L: 6, S: 5 }',
},
{ ...base, name: 'no-formula', vertebrae: 'nothing here' },
{
...base,
name: 'inline-sources',
catalog: JSON.stringify({
zones: {
hip: zone('hip', 'Hanche', 'Hip', [node('c', { sources: [{}] })]),
},
}),
},
{
...base,
name: 'key-not-id',
catalog: JSON.stringify({
zones: { hip: zone('hop', 'Hanche', 'Hip', []) },
}),
},
{
...base,
name: 'truthy-referral',
catalog: JSON.stringify({
zones: {
hip: zone('hip', 'Hanche', 'Hip', [
node('c', {
children: [node('d', { requires_vet_referral: 'yes' })],
}),
]),
},
}),
},
]
return [base, minimal, ...refusals]
}
async function run(c: Case) {
const input = {
mainSvg: { fileName: 'Main.svg', bytes: new TextEncoder().encode(c.main) },
headSvg: { fileName: 'Head.svg', bytes: new TextEncoder().encode(c.head) },
catalogJson: c.catalog,
vertebraeSource: c.vertebrae,
}
try {
const result = importEquineLegacy(input)
const parts: Record<string, unknown> = { ...result }
delete parts.document
delete parts.legacyIdMap
return { ...c, ok: true, files: await renderArtifacts(result), parts }
} catch (error) {
return {
...c,
ok: false,
error: error instanceof Error ? error.message : String(error),
}
}
}
describe('R5.2 synthetic importer oracle', () => {
it('legacy-synthetic.json', async () => {
const rows = []
for (const c of cases()) rows.push(await run(c))
const text = `${JSON.stringify({ cases: rows })}\n`
const file = resolve(OUT, 'legacy-synthetic.json')
if (MAKE) {
mkdirSync(OUT, { recursive: true })
writeFileSync(file, text)
}
expect(text).toBe(readFileSync(file, 'utf8'))
})
})
+5
View File
@@ -24,6 +24,11 @@ cargo clippy -p faunosteo-core --all-targets --locked -- -D warnings
# (`report-compare`, R4.7) and its O10 suite, never built into the module.
cargo clippy -p faunosteo-core --all-targets --features cli --locked -- -D warnings
cargo test -p faunosteo-core --features cli --locked
# The anatomical pipeline (R5) is feature-gated too (`curation`): the legacy importer, the
# formatting of the generated seed files and the `import-equine` tool, never built into the
# module or the worker; its suites hold it to the TypeScript oracles of `oracles/r5`.
cargo clippy -p faunosteo-core --all-targets --features curation --locked -- -D warnings
cargo test -p faunosteo-core --features curation --locked
# Exports the TypeScript types of the contracts (ts-rs) into packages/core-wasm/types.
# `git status --porcelain --untracked-files=all -- packages/core-wasm/types` must stay
# empty afterwards (scripts/check-all.sh, .gitea/workflows/ci.yml): the committed types