fix(anatomy): R5.4 Markdown guard from a TypeScript sweep, strict drawing comparison, unwritable characters refused, indexed renderers

This commit is contained in:
bchanot committed 2026-10-23 11:51:41 +02:00
1 parent 0a79c52e62
commit 94f8b35105
14 files changed
+647 -50

No files matched your search

+1 -1
View File
@@ -206,7 +206,7 @@ fn compare_file(
match compare_drawing(produced, committed, rule) {
Ok(_) => {
println!(
"{}: declared: {name} (trees equal under the R3 trigger-name rule)",
"{}: declared: {name} (byte-identical once the R3 trigger-name rule is applied)",
folder.name
);
Ok(())
+1 -1
View File
@@ -33,5 +33,5 @@ pub fn render_app_review_drawing(
let layer = render_layer(document, view_id, &HashSet::new())
.map_err(|_| fail(format!("unknown view \"{view_id}\"")))?;
root.children.push(Node::Element(layer));
Ok(drawing_text(&drawing.prolog, &root))
drawing_text(&drawing.prolog, &root)
}
+32 -5
View File
@@ -4,8 +4,8 @@
//! root element: the XML declaration and its line break), kept verbatim in front of every
//! drawing the pipeline writes, as xmldom's serialiser kept it.
use crate::anatomy::svg::serialize::serialize;
use crate::anatomy::svg::{Element, read_svg};
use crate::anatomy::svg::serialize::{is_xml_text, serialize};
use crate::anatomy::svg::{Element, Node, read_svg};
use crate::curation::PipelineError;
pub struct CleanedDrawing {
@@ -35,7 +35,34 @@ pub fn read_cleaned_drawing(bytes: &[u8]) -> Result<CleanedDrawing, PipelineErro
})
}
/// The file text of a drawing built on the cleaned root.
pub fn drawing_text(prolog: &str, root: &Element) -> String {
format!("{prolog}{}", serialize(root))
/// A value or text the drawing cannot carry as the TypeScript wrote it: a code point XML 1.0
/// refuses, or a carriage return (xmldom wrote it raw in text, a parser reads it back as a line
/// feed; the core writes `&#13;`, which a parser keeps). Walked with an explicit stack.
fn first_unwritable(root: &Element) -> Option<String> {
let bad = |text: &str| !is_xml_text(text) || text.contains('\r');
let mut pending = vec![root];
while let Some(element) = pending.pop() {
if let Some(attr) = element.attrs.iter().find(|a| bad(&a.value)) {
return Some(attr.value.clone());
}
for child in &element.children {
match child {
Node::Element(child) => pending.push(child),
Node::Text(text) if bad(text) => return Some(text.clone()),
Node::Text(_) => {}
}
}
}
None
}
/// The file text of a drawing built on the cleaned root; refused when a label, an id or a path
/// holds what the drawing cannot carry (`first_unwritable`).
pub fn drawing_text(prolog: &str, root: &Element) -> Result<String, PipelineError> {
if let Some(text) = first_unwritable(root) {
return Err(PipelineError::Guard(format!(
"a review drawing cannot carry {text:?}"
)));
}
Ok(format!("{prolog}{}", serialize(root)))
}
+23 -13
View File
@@ -3,6 +3,8 @@
//! as an SVG layer, and the baseline position of every moved or removed annotation beside it
//! (dashed). Pure data, deterministic, opens in any browser.
use std::collections::{HashMap, HashSet};
use serde_json::Value;
use super::drawing::{CleanedDrawing, drawing_text};
@@ -84,17 +86,28 @@ fn with_geometry<'a>(doc: &'a Value, view_id: &str) -> impl Iterator<Item = &'a
}
/// `new Map(entries)`: the first position of an id, its last value.
fn previous_geometries<'a>(before: &'a Value, view_id: &str) -> Vec<(String, &'a Value)> {
let mut map: Vec<(String, &Value)> = Vec::new();
struct Previous<'a> {
entries: Vec<(String, &'a Value)>,
index: HashMap<String, usize>,
}
fn previous_geometries<'a>(before: &'a Value, view_id: &str) -> Previous<'a> {
let mut previous = Previous {
entries: Vec::new(),
index: HashMap::new(),
};
for annotation in with_geometry(before, view_id) {
let id = js_string(annotation.get("id"));
let geometry = &annotation["geometry"];
match map.iter_mut().find(|(known, _)| *known == id) {
Some(entry) => entry.1 = geometry,
None => map.push((id, geometry)),
match previous.index.get(&id) {
Some(&at) => previous.entries[at].1 = geometry,
None => {
previous.index.insert(id.clone(), previous.entries.len());
previous.entries.push((id, geometry));
}
}
}
map
previous
}
/// `overlayMarkers`: curated annotations with their status, then the baseline shapes they left.
@@ -104,10 +117,7 @@ fn overlay_markers<'a>(before: &'a Value, after: &'a Value, view_id: &str) -> Ve
for annotation in with_geometry(after, view_id) {
let id = js_string(annotation.get("id"));
let geometry = &annotation["geometry"];
let old = previous
.iter()
.find(|(known, _)| *known == id)
.map(|(_, g)| *g);
let old = previous.index.get(&id).map(|&at| previous.entries[at].1);
let status = match old {
None => Status::Added,
Some(old) if js_deep_equal(old, geometry) => Status::Unchanged,
@@ -126,11 +136,11 @@ fn overlay_markers<'a>(before: &'a Value, after: &'a Value, view_id: &str) -> Ve
});
}
}
let kept: Vec<String> = annotations(after)
let kept: HashSet<String> = annotations(after)
.iter()
.map(|a| js_string(a.get("id")))
.collect();
for (id, geometry) in previous {
for (id, geometry) in previous.entries {
if !kept.contains(&id) {
markers.push(Marker {
id,
@@ -361,5 +371,5 @@ pub fn render_review_overlay(input: &OverlayInput<'_>) -> Result<String, Pipelin
}
legend(&mut layer, (at(0) + font_size, at(1)), font_size);
root.children.push(Node::Element(layer));
Ok(drawing_text(&input.drawing.prolog, &root))
drawing_text(&input.drawing.prolog, &root)
}
+78 -20
View File
@@ -5,7 +5,7 @@
//! alignment.
use std::cmp::Ordering;
use std::collections::HashSet;
use std::collections::{HashMap, HashSet};
use serde_json::Value;
@@ -44,13 +44,28 @@ fn codes(items: &[String]) -> String {
.join(", ")
}
fn subjects(input: &ReviewReportInput<'_>, change: &ChangeSummary) -> String {
let own = input
.records
.iter()
.filter(|record| record.change.id == change.id)
.map(record_subject);
codes(&distinct(own))
fn subjects(subjects: &HashMap<&str, Vec<String>>, change: &ChangeSummary) -> String {
codes(
subjects
.get(change.id.as_str())
.map_or(&[][..], Vec::as_slice),
)
}
/// The distinct subjects of each change's records, grouped in one pass.
fn subjects_by_change(records: &[ChangeRecord]) -> HashMap<&str, Vec<String>> {
let mut grouped: HashMap<&str, Vec<String>> = HashMap::new();
let mut seen: HashSet<(&str, String)> = HashSet::new();
for record in records {
let subject = record_subject(record);
if seen.insert((record.change.id.as_str(), subject.clone())) {
grouped
.entry(record.change.id.as_str())
.or_default()
.push(subject);
}
}
grouped
}
fn references(ids: &[String]) -> String {
@@ -61,7 +76,7 @@ fn references(ids: &[String]) -> String {
}
}
fn change_line(input: &ReviewReportInput<'_>, change: &ChangeSummary) -> String {
fn change_line(grouped: &HashMap<&str, Vec<String>>, change: &ChangeSummary) -> String {
let confirm = if change.level.needs_confirmation() {
"oui"
} else {
@@ -79,17 +94,18 @@ fn change_line(input: &ReviewReportInput<'_>, change: &ChangeSummary) -> String
level_letter(change.level),
kind_label(change.kind),
change.rationale,
subjects(input, change),
subjects(grouped, change),
references(&change.reference_ids),
)
}
fn change_list(input: &ReviewReportInput<'_>, keep: impl Fn(&ChangeSummary) -> bool) -> String {
let grouped = subjects_by_change(input.records);
let lines: Vec<String> = input
.changes
.iter()
.filter(|change| keep(change))
.map(|change| change_line(input, change))
.map(|change| change_line(&grouped, change))
.collect();
if lines.is_empty() {
NONE.to_owned()
@@ -220,17 +236,24 @@ fn clinical_vigilance(input: &ReviewReportInput<'_>) -> Result<String, PipelineE
.join("\n"))
}
fn citation_count(document: &Value, id: &str) -> Result<usize, PipelineError> {
let wanted = Value::from(id);
let mut count = 0;
/// Citing findings per reference id, counted in one walk: a finding citing an id twice
/// counts once, as `ids.includes(id)` did.
fn citation_counts(document: &Value) -> Result<HashMap<String, usize>, PipelineError> {
let mut counts: HashMap<String, usize> = HashMap::new();
for entry in all_findings(document)? {
let cited = at(document, &entry.path)?
let ids = at(document, &entry.path)?
.get("reference_ids")
.and_then(Value::as_array)
.is_some_and(|ids| ids.contains(&wanted));
count += usize::from(cited);
.and_then(Value::as_array);
let distinct: HashSet<&str> = ids
.into_iter()
.flatten()
.filter_map(Value::as_str)
.collect();
for id in distinct {
*counts.entry(id.to_owned()).or_default() += 1;
}
}
Ok(count)
Ok(counts)
}
/// `Boolean(value)` of a reference field.
@@ -247,6 +270,7 @@ fn bibliography(document: &Value) -> Result<String, PipelineError> {
.get("references")
.and_then(Value::as_array)
.map_or(&[][..], Vec::as_slice);
let counts = citation_counts(document)?;
let mut lines = Vec::new();
for (index, reference) in references.iter().enumerate() {
let links: Vec<String> = [
@@ -267,7 +291,7 @@ fn bibliography(document: &Value) -> Result<String, PipelineError> {
index + 1,
code(Some(&id)),
js_string(reference.get("citation")),
citation_count(document, &id)?
counts.get(&id).copied().unwrap_or(0)
));
}
Ok(if lines.is_empty() {
@@ -330,8 +354,42 @@ fn intro() -> String {
.join("\n")
}
/// A control character (a line break, a tab, a carriage return…) in an authored text the
/// report writes in a list item would break the item, and Prettier re-indents what follows:
/// refused (the TypeScript wrote it raw).
fn assert_single_line(text: &str) -> Result<(), PipelineError> {
if text.chars().any(char::is_control) {
return Err(PipelineError::UnsupportedMarkdown(text.to_owned()));
}
Ok(())
}
fn assert_list_items_single_line(input: &ReviewReportInput<'_>) -> Result<(), PipelineError> {
for change in input.changes {
assert_single_line(&change.title.fr)?;
assert_single_line(&change.rationale)?;
if let Some(question) = &change.question {
assert_single_line(question)?;
}
}
let references = input.document.get("references").and_then(Value::as_array);
for reference in references.into_iter().flatten() {
for key in ["citation", "url", "doi"] {
assert_single_line(&js_string(reference.get(key)))?;
}
}
for record in input.records {
if record.field.as_deref() == Some("requires_vet_referral") {
assert_single_line(&compact_value(&record.before)?)?;
assert_single_line(&compact_value(&record.after)?)?;
}
}
Ok(())
}
/// `renderReviewReport`: the raw text, before the table alignment.
pub fn render_review_report(input: &ReviewReportInput<'_>) -> Result<String, PipelineError> {
assert_list_items_single_line(input)?;
let is = |level: ChangeLevel| move |c: &ChangeSummary| c.level == level;
let sections: Vec<(&str, String)> = vec![
("1. Qualité des données initiales", initial_quality(input)),
+30 -8
View File
@@ -1,11 +1,13 @@
//! How a produced review drawing is compared with the committed xmldom file: byte for byte,
//! else as parsed trees after a declared rule. One rule exists: the R3 owner decision
//! else byte for byte after a declared rule applied to the committed file's tree (strict: the
//! committed file must rebuild to itself from its tree, so nothing outside the tree can hide). One rule exists: the R3 owner decision
//! (2026-10-15) that a trigger without a name of its own carries the opened view's name in
//! each language, where the TypeScript renderer wrote the French name twice. It is applied to
//! the committed tree (the frozen file stays as it is): on an element that opens a view, a
//! `data-anno-name-en` equal to the view's French name takes its English name, and a title
//! `FR / FR` under it reads `FR / EN`. Any other difference is reported.
use crate::anatomy::svg::serialize::serialize;
use crate::anatomy::svg::tree::parse_svg;
use crate::anatomy::svg::{Element, Node};
use crate::domain::sheet::AnatomicalSheetDocument;
@@ -101,7 +103,18 @@ fn first_difference(a: &Element, b: &Element, path: &str) -> Option<String> {
})
}
/// Compares a produced drawing with the committed one: bytes, else trees after `rule`.
/// The prolog (the bytes before the root element) and the text after it.
fn outside_root(text: &str) -> Result<(&str, &str), String> {
let document = roxmltree::Document::parse(text).map_err(|error| error.to_string())?;
let range = document.root_element().range();
Ok((&text[..range.start], &text[range.end..]))
}
/// Compares a produced drawing with the committed one: byte identity, else the committed file
/// rebuilt from its tree must be byte-identical to itself (so the tree carries all of it: no
/// comment, processing instruction, encoding or entity form the comparison could miss), the
/// rule must change that tree, and the rebuilt text after the rule must be byte-identical to
/// the produced one.
pub fn compare_drawing(
produced: &str,
committed: &str,
@@ -113,11 +126,20 @@ pub fn compare_drawing(
let Some(rule) = rule else {
return Err("bytes differ and no tree rule is declared".to_owned());
};
let produced = parse_svg(produced).map_err(|e| format!("produced: {}", e.reason()))?;
let mut committed = parse_svg(committed).map_err(|e| format!("committed: {}", e.reason()))?;
rule.apply(&mut committed);
match first_difference(&produced, &committed, "") {
None => Ok(Match::TreeAfterRule),
Some(difference) => Err(difference),
let (prolog, epilog) = outside_root(committed).map_err(|e| format!("committed: {e}"))?;
let tree = parse_svg(committed).map_err(|e| format!("committed: {}", e.reason()))?;
if format!("{prolog}{}{epilog}", serialize(&tree)) != committed {
return Err("the committed file holds what its tree does not carry".to_owned());
}
let mut ruled = tree.clone();
rule.apply(&mut ruled);
if ruled == tree {
return Err("bytes differ and the declared rule changes nothing".to_owned());
}
if format!("{prolog}{}{epilog}", serialize(&ruled)) == produced {
return Ok(Match::TreeAfterRule);
}
let produced_tree = parse_svg(produced).map_err(|e| format!("produced: {}", e.reason()))?;
Err(first_difference(&produced_tree, &ruled, "")
.unwrap_or_else(|| "the texts differ outside the trees".to_owned()))
}
+99 -1
View File
@@ -67,11 +67,105 @@ pub fn code(value: Option<&str>) -> String {
value.map_or_else(|| "-".to_owned(), |text| format!("`{text}`"))
}
/// The plain text of a raw line Prettier would rewrite, found in its parts outside code spans
/// (`oracles/r5/markdown-sweep.json`): `*` outside a `**strong**` span; an underscore not between two letters or digits; a
/// backslash before a space or at the end, or an authored one before the cell escape `\|`
/// (`\"` in a JSON value is kept as written); two spaces in a row, a tab, a carriage return.
/// The parts of plain text outside the `**strong**` spans Prettier keeps as written (the
/// opening and closing pairs bounded by a non-word character or the line's edge, the inside
/// non-empty, without `*` and without an outer space). `None` when a `*` is left outside
/// such a span.
fn outside_strong(plain: &str) -> Option<String> {
let mut out = String::new();
let mut rest = plain;
let edge = |c: Option<char>| c.is_none_or(|c| !c.is_alphanumeric());
while let Some(open) = rest.find("**") {
let inside = &rest[open + 2..];
let close = inside.find("**")?;
let content = &inside[..close];
let after = inside[close + 2..].chars().next();
let valid = !content.is_empty()
&& !content.contains('*')
&& !content.starts_with(' ')
&& !content.ends_with(' ')
&& edge(rest[..open].chars().next_back())
&& edge(after);
if !valid {
return None;
}
out.push_str(&rest[..open]);
out.push_str(content);
rest = &inside[close + 2..];
}
out.push_str(rest);
(!out.contains('*')).then_some(out)
}
fn plain_rewrite(plain: &str) -> bool {
let Some(plain) = outside_strong(plain) else {
return true;
};
let chars: Vec<char> = plain.chars().collect();
let word = |i: Option<usize>| {
i.and_then(|i| chars.get(i))
.is_some_and(|c| c.is_alphanumeric())
};
chars.iter().enumerate().any(|(i, &c)| match c {
'*' | '\t' | '\r' => true,
'_' => !(word(i.checked_sub(1)) && word(Some(i + 1))),
'\\' => match chars.get(i + 1) {
None | Some(' ') => true,
Some('|') => i.checked_sub(1).and_then(|p| chars.get(p)) == Some(&'\\'),
Some(_) => false,
},
' ' => chars.get(i + 1) == Some(&' '),
_ => false,
})
}
/// Refuses a raw line holding Markdown the port does not reproduce, cell by cell in a table
/// row: an odd number of backticks, an empty code span or one with an outer space, or plain
/// text Prettier would rewrite (`plain_rewrite`). The report is then not written at all.
fn guard_line(line: &str) -> Result<(), PipelineError> {
if line.starts_with('|') {
// A table row: each cell as GFM reads it (the padding around the pipes is layout).
for cell in split_row(line) {
guard_text(&cell).map_err(|_| PipelineError::UnsupportedMarkdown(line.to_owned()))?;
}
return Ok(());
}
guard_text(line)
}
fn guard_text(line: &str) -> Result<(), PipelineError> {
let parts: Vec<&str> = line.split('`').collect();
let refuse = || Err(PipelineError::UnsupportedMarkdown(line.to_owned()));
if parts.len().is_multiple_of(2) {
return refuse();
}
for (index, part) in parts.iter().enumerate() {
let code = index % 2 == 1;
let bad = if code {
part.is_empty() || part.starts_with(' ') || part.ends_with(' ')
} else {
plain_rewrite(part)
};
if bad {
return refuse();
}
}
Ok(())
}
/// 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.
/// width), every other line kept. A text with Markdown Prettier would rewrite otherwise is
/// refused (`guard_line`).
pub fn format_markdown(raw: &str) -> Result<String, PipelineError> {
let lines: Vec<&str> = raw.split('\n').collect();
for line in &lines {
guard_line(line)?;
}
let mut out: Vec<String> = Vec::with_capacity(lines.len());
let mut index = 0;
while index < lines.len() {
@@ -132,6 +226,10 @@ fn split_row(line: &str) -> Vec<String> {
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);
// A row with another cell count would lose or invent a cell where Prettier keeps it.
if let Some(index) = rows.iter().position(|row| row.len() != columns) {
return Err(PipelineError::UnsupportedMarkdown(lines[index].to_owned()));
}
let mut widths = vec![3; columns];
let content = rows.iter().enumerate().filter(|&(index, _)| index != 1);
for (_, row) in content {
+7
View File
@@ -28,6 +28,10 @@ pub enum PipelineError {
/// A text holds a character whose Prettier display width is not reproduced (emoji
/// sequences): the layout would differ, so nothing is written.
UnsupportedWidth(String),
/// A report text holds Markdown Prettier would rewrite (emphasis, escapes, whitespace
/// runs, line breaks in a list item…), which the port does not reproduce: nothing is
/// written rather than a report that differs silently.
UnsupportedMarkdown(String),
}
impl std::fmt::Display for PipelineError {
@@ -37,6 +41,9 @@ impl std::fmt::Display for PipelineError {
Self::UnsupportedWidth(text) => {
write!(f, "display width not reproduced for {text:?} (emoji)")
}
Self::UnsupportedMarkdown(text) => {
write!(f, "Markdown formatting not reproduced for {text:?}")
}
}
}
}