Files
Sylpheed/crates/sylpheed-formats/examples/ratc_optless_children.rs
Fabian Hamm ed54f95d54 style: rustfmt sweep -- 774 hunks across 154 files -> 0
`cargo fmt --all -- --check` has failed on every run in this repository's
history, identically on `main` and on every branch. This is #12.

Mechanical: `cargo fmt --all`, nothing else. 154 files, all `.rs`, no other
extension touched. `cargo check --workspace` exits 0 afterwards, so nothing
changed semantically.

ON THE ORDERING, WHICH WAS THE REAL QUESTION.

HANDOFF-2026-09-06 section 7 warns this is the expensive fix: a whole-tree
reformat before #7 and #8 return "would put a conflict in every file of 861
commits and make the reviews those items exist to enable unreadable".

That is measurably too pessimistic, and it had been reasoned rather than
tested. Measured here by three-way merging a rustfmt'd `main` against both
unmerged branches, file by file:

  file/branch pairs tested   32
  merges CLEAN               28
  merges CONFLICTING          4   (8 conflict hunks total)

    sylpheed-cli/src/main.rs      1 hunk
    sylpheed-export/src/check.rs  1
    sylpheed-export/src/screen.rs 4
    sylpheed-export/src/video.rs  2

All four are against `auto/frame-blend-draw-path` only;
`auto/port-p6-audio` does not conflict anywhere. The earlier framing --
154 dirty files, 133 that cannot collide, 21 that can, the collision set
carrying 147 of 774 hunks (19%) -- reproduces exactly. What it did not say
is that most of the 21 still merge cleanly, because rustfmt's edits and the
branches' edits rarely land on the same lines.

So the cost of sweeping now is 4 files and 8 hunks for one branch, against
a check that is otherwise red forever. Deliberately NOT folded into the
WASM PR: 154 reformatted files would make that one unreviewable.

Closes #12
2026-09-08 20:07:01 +02:00

126 lines
5.1 KiB
Rust

//! The 60 RATC children that carry no `opt ` block — do they lack it, or is our
//! window too small?
//!
//! [`ratc::parse`] now prefers the name a child's own `opt ` block states, and
//! falls back to the old backwards printable-run scan when there is no block
//! within 128 bytes. That fallback fires for 60 of the disc's 18 002 children
//! (0.3 %), and the reach of the finding in `docs/re/structures/ratc-child-names.md`
//! stops there: "whether they genuinely lack the block or sit past the search
//! window is not established".
//!
//! This settles that. For every child with no accepted block it reports
//!
//! * whether an `opt ` tag exists at all further back, and how far;
//! * which guard rejected a tag that WAS in the window (length, gap, charset);
//! * the child's position in its bundle and its magic, in case the opt-less
//! ones are structurally distinct (e.g. always the first child);
//! * the raw bytes before the magic, so the fallback's answer can be judged.
//!
//! cargo run -p sylpheed-formats --example ratc_optless_children -- <pak>...
use sylpheed_formats::{pak, ratc};
/// Why a child has no accepted `opt ` name. Mirrors `ratc::opt_name`'s guards
/// one for one, so a rejection here is the same rejection the parser made.
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone)]
enum Why {
/// No `opt ` tag in the 128-byte window, and none anywhere before it either.
NoTagAtAll,
/// No tag in the window, but one exists further back, this many bytes away.
TagBeyondWindow(usize),
/// Tag found, but its BE32 length is 0 or > 64.
BadLength(usize),
/// Tag found, name ends more than 8 bytes before the magic — a neighbour's.
GapTooBig(usize),
/// Tag found, the named bytes are not all printable ASCII.
NotGraphic,
}
/// The parser's own window.
const WINDOW: usize = 128;
fn classify(buf: &[u8], at: usize) -> Option<Why> {
let lo = at.saturating_sub(WINDOW);
let pos = match buf[lo..at].windows(4).rposition(|w| w == b"opt ") {
Some(p) => lo + p,
None => {
// Widen to the whole buffer before the child: is it merely far away?
return Some(match buf[..at].windows(4).rposition(|w| w == b"opt ") {
Some(p) => Why::TagBeyondWindow(at - p),
None => Why::NoTagAtAll,
});
}
};
let len = u32::from_be_bytes(buf.get(pos + 4..pos + 8)?.try_into().ok()?) as usize;
if len == 0 || len > 64 || pos + 8 + len > at {
return Some(Why::BadLength(len));
}
let gap = at - (pos + 8 + len);
if gap > 8 {
return Some(Why::GapTooBig(gap));
}
let s = String::from_utf8_lossy(&buf[pos + 8..pos + 8 + len]);
if s.is_empty() || !s.chars().all(|c| c.is_ascii_graphic()) {
return Some(Why::NotGraphic);
}
None // accepted — this child is not one of the 60
}
fn main() {
let mut total = 0usize;
let mut rows: Vec<(String, usize, usize, String, String, Why)> = Vec::new();
let mut first_child_of_bundle = 0usize;
for path in std::env::args().skip(1) {
let Ok(ar) = pak::PakArchive::open(&path) else {
continue;
};
let short = path.rsplit('/').next().unwrap_or(&path).to_string();
let entries: Vec<_> = ar.entries().to_vec();
for (ei, e) in entries.iter().enumerate() {
let Ok(bytes) = ar.read(e) else { continue };
let Some(kids) = ratc::parse(&bytes) else {
continue;
};
for (ci, c) in kids.iter().enumerate() {
total += 1;
let Some(why) = classify(&bytes, c.offset) else {
continue;
};
if ci == 0 {
first_child_of_bundle += 1;
}
let lo = c.offset.saturating_sub(24);
let hex = bytes[lo..c.offset]
.iter()
.map(|b| format!("{b:02x}"))
.collect::<Vec<_>>()
.join(" ");
rows.push((short.clone(), ei, ci, c.name.clone(), hex, why));
}
}
}
println!("RATC children scanned : {total}");
println!(" with NO accepted `opt ` block: {}", rows.len());
println!(" ... of which are child #0 : {first_child_of_bundle}");
let mut by_why: std::collections::BTreeMap<String, usize> = Default::default();
for r in &rows {
let k = match &r.5 {
Why::TagBeyondWindow(_) => "TagBeyondWindow".to_string(),
Why::BadLength(_) => "BadLength".to_string(),
Why::GapTooBig(_) => "GapTooBig".to_string(),
other => format!("{other:?}"),
};
*by_why.entry(k).or_default() += 1;
}
println!("\nwhy, by cause:");
for (k, n) in &by_why {
println!(" {k:20} x{n}");
}
println!("\nevery occurrence (name is what the FALLBACK scan returned):");
for (p, ei, ci, name, hex, why) in &rows {
println!(" {p:28} entry {ei:4} child {ci:3} {name:24} {why:?}");
println!(" 24 bytes before the magic: {hex}");
}
}