Files
faunosteo/crates/core/src/curation/draft/pipeline.rs
T

291 lines
11 KiB
Rust

//! Legacy import → curated working draft (`packages/anatomy/src/curation/pipeline.ts`). The
//! asset step runs first as the reserved change CHG-000 (one cleaned drawing, head view =
//! crop), then the authored changeset; both land in the same records, so the CHANGELOG lists
//! them alike. Deterministic: same inputs, same bytes.
//!
//! The cleaned drawing is an input here, not a product: the owner froze the bytes of
//! `draft/assets/horse.svg` (BDR-068), so the Rust pipeline describes them (sha256, byte size,
//! asset id) and copies them, and never re-cleans the legacy drawing. The review drawings and
//! the two Markdown reports are R5.4.
use serde_json::{Map, Value, json};
use super::app_review::render_app_review_drawing;
use super::apply::{apply_changeset, curated_issues};
use super::changelog::render_changelog;
use super::drawing::read_cleaned_drawing;
use super::migration_map::{MigrationMapInput, SheetRef, build_report_migration_map};
use super::overlay::{OverlayInput, REVIEW_OVERLAY_DIR, render_review_overlay};
use super::records::ChangeRecord;
use super::review_report::{ReviewReportInput, render_review_report};
use super::schema::{
ASSET_CHANGE_ID, Change, ChangeKind, ChangeLevel, ChangePriority, ChangeSummary, ChangeTitle,
Op, SetOp,
};
use super::seed::{DRAFT_CHANGE_NOTE, DraftSeedParams, render_draft_seed_sql};
use super::stats::{DocumentStats, document_stats};
use super::targets::fail;
use crate::anatomy::svg::number::{js_number_of, js_split_list};
use crate::curation::PipelineError;
use crate::curation::ids::{asset_id_from_sha256, sha256_hex};
use crate::curation::legacy::types::{Anomaly, AssetRecord, ImportResult};
use crate::curation::legacy::{HEAD_VIEW_ID, MAIN_VIEW_ID};
use crate::curation::prettier_json::format_json;
use crate::curation::report_guard::format_report;
use crate::domain::sheet::{AnatomicalSheetDocument, Reference, SheetIssue, ViewBox};
pub const DRAFT_SEED_SQL_FILE: &str = "equine_draft.sql";
pub const SHEET_REF_FILE: &str = "sheet-ref.json";
pub const MIGRATION_MAP_FILE: &str = "report-migration-map.json";
pub const CLEANED_ASSET_FILE: &str = "horse.svg";
pub struct DraftInput<'a> {
pub import_result: &'a ImportResult,
/// The frozen cleaned drawing, `draft/assets/horse.svg`.
pub cleaned_svg: &'a [u8],
pub head_view_box: ViewBox,
pub references: &'a [Reference],
pub changes: &'a [Change],
}
pub struct DraftResult {
pub document: Value,
/// Legacy import the changeset was applied to.
pub baseline: Value,
pub legacy_id_map: Map<String, Value>,
pub records: Vec<ChangeRecord>,
pub changes: Vec<ChangeSummary>,
pub assets: Vec<AssetRecord>,
pub stats_before: DocumentStats,
pub stats_after: DocumentStats,
/// Integrity warnings of the curated document (errors refuse).
pub issues: Vec<SheetIssue>,
pub sheet_id: String,
/// The frozen cleaned drawing, `draft/assets/horse.svg`, the review drawings' base.
pub cleaned_svg: Vec<u8>,
}
/// Manifest entry of the cleaned drawing; id derived from its sha256 (`cleanedAsset`).
pub fn cleaned_asset(bytes: &[u8]) -> AssetRecord {
let sha256 = sha256_hex(bytes);
AssetRecord {
file: CLEANED_ASSET_FILE.to_owned(),
svg_asset_id: asset_id_from_sha256(&sha256),
sha256,
byte_size: bytes.len(),
}
}
/// SVG viewBox attribute ("minX minY width height") → document view_box (`parseViewBox`).
pub fn parse_view_box(raw: Option<&str>) -> Result<ViewBox, PipelineError> {
let numbers: Vec<f64> = js_split_list(raw.unwrap_or_default())
.into_iter()
.map(js_number_of)
.collect();
if numbers.len() != 4 || numbers.iter().any(|n| !n.is_finite()) {
return Err(fail(format!("invalid viewBox {:?}", raw.unwrap_or("null"))));
}
Ok(ViewBox {
min_x: numbers[0],
min_y: numbers[1],
width: numbers[2],
height: numbers[3],
})
}
fn set(target: String, field: &str, value: Value) -> Op {
Op::Set(SetOp {
target,
field: field.to_owned(),
value,
scope: None,
})
}
fn asset_change(asset: &AssetRecord, head_view_box: &ViewBox) -> Result<Change, PipelineError> {
let view_box = serde_json::to_value(head_view_box)
.map_err(|error| fail(format!("head view box: {error}")))?;
Ok(Change {
id: ASSET_CHANGE_ID.to_owned(),
level: ChangeLevel::A,
kind: ChangeKind::Technical,
title: ChangeTitle {
fr: "Un seul dessin, vue tête = recadrage".to_owned(),
en: "Single drawing, head view = crop".to_owned(),
},
rationale: "Les deux fichiers historiques portent le même dessin (tracés identiques) et \
les marqueurs sont désormais des données de la fiche : un seul dessin \
nettoyé, la vue tête en est un recadrage."
.to_owned(),
reference_ids: Vec::new(),
question: None,
priority: ChangePriority::Medium,
ops: vec![
set(
format!("view:{MAIN_VIEW_ID}"),
"svg_asset_id",
Value::from(asset.svg_asset_id.as_str()),
),
set(format!("view:{HEAD_VIEW_ID}"), "svg_asset_id", Value::Null),
set(format!("view:{HEAD_VIEW_ID}"), "view_box", view_box),
],
})
}
fn assert_not_reserved(changes: &[Change]) -> Result<(), PipelineError> {
if changes.iter().any(|change| change.id == ASSET_CHANGE_ID) {
return Err(fail(format!(
"{ASSET_CHANGE_ID} is reserved for the asset step"
)));
}
Ok(())
}
pub fn build_equine_draft(input: &DraftInput<'_>) -> Result<DraftResult, PipelineError> {
assert_not_reserved(input.changes)?;
let asset = cleaned_asset(input.cleaned_svg);
let mut changes = vec![asset_change(&asset, &input.head_view_box)?];
changes.extend(input.changes.iter().cloned());
let baseline = &input.import_result.document;
let result = apply_changeset(baseline, input.references, &changes)?;
Ok(DraftResult {
stats_before: document_stats(baseline)?,
stats_after: document_stats(&result.document)?,
issues: curated_issues(&result.document)?,
document: result.document,
baseline: baseline.clone(),
legacy_id_map: input.import_result.legacy_id_map.clone(),
records: result.records,
changes: changes.iter().map(Change::summary).collect(),
assets: vec![asset],
sheet_id: input.import_result.sheet_id.clone(),
cleaned_svg: input.cleaned_svg.to_vec(),
})
}
/// What a report references: the sheet and the exact document bytes (the historical rule,
/// kept until the R5.6 switch).
fn sheet_ref(draft: &DraftResult, document_json: &str) -> SheetRef {
SheetRef {
sheet_id: draft.sheet_id.clone(),
version_id: None,
document_sha256: sha256_hex(document_json.as_bytes()),
}
}
/// The migration map (`report-migration-map.json` value) and its stats.
pub fn draft_migration_map(
draft: &DraftResult,
reference: &SheetRef,
) -> Result<(Value, super::migration_map::MigrationMapStats), PipelineError> {
build_report_migration_map(&MigrationMapInput {
baseline: &draft.baseline,
final_document: &draft.document,
legacy_id_map: &draft.legacy_id_map,
records: &draft.records,
sheet: reference,
main_view_id: MAIN_VIEW_ID,
})
}
/// The views of the curated document, in document order.
fn view_ids(document: &Value) -> Vec<String> {
document
.get("views")
.and_then(Value::as_array)
.into_iter()
.flatten()
.filter_map(|view| view.get("id").and_then(Value::as_str).map(str::to_owned))
.collect()
}
/// The review drawings' bytes, projected before any is rendered: every drawing repeats the
/// whole cleaned drawing, two per view (the equine draft: 2 views, about 0.55 MB). Views added
/// by a changeset multiply it (2,000 views asked for 547 MB, security gate R5.4 round 8).
pub const MAX_REVIEW_DRAWINGS_BYTES: usize = 64 * 1024 * 1024;
/// One review overlay per view (`review/annotations-<view>.svg`), then the application's
/// drawing of each view (`review/app-<view>.svg`).
fn review_drawings(draft: &DraftResult) -> Result<Vec<(String, String)>, PipelineError> {
let views = view_ids(&draft.document);
let projected = draft.cleaned_svg.len().saturating_mul(2 * views.len());
if projected > MAX_REVIEW_DRAWINGS_BYTES {
return Err(fail(format!(
"{} views would write {projected} bytes of review drawings, past the \
{MAX_REVIEW_DRAWINGS_BYTES} bound",
views.len()
)));
}
let drawing = read_cleaned_drawing(&draft.cleaned_svg)?;
let typed: AnatomicalSheetDocument = serde_json::from_value(draft.document.clone())
.map_err(|error| fail(format!("curated document: {error}")))?;
let mut files = Vec::new();
for view_id in &views {
let overlay = render_review_overlay(&OverlayInput {
drawing: &drawing,
before: &draft.baseline,
after: &draft.document,
view_id,
})?;
files.push((
format!("{REVIEW_OVERLAY_DIR}/annotations-{view_id}.svg"),
overlay,
));
}
for view_id in &views {
let app = render_app_review_drawing(&drawing, &typed, view_id)?;
files.push((format!("{REVIEW_OVERLAY_DIR}/app-{view_id}.svg"), app));
}
Ok(files)
}
/// The text artifacts of the draft folder, keyed by file name, in the TypeScript order
/// (`renderDraftArtifacts`); the frozen `assets/horse.svg` is the caller's byte copy.
pub fn render_draft_artifacts(
draft: &DraftResult,
legacy_anomalies: &[Anomaly],
) -> Result<Vec<(String, String)>, PipelineError> {
let document_json = format_json(&draft.document)?;
let reference = sheet_ref(draft, &document_json);
let (map, migration_stats) = draft_migration_map(draft, &reference)?;
let assets = serde_json::to_value(&draft.assets)
.map_err(|error| fail(format!("assets.json: {error}")))?;
let sheet_ref_json = json!({
"sheet_id": reference.sheet_id,
"version_id": reference.version_id,
"document_sha256": reference.document_sha256,
"schema_version": draft.document.get("schema_version"),
"species_id": draft.document.get("species_id"),
});
let changelog = render_changelog(
&draft.records,
&draft.stats_before,
&draft.stats_after,
&migration_stats,
)?;
let review = render_review_report(&ReviewReportInput {
changes: &draft.changes,
records: &draft.records,
document: &draft.document,
stats_before: &draft.stats_before,
legacy_anomalies,
})?;
let seed = render_draft_seed_sql(&DraftSeedParams {
sheet_id: &draft.sheet_id,
assets: &draft.assets,
document_json: &document_json,
change_note: DRAFT_CHANGE_NOTE,
})?;
let mut files = vec![
("document.json".to_owned(), document_json),
("assets.json".to_owned(), format_json(&assets)?),
(SHEET_REF_FILE.to_owned(), format_json(&sheet_ref_json)?),
(MIGRATION_MAP_FILE.to_owned(), format_json(&map)?),
];
files.extend(review_drawings(draft)?);
files.push(("CHANGELOG.md".to_owned(), format_report(&changelog)?));
files.push(("REVIEW-REPORT.md".to_owned(), format_report(&review)?));
files.push((DRAFT_SEED_SQL_FILE.to_owned(), seed));
Ok(files)
}