feat(domain): R4.7 report-compare on the core: pdf::compare (feature cli, pure lines, runner over a Files trait), report-compare binary on the seed sheet, pnpm report:compare switched to it; O10 230 rows (158 exact, 72 declared A.3 closed lines), 11 cases as ledger targets, binary spawned; rust-check on cli

This commit is contained in:
bchanot committed 2026-10-19 11:13:35 +02:00
1 parent 264c5fa3fc
commit 02ebf05195
17 files changed
+1598 -24

No files matched your search

+423
View File
@@ -0,0 +1,423 @@
//! The lines of a comparison (`compare/report.ts`): one zone or finding per line, the sheet's
//! current label next to the archived one, `!! UNRESOLVED` with the reason where the sheet
//! cannot place an item. Every untrusted string goes through [`esc`].
use serde::Deserialize;
use serde_json::Value;
use super::{CompareOptions, Counts, Locale, Source, UNRESOLVED};
use crate::domain::archive::{ArchiveReadOutcome, SheetStatus};
use crate::domain::report::v2::{FindingV2, ObservationV2, ObservationZoneV2, ReportV2};
use crate::domain::report::{
Finding, LateralSide, NextAppointment, Observation, Report, ReportZone, finding_path_key,
};
use crate::domain::sheet::{LoadedSheet, LocalizedText};
use crate::json::{js_number, to_js_compact};
use crate::pdf::write::text::is_control_or_format;
/// Escapes Unicode control (C0, DEL, C1) and format characters (bidi overrides, zero-width)
/// plus the line and paragraph separators as `\uXXXX` (newline and carriage return included):
/// a PDF is untrusted, and a raw escape sequence printed to a terminal can rewrite the screen
/// or fake a line. The class is V8's `[\p{Cc}\p{Cf}

]` (`esc` of `report.ts`).
pub fn esc(text: &str) -> String {
let mut out = String::with_capacity(text.len());
for c in text.chars() {
if is_control_or_format(c) {
out.push_str(&format!("\\u{:04x}", u32::from(c)));
} else {
out.push(c);
}
}
out
}
fn q(text: &str) -> String {
format!("\"{}\"", esc(text))
}
/// `pickLabel`: the locale's label, the other one when it is empty.
fn pick_label(text: &LocalizedText, locale: Locale) -> &str {
let (own, other) = match locale {
Locale::En => (&text.en, &text.fr),
Locale::Fr => (&text.fr, &text.en),
};
if own.is_empty() { other } else { own }
}
fn side_suffix(side: Option<LateralSide>) -> String {
side.map_or_else(String::new, |s| format!(" {}", s.as_str()))
}
fn zone_ref(zone: &ReportZone) -> String {
let text = format!("{}@{}", zone.annotation_id, zone.view_id);
esc(&format!("{text}{}", side_suffix(zone.side)))
}
fn finding_ref(finding: &Finding) -> String {
esc(&finding_path_key(&finding.finding_zone_id, &finding.path))
}
/// ` [C3, C4]` for a zone with vertebrae, nothing otherwise (an empty list included).
fn vertebrae_of<'a>(vertebrae: impl Iterator<Item = (&'a str, f64)>) -> String {
let listed: Vec<String> = vertebrae
.map(|(region, index)| esc(&format!("{region}{}", js_number(index))))
.collect();
if listed.is_empty() {
String::new()
} else {
format!(" [{}]", listed.join(", "))
}
}
fn v3_vertebrae(zone: &ReportZone) -> String {
vertebrae_of(
zone.vertebrae
.iter()
.flatten()
.map(|v| (v.region.as_str(), v.index.get())),
)
}
fn v2_vertebrae(zone: &ObservationZoneV2) -> String {
vertebrae_of(
zone.vertebrae
.iter()
.flatten()
.map(|v| (v.region.as_str(), v.index.get())),
)
}
/// Free-text lines: the texts with `full`, else only how many there are.
fn text_lines(texts: &[String], full: bool) -> Vec<String> {
if texts.is_empty() {
return Vec::new();
}
if !full {
return vec![format!("text ({} entries)", texts.len())];
}
texts.iter().map(|t| format!("text {}", q(t))).collect()
}
/// A single free-text field: its content with `full`, else a placeholder.
fn field_line(label: &str, value: &str, full: bool) -> Vec<String> {
if value.is_empty() {
return Vec::new();
}
let line = if full {
format!("{label} {}", q(value))
} else {
format!("{label} (present)")
};
vec![line]
}
/// A v3 zone against the sheet: current label, or why it cannot be shown.
fn describe_zone(zone: &ReportZone, sheet: &LoadedSheet, locale: Locale) -> String {
let reference = format!("{}{}", zone_ref(zone), v3_vertebrae(zone));
let snapshot = q(zone.label_fr_snapshot.as_deref().unwrap_or(""));
if zone.unresolved.is_some() {
return format!("{reference} {snapshot} {UNRESOLVED} legacy zone kept from the archive");
}
let indexed = sheet.indexed();
match indexed.annotation(&zone.annotation_id) {
Some(anno) if anno.view_id() == zone.view_id => {
let label = q(pick_label(&indexed.name(anno), locale));
format!("{reference} {label}")
}
_ => format!("{reference} {snapshot} !! not in the sheet (unflagged)"),
}
}
fn snapshot_label(finding: &Finding, locale: Locale) -> String {
q(match locale {
Locale::En => &finding.label_en_snapshot,
Locale::Fr => &finding.label_fr_snapshot,
})
}
/// A v3 finding against the sheet: current label, or why it cannot be shown.
fn describe_finding(finding: &Finding, sheet: &LoadedSheet, locale: Locale) -> String {
let snapshot = snapshot_label(finding, locale);
let vet = if finding.requires_vet_referral == Some(true) {
" (!)"
} else {
""
};
let reference = format!("{}{}{vet}", finding_ref(finding), side_suffix(finding.side));
if finding.unresolved.is_some() {
return format!("{reference} {snapshot} {UNRESOLVED} kept with its snapshot");
}
let Some(node) = sheet
.indexed()
.node_at(&finding.finding_zone_id, &finding.path)
else {
return format!("{reference} {snapshot} !! not in the sheet (unflagged)");
};
if !node.children().is_empty() {
return format!("{reference} {snapshot} !! points at a category, not a leaf");
}
format!("{reference} {}", q(pick_label(node.name(), locale)))
}
/// Legacy zone → migrated zone.
fn describe_zone_migration(
legacy: &ObservationZoneV2,
out: &ReportZone,
sheet: &LoadedSheet,
locale: Locale,
) -> String {
// JavaScript truthiness: an empty detail key is no detail.
let detail = legacy
.detail_source_id
.as_deref()
.filter(|key| !key.is_empty())
.map_or_else(String::new, |key| format!("@{key}"));
let from = format!(
"{}{}",
esc(&format!(
"{}{detail}{}",
legacy.bone_id,
side_suffix(legacy.side)
)),
v2_vertebrae(legacy)
);
if out.unresolved.is_some() {
return format!(
"{from} {UNRESOLVED} unknown legacy id, kept as {}",
zone_ref(out)
);
}
let indexed = sheet.indexed();
let label = indexed.annotation(&out.annotation_id).map_or_else(
|| "!! missing".to_owned(),
|anno| q(pick_label(&indexed.name(anno), locale)),
);
let snapshot = q(out.label_fr_snapshot.as_deref().unwrap_or(""));
format!("{from} {snapshot} → {} {label}", zone_ref(out))
}
/// Legacy finding → migrated finding, with the fate the map decided.
fn describe_finding_migration(
legacy: &FindingV2,
out: &Finding,
sheet: &LoadedSheet,
locale: Locale,
) -> String {
let vet = if legacy.requires_vet_referral == Some(true) {
" (!)"
} else {
""
};
let legacy_key = finding_path_key(&legacy.zone_id, &legacy.path);
let key = format!("{}{vet}", esc(&legacy_key));
let snapshot = q(match locale {
Locale::En => &legacy.label_en_snapshot,
Locale::Fr => &legacy.label_fr_snapshot,
});
if out.unresolved.is_some() {
let why = if sheet.migration_map.finding_paths.contains_key(&legacy_key) {
"removed from the sheet, kept with its label"
} else {
"never in the legacy catalog, kept with its label"
};
return format!("{key} {snapshot} {UNRESOLVED} {why}");
}
let label = sheet
.indexed()
.node_at(&out.finding_zone_id, &out.path)
.map_or_else(
|| "!! missing".to_owned(),
|node| q(pick_label(node.name(), locale)),
);
let target = if finding_ref(out) == esc(&legacy_key) {
"same".to_owned()
} else {
finding_ref(out)
};
format!("{key} {snapshot} → {target} {label}")
}
pub(super) fn observation_lines(
obs: &Observation,
sheet: &LoadedSheet,
opts: CompareOptions,
) -> Vec<String> {
let severity = obs.severity.map_or("none", |s| s.as_str());
let mut lines = vec![format!("[{}] severity {}", esc(&obs.id), esc(severity))];
for zone in &obs.zones {
lines.push(format!(
"zone {}",
describe_zone(zone, sheet, opts.locale)
));
}
for finding in &obs.findings {
lines.push(format!(
"finding {}",
describe_finding(finding, sheet, opts.locale)
));
}
lines.extend(text_lines(&obs.free_text_findings, opts.full));
lines.extend(field_line("comment ", &obs.comment, opts.full));
lines
}
/// The report document inside the attachment: the envelope's, or the bare one.
fn report_data_of(data: &Value) -> &Value {
let is_envelope = data.get("envelopeVersion").is_some_and(Value::is_number);
if is_envelope {
data.get("report").unwrap_or(&Value::Null)
} else {
data
}
}
fn migrated_observation(
legacy: &ObservationV2,
out: &Observation,
sheet: &LoadedSheet,
opts: CompareOptions,
) -> Vec<String> {
let severity = match (legacy.severity, out.severity) {
(Some(from), Some(to)) => esc(&format!("{} → {}", from.as_str(), to.as_str())),
_ => "none".to_owned(),
};
let mut lines = vec![format!("[{}] severity {severity}", esc(&legacy.id))];
for (zone, migrated) in legacy.zones.iter().zip(&out.zones) {
let line = describe_zone_migration(zone, migrated, sheet, opts.locale);
lines.push(format!("zone {line}"));
}
for (finding, migrated) in legacy.findings.iter().zip(&out.findings) {
let line = describe_finding_migration(finding, migrated, sheet, opts.locale);
lines.push(format!("finding {line}"));
}
lines.extend(text_lines(&legacy.free_text_findings, opts.full));
lines.extend(field_line("comment ", &legacy.comment, opts.full));
lines
}
/// The legacy observations next to their migration. The v2 document parsed again from the
/// attachment: the layered reader already accepted it, so the parse cannot fail; were it to,
/// the v3 lines stand in.
pub(super) fn migrated_observation_lines(
data: &Value,
report: &Report,
sheet: &LoadedSheet,
opts: CompareOptions,
) -> Vec<Vec<String>> {
let Ok(v2) = ReportV2::deserialize(report_data_of(data)) else {
return report
.observations
.iter()
.map(|o| observation_lines(o, sheet, opts))
.collect();
};
v2.observations
.iter()
.zip(&report.observations)
.map(|(legacy, out)| migrated_observation(legacy, out, sheet, opts))
.collect()
}
pub(super) fn counts_of(report: &Report) -> Counts {
let zones = || report.observations.iter().flat_map(|o| &o.zones);
let findings = || report.observations.iter().flat_map(|o| &o.findings);
Counts {
observations: report.observations.len(),
zones: zones().count(),
unresolved_zones: zones().filter(|z| z.unresolved.is_some()).count(),
findings: findings().count(),
unresolved_findings: findings().filter(|f| f.unresolved.is_some()).count(),
}
}
/// The envelope line: present only when the attachment was an archive.
fn archive_line(outcome: &ArchiveReadOutcome) -> Vec<String> {
if outcome.archive.is_some() {
return vec!["archive v1 (snapshot kept)".to_owned()];
}
match outcome.snapshot_rejected {
Some(reason) => vec![format!(
"archive v1 (snapshot rejected: {})",
esc(reason.as_str())
)],
None => Vec::new(),
}
}
fn sheet_line(report: &Report, sheet: &LoadedSheet) -> String {
let reference = &sheet.reference;
let same_sheet = report.sheet.sheet_id == reference.sheet_id
&& report.sheet.document_sha256 == reference.document_sha256;
if same_sheet {
let status = match sheet.status {
SheetStatus::Draft => "draft",
SheetStatus::Published => "published",
};
return format!(
"sheet {} ({status}, matches the bundled sheet)",
esc(&reference.sheet_id)
);
}
let sha: String = report.sheet.document_sha256.chars().take(12).collect();
format!(
"sheet {} !! written on another sheet document (sha {}…)",
esc(&report.sheet.sheet_id),
esc(&sha)
)
}
pub(super) fn header_lines(
source: Source,
outcome: &ArchiveReadOutcome,
sheet: &LoadedSheet,
opts: CompareOptions,
) -> Vec<String> {
let report = &outcome.report;
let c = counts_of(report);
let mut lines = vec![match source {
Source::V2 => "v2 → v3 (migrated)".to_owned(),
Source::V3 => "v3 (read as is)".to_owned(),
}];
lines.extend(archive_line(outcome));
lines.push(sheet_line(report, sheet));
lines.push(format!(
"id {} · date {} · {} observations · {} zones ({} unresolved) · {} findings ({} unresolved)",
esc(&report.id),
esc(&report.date),
c.observations,
c.zones,
c.unresolved_zones,
c.findings,
c.unresolved_findings
));
if opts.full {
lines.push(format!(
"practitioner {} · animal {} · owner {}",
q(&report.practitioner.name),
q(&report.animal.name),
q(&report.owner.name)
));
}
lines
}
fn appointment_line(appointment: &NextAppointment, full: bool) -> String {
if full {
let json = to_js_compact(appointment).unwrap_or_default();
return format!("next appointment {}", esc(&json));
}
let kind = match appointment {
NextAppointment::Date(_) => "date",
NextAppointment::Period(_) => "period",
};
format!("next appointment ({kind})")
}
pub(super) fn footer_lines(report: &Report, full: bool) -> Vec<String> {
let mut lines = field_line("conclusion", &report.conclusion, full);
lines.extend(field_line("recommendations", &report.recommendations, full));
if let Some(appointment) = &report.next_appointment {
lines.push(appointment_line(appointment, full));
}
lines
}
+177
View File
@@ -0,0 +1,177 @@
//! R4.7: the archive check tool (`apps/app/scripts/compare-reports.ts` and `compare/report.ts`,
//! plan §B.1.7), pure. A PDF's attachment is read by the R4 reader, parsed as the session
//! parses it, read through the layered archive reader, then printed next to its v3 reading on
//! the reference sheet: a v2 archive line by line legacy id → canonical id, a v3 report
//! checked against the sheet, every item the sheet cannot place marked `!! UNRESOLVED` with
//! its reason. Nothing is summarised away. The lines, the summary, the `--diff` and the
//! argument parsing are the TypeScript's, pinned by the frozen O10 oracle
//! (`tests/oracles/r4/compare/compare.json`): 230 invocations, of which 72 declared in plan
//! §A.3 (a ZodError dump, a lone surrogate kept, a version read as `Infinity`) print the
//! closed wording of [`CompareFailure::reason`] instead.
//!
//! No I/O here: the binary (`src/bin/report_compare.rs`) owns the file system through the
//! [`run::Files`] trait, and so do the tests.
pub mod lines;
pub mod run;
use serde_json::Value;
use crate::domain::archive::ArchiveReadOutcome;
use crate::domain::archive::read::read_archive_document;
use crate::domain::js::{JsonReadError, parse_json};
use crate::domain::report::ReadFailure;
use crate::domain::sheet::LoadedSheet;
use crate::json::js_number;
use crate::pdf::read::ReadKind;
use crate::pdf::read::probe::{ProbeLimits, read_attached_document};
pub use run::{CliArgs, FileFailure, Files, Run, parse_args, run};
/// The marker of an item the sheet cannot place.
pub const UNRESOLVED: &str = "!! UNRESOLVED";
/// The two locales of the sheet labels (`--locale fr|en`).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Locale {
Fr,
En,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct CompareOptions {
pub locale: Locale,
/// Print names and the real free texts (off by default: placeholders).
pub full: bool,
}
/// `v2` when the attachment was migrated, `v3` when read as is.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Source {
V2,
V3,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct Counts {
pub observations: usize,
pub zones: usize,
pub unresolved_zones: usize,
pub findings: usize,
pub unresolved_findings: usize,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Comparison {
pub source: Source,
pub header: Vec<String>,
pub observations: Vec<Vec<String>>,
pub footer: Vec<String>,
/// Lines in the v3 vocabulary only, for `--diff` between two PDFs.
pub normalized: Vec<String>,
pub counts: Counts,
}
/// Why one PDF gives no comparison, worded without echoing the file (`READ_FAILURES` and
/// `failureReason` of the TypeScript CLI; the closed lines of plan §A.3 for what the
/// TypeScript printed as a ZodError, read as `Infinity` or kept as a lone surrogate).
#[derive(Debug)]
pub enum CompareFailure {
Read(ReadKind),
/// The attachment text the probe accepted does not read as JSON here: a lone surrogate
/// escape, refused unchanged (A.3), or a syntax the probe tolerated.
Json(JsonReadError),
/// The `version` is not 2 or 3: the number found, `None` for none (an overflowing number
/// reads as `null`: A.3).
Version(Option<f64>),
/// A document of a known version that fails its schema (the TypeScript printed the
/// ZodError issues: A.3).
Invalid(String),
}
impl CompareFailure {
/// The reason printed after `!! <name>: `.
pub fn reason(&self) -> String {
match self {
Self::Read(ReadKind::InvalidPdf) => "not a readable PDF".to_owned(),
Self::Read(ReadKind::NoAttachment) => "no Faunosteo attachment found".to_owned(),
Self::Read(ReadKind::AttachmentStripped) => {
"attachment stripped (the PDF is stamped as a Faunosteo archive)".to_owned()
}
Self::Json(error @ JsonReadError::LoneSurrogate { .. }) => {
format!("attachment refused: {error}")
}
Self::Json(JsonReadError::Syntax(_)) => "attachment is not valid JSON".to_owned(),
Self::Version(found) => {
let found = found.map_or_else(|| "null".to_owned(), js_number);
format!("report version {found} cannot be read")
}
Self::Invalid(detail) => format!("report document invalid: {detail}"),
}
}
}
impl From<ReadFailure> for CompareFailure {
fn from(failure: ReadFailure) -> Self {
match failure {
ReadFailure::UnsupportedVersion(found) => Self::Version(found),
ReadFailure::Invalid(detail) => Self::Invalid(detail),
}
}
}
/// The attachment of `bytes` read and compared: the reader's steps of the import, then the
/// layered reading, then the lines. No cap on the JSON values (the TypeScript had none).
pub fn compare_bytes(
sheet: &LoadedSheet,
bytes: &[u8],
opts: CompareOptions,
) -> Result<Comparison, CompareFailure> {
let located =
read_attached_document(bytes, ProbeLimits::UNBOUNDED).map_err(CompareFailure::Read)?;
let data: Value = parse_json(&located.text).map_err(CompareFailure::Json)?;
let outcome = read_archive_document(&data, &sheet.migration_map)?;
Ok(compare_report(&data, &outcome, sheet, opts))
}
/// `data` is the attachment as found in the PDF (bare report or envelope), `outcome` its
/// layered reading; `outcome.migrated` tells which vocabulary the lines use.
pub fn compare_report(
data: &Value,
outcome: &ArchiveReadOutcome,
sheet: &LoadedSheet,
opts: CompareOptions,
) -> Comparison {
let report = &outcome.report;
let source = if outcome.migrated {
Source::V2
} else {
Source::V3
};
let v3_lines: Vec<Vec<String>> = report
.observations
.iter()
.map(|o| lines::observation_lines(o, sheet, opts))
.collect();
let footer = lines::footer_lines(report, opts.full);
// Same privacy default as the listing: texts are compared with `--full`.
let normalized = v3_lines
.iter()
.flatten()
.chain(footer.iter())
.cloned()
.collect();
let observations = if outcome.migrated {
lines::migrated_observation_lines(data, report, sheet, opts)
} else {
v3_lines
};
Comparison {
source,
header: lines::header_lines(source, outcome, sheet, opts),
observations,
footer,
normalized,
counts: lines::counts_of(report),
}
}
+254
View File
@@ -0,0 +1,254 @@
//! The command line of the tool (`compare-reports.ts` `main`), without its file system: the
//! arguments, the loop over the entries, what goes to stdout and stderr, the exit code. The
//! caller lists folders and reads files through [`Files`]; a missing or unreadable file is a
//! failed entry, not a crash.
use std::collections::HashSet;
use super::lines::esc;
use super::{CompareOptions, Comparison, Locale, Source, compare_bytes};
use crate::domain::sheet::LoadedSheet;
pub const USAGE: &str = "usage: pnpm report:compare <file.pdf | folder>... [--summary] [--full] [--locale fr|en] [--diff a.pdf b.pdf]";
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CliArgs {
pub files: Vec<String>,
pub locale: Locale,
pub full: bool,
pub summary: bool,
pub diff: bool,
}
impl CliArgs {
fn options(&self) -> CompareOptions {
CompareOptions {
locale: self.locale,
full: self.full,
}
}
}
/// `parseArgs`: options anywhere, every other argument an entry; the error is the message
/// the TypeScript threw (printed on stderr, exit 1).
pub fn parse_args(argv: &[String]) -> Result<CliArgs, String> {
let mut args = CliArgs {
files: Vec::new(),
locale: Locale::Fr,
full: false,
summary: false,
diff: false,
};
let mut rest = argv.iter();
while let Some(arg) = rest.next() {
match arg.as_str() {
"--full" => args.full = true,
"--summary" => args.summary = true,
"--diff" => args.diff = true,
"--locale" => {
args.locale = match rest.next().map(String::as_str) {
Some("fr") => Locale::Fr,
Some("en") => Locale::En,
_ => return Err("--locale takes fr or en".to_owned()),
}
}
other if other.starts_with("--") => {
return Err(format!("unknown option {}", esc(other)));
}
_ => args.files.push(arg.clone()),
}
}
Ok(args)
}
/// Why an entry cannot be read, worded as the TypeScript CLI did.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum FileFailure {
NotFound,
NotReadable,
NotRegular,
TooLarge,
/// Any other error of the file system, its message.
Other(String),
}
impl FileFailure {
fn reason(&self) -> String {
match self {
Self::NotFound => "file not found".to_owned(),
Self::NotReadable => "file not readable".to_owned(),
Self::NotRegular => "not a regular file".to_owned(),
Self::TooLarge => "file too large".to_owned(),
Self::Other(message) => message.clone(),
}
}
}
/// The file system as the tool uses it. Paths are the caller's: resolved against the folder
/// the command was typed from, printed by their last segment only.
pub trait Files {
/// A folder stands for its PDFs (`.pdf` in any case, sorted); any other argument stays
/// one entry, a missing one included (reported when read).
fn expand(&self, arg: &str) -> Vec<String>;
/// The bytes of one entry: a regular file under the reader's size cap.
fn read(&self, path: &str) -> Result<Vec<u8>, FileFailure>;
}
/// What a run printed and answered.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct Run {
pub stdout: String,
pub stderr: String,
pub exit_code: i32,
}
impl Run {
/// `console.log`: the text and a newline.
fn log(&mut self, text: &str) {
self.stdout.push_str(text);
self.stdout.push('\n');
}
fn fail(message: &str) -> Self {
Self {
stdout: String::new(),
stderr: format!("{message}\n"),
exit_code: 1,
}
}
}
/// The last path segment, as `basename` printed it.
fn name_of(path: &str) -> String {
let name = std::path::Path::new(path)
.file_name()
.map(|n| n.to_string_lossy().into_owned())
.unwrap_or_else(|| path.to_owned());
esc(&name)
}
pub fn render_comparison(name: &str, c: &Comparison) -> Vec<String> {
let mut lines = vec![format!("== {name}")];
lines.extend(c.header.iter().map(|l| format!(" {l}")));
for observation in &c.observations {
for (i, line) in observation.iter().enumerate() {
let indent = if i == 0 { " " } else { " " };
lines.push(format!("{indent}{line}"));
}
}
lines.extend(c.footer.iter().map(|l| format!(" {l}")));
lines
}
pub fn render_summary(name: &str, c: &Comparison) -> String {
let n = c.counts;
let source = match c.source {
Source::V2 => "v2→v3",
Source::V3 => "v3",
};
format!(
"{name} · {source} · {} obs · zones {}/{} unresolved · findings {}/{} unresolved",
n.observations, n.zones, n.unresolved_zones, n.findings, n.unresolved_findings
)
}
/// Lines of A absent from B and lines of B absent from A, in order.
pub fn diff_lines(a: &[String], b: &[String]) -> Vec<String> {
let in_a: HashSet<&String> = a.iter().collect();
let in_b: HashSet<&String> = b.iter().collect();
let mut out: Vec<String> = a
.iter()
.filter(|l| !in_b.contains(l))
.map(|l| format!("- {l}"))
.collect();
out.extend(
b.iter()
.filter(|l| !in_a.contains(l))
.map(|l| format!("+ {l}")),
);
out
}
fn compare_entry(
sheet: &LoadedSheet,
files: &dyn Files,
path: &str,
opts: CompareOptions,
) -> Result<Comparison, String> {
let bytes = files.read(path).map_err(|f| f.reason())?;
compare_bytes(sheet, &bytes, opts).map_err(|f| f.reason())
}
fn print_entry(run: &mut Run, name: &str, c: &Comparison, args: &CliArgs) {
if args.diff {
return;
}
if args.summary {
run.log(&render_summary(name, c));
} else {
let mut lines = render_comparison(name, c);
lines.push(String::new());
run.log(&lines.join("\n"));
}
}
/// Every entry compared and printed, a failure printed as `!! <name>: <reason>`: the
/// comparisons, and how many entries failed.
fn compare_entries(
sheet: &LoadedSheet,
files: &dyn Files,
entries: &[String],
args: &CliArgs,
run: &mut Run,
) -> (Vec<Comparison>, usize) {
let mut results = Vec::new();
let mut failures = 0;
for path in entries {
let name = name_of(path);
match compare_entry(sheet, files, path, args.options()) {
Ok(c) => {
print_entry(run, &name, &c, args);
results.push(c);
}
Err(reason) => {
failures += 1;
run.log(&format!("!! {name}: {}", esc(&reason)));
}
}
}
(results, failures)
}
/// `--diff`: the lines one of the two comparisons has and the other lacks, or that they are
/// identical; nothing when one of the two entries failed.
fn print_diff(run: &mut Run, results: &[Comparison]) {
let [a, b] = results else {
return;
};
let diff = diff_lines(&a.normalized, &b.normalized);
if diff.is_empty() {
run.log("identical v3 content");
} else {
run.log(&diff.join("\n"));
}
}
/// `main`: usage on stderr (exit 1) without an entry or with `--diff` and not two; the
/// entries compared and printed (exit 2 when any failed); the diff of the two printed last.
pub fn run(sheet: &LoadedSheet, argv: &[String], files: &dyn Files) -> Run {
let args = match parse_args(argv) {
Ok(args) => args,
Err(message) => return Run::fail(&message),
};
let entries: Vec<String> = args.files.iter().flat_map(|a| files.expand(a)).collect();
if entries.is_empty() || (args.diff && entries.len() != 2) {
return Run::fail(USAGE);
}
let mut run = Run::default();
let (results, failures) = compare_entries(sheet, files, &entries, &args, &mut run);
if args.diff {
print_diff(&mut run, &results);
}
run.exit_code = if failures > 0 { 2 } else { 0 };
run
}