Files
Sylpheed/crates/sylpheed-formats/examples/kind_bit0_census.rs
MechaCat02 d394ba6aed style: rustfmt sweep — 107 files the lint gate never saw
This branch predates CI on `main`. `cargo fmt --all` only; no behaviour.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-12 16:34:40 +02:00

72 lines
2.7 KiB
Rust

//! Disc-wide test of one bit: does `kind & 1` mean "this element has a parent"?
//!
//! The port is blocked on what `0x3003` is, having only `0x3002` in its rule.
//! `0x3002` and `0x3003` differ in bit 0 alone, and the struct doc claims bit 0
//! is "has a parent". That is a falsifiable claim over every element on the
//! disc, so it is tested here rather than argued from two screens.
//!
//! cargo run -p sylpheed-formats --example kind_bit0_census
use std::collections::BTreeMap;
use std::path::PathBuf;
use sylpheed_formats::{pak::PakArchive, ui_layout};
fn main() {
let root = PathBuf::from(std::env::var("SYLPHEED_DISC").expect("SYLPHEED_DISC"));
let mut agree = 0usize;
let mut disagree = 0usize;
let mut kinds: BTreeMap<u32, usize> = BTreeMap::new();
let mut kind_parent: BTreeMap<(u32, bool), usize> = BTreeMap::new();
let mut examples: Vec<String> = Vec::new();
let mut paks: Vec<_> = std::fs::read_dir(root.join("dat"))
.expect("dat")
.filter_map(|e| e.ok())
.map(|e| e.path())
.filter(|p| p.extension().map(|x| x == "pak").unwrap_or(false))
.collect();
paks.sort();
for p in &paks {
let Ok(ar) = PakArchive::open(p) else {
continue;
};
for (i, ent) in ar.entries().iter().enumerate() {
let Ok(by) = ar.read(ent) else { continue };
let Some(b) = ui_layout::parse_build(&by) else {
continue;
};
for el in &b.elements {
let has_parent = el.parent.is_some();
let bit0 = el.kind & 1 == 1;
*kinds.entry(el.kind).or_default() += 1;
*kind_parent.entry((el.kind, has_parent)).or_default() += 1;
if bit0 == has_parent {
agree += 1
} else {
disagree += 1;
if examples.len() < 10 {
examples.push(format!(
"{} entry {i} {} kind={:#x} parent={:?}",
p.file_name().unwrap().to_string_lossy(),
el.name,
el.kind,
el.parent
));
}
}
}
}
}
println!("kind&1 == has_parent : agree {agree} DISAGREE {disagree}");
for e in &examples {
println!(" counterexample: {e}");
}
println!("\nkind histogram (count, and how many of each have a parent):");
for (k, n) in &kinds {
let wp = kind_parent.get(&(*k, true)).copied().unwrap_or(0);
println!(
" {k:#06x} n={n:6} with parent {wp:6} without {:6}",
n - wp
);
}
}