Share GDSII reader logic and simplify review output

Share record framing, name decoding, payload validation, array reference
decoding, and Arrow cell-header parsing. Accept trailing NUL padding in
names and retain located diagnostics for malformed records.

Summarize text diffs by cell, layer, and reference target. Show comparison
and omission counts before detail. Compact canonical dumps while
preserving exact fields and duplicate counts for Git textconv. Simplify
hierarchy output and retain per-object evidence in JSON.
This commit is contained in:
Jan Petykiewicz 2026-11-17 21:49:26 -08:00
commit 15e8a3b7b5

View file

@ -52,10 +52,14 @@ fn gzip(data: &[u8]) -> Vec<u8> {
}
fn polygon(points: &[[i32; 2]]) -> Vec<u8> {
ring(RTAG_BOUNDARY, RTAG_DATATYPE, 1, points)
}
fn ring(kind: u16, type_tag: u16, layer: u16, points: &[[i32; 2]]) -> Vec<u8> {
let mut bytes = Vec::new();
record(&mut bytes, RTAG_BOUNDARY, &[]);
record(&mut bytes, RTAG_LAYER, &1_u16.to_be_bytes());
record(&mut bytes, RTAG_DATATYPE, &0_u16.to_be_bytes());
record(&mut bytes, kind, &[]);
record(&mut bytes, RTAG_LAYER, &layer.to_be_bytes());
record(&mut bytes, type_tag, &0_u16.to_be_bytes());
xy(&mut bytes, points);
record(&mut bytes, RTAG_ENDEL, &[]);
bytes
@ -312,9 +316,7 @@ fn canonical_polygon_dump_is_readable_and_offsets_are_separate() {
let doc = Document::parse(&bytes).unwrap();
let dump = doc.canonical_dump();
assert!(
dump.ends_with(
"cell \"CHIP\"\n BOUNDARY layer=1 datatype=0 xy=[0, 0, 0, 10, 10, 10, 10, 0, 0, 0]\n"
),
dump.ends_with("cell \"CHIP\"\n 1/0: rectangle (0, 0) to (10, 10)\n"),
"{dump}"
);
assert!(!dump.contains("BGNSTR"));
@ -323,6 +325,44 @@ fn canonical_polygon_dump_is_readable_and_offsets_are_separate() {
.contains("0000000000 0002 HEADER size=6 [600]")
);
assert!(doc.record_dump().contains("BGNSTR"));
let notched = polygon(&[[0, 0], [0, 3], [2, 3], [2, 2], [3, 2], [3, 0], [0, 0]]);
let bytes = layout(&[("CHIP", vec![notched])]);
let dump = Document::parse(&bytes).unwrap().canonical_dump();
assert!(
dump.ends_with(concat!(
" 1/0: polygon xy=[(0, 0), (0, 3), (2, 3), (2, 2),\n",
" (3, 2), (3, 0), (0, 0)]\n",
)),
"{dump}"
);
}
#[test]
fn canonical_dump_counts_duplicates_by_normalized_object_within_each_cell() {
let test_layout = |copies: usize| {
let mut objects = vec![square(); copies - 1];
objects.push(polygon(&[[10, 10], [10, 0], [0, 0], [0, 10], [10, 10]]));
objects.push(with_property(square(), "marked"));
layout(&[("A", objects), ("B", vec![square()])])
};
let a = test_layout(3);
let b = test_layout(2);
let a = Document::parse(&a).unwrap();
let b = Document::parse(&b).unwrap();
let dump = a.canonical_dump();
assert!(
dump.ends_with(concat!(
"cell \"A\"\n",
" 3x 1/0: rectangle (0, 0) to (10, 10)\n",
" 1/0: rectangle (0, 0) to (10, 10) property[17]=\"marked\"\n",
"cell \"B\"\n",
" 1/0: rectangle (0, 0) to (10, 10)\n",
)),
"{dump}"
);
assert!(b.canonical_dump().contains(" 2x 1/0: rectangle"));
assert_ne!(dump, b.canonical_dump());
assert_eq!(Comparison::between(&a, &b).outcome().exit_code(), 1);
}
#[test]
@ -442,7 +482,9 @@ fn unknown_records_never_produce_a_successful_equality_claim() {
assert!(!diff.outcome().is_equal());
assert_eq!(diff.outcome().exit_code(), 2);
assert!(doc.record_dump().contains("7f00"));
assert!(diff.text(0).contains("INCOMPLETE"));
let text = diff.text(0);
assert!(text.starts_with("Incomplete\nDiagnostics\n"));
assert!(text.contains("unsupported record 0x7f00"));
}
#[test]
@ -455,8 +497,8 @@ fn shared_hierarchy_reports_ancestors_and_marks_repeated_definitions() {
assert_eq!(
diff.hierarchy(false, 8),
concat!(
"scope top=all cell=all layer=all\n",
"\"CHIP\" x1 [affected]\n",
"Hierarchy\n",
"\"CHIP\" [affected]\n",
" \"left\" x1 [affected]\n",
" \"leaf\" x2 [cell changed]\n",
" \"right\" x1 [affected]\n",
@ -510,7 +552,7 @@ fn same_bounds_changes_and_truncated_reports_remain_differences() {
diff.cells[0].removed[0].bounds,
diff.cells[0].added[0].bounds
);
assert!(diff.text(0).contains("0 of 2 object changes shown"));
assert!(diff.text(0).contains("Showing 0 of 2 object changes"));
assert_eq!(diff.outcome().exit_code(), 1);
assert!(diff.cells[0].removed[0].offset > 0);
}
@ -539,7 +581,10 @@ fn unit_changes_report_values_without_claiming_geometry_equivalence() {
..
}]
));
assert!(diff.text(0).contains("UNITS changed"));
let text = diff.text(0);
assert!(text.contains("library UNITS [0.001, 1e-9] -> [0.001, 2e-9]"));
assert!(text.contains("Coordinates use each file's database units"));
assert!(!text.contains("raw="));
assert!(diff.cells.is_empty());
}
@ -669,6 +714,7 @@ fn cli_uses_documented_exit_codes_and_accepts_paths_with_spaces() {
let command = || Command::new(env!("CARGO_BIN_EXE_pheon"));
let equal = command().arg("diff").arg(&old).arg(&old).output().unwrap();
assert_eq!(equal.status.code(), Some(0));
assert_eq!(equal.stdout, b"Equal\n");
let diff = command()
.arg("diff")
.arg(&old)
@ -678,8 +724,9 @@ fn cli_uses_documented_exit_codes_and_accepts_paths_with_spaces() {
.unwrap();
assert_eq!(diff.status.code(), Some(1));
let text = String::from_utf8(diff.stdout).unwrap();
assert!(text.contains("0 of 2 object changes shown"));
assert!(text.contains("```mermaid\nflowchart TD"));
assert!(text.contains("Showing 0 of 2 object changes"));
assert!(text.contains("Hierarchy\n```mermaid\nflowchart TD"));
assert!(!text.contains("scope top=all"));
let dump = command()
.arg("dump")
.arg(&old)
@ -921,7 +968,7 @@ fn scope_selection_distinguishes_reachable_subtrees_from_exact_cells() {
["CHIP", "leaf"]
);
assert_eq!(Comparison::between(&old, &new).cells.len(), 1);
assert!(old.canonical_dump().contains("scope top=\"CHIP\""));
assert!(old.canonical_dump().contains("Scope top=\"CHIP\"\n"));
let cell = Scope {
cell: Some("CHIP".to_string()),
..Scope::default()
@ -1058,11 +1105,99 @@ fn display_filters_never_change_equality_or_totals() {
layer: Some((42, 0)),
},
);
assert!(text.contains("display-only"));
assert!(text.contains("0 of 2 object changes shown"));
assert!(text.starts_with("Different · 1 changed cell · 1 removed, 1 added\n"));
assert!(text.contains("Display filter layer=42/0\n"));
assert!(text.contains("Showing 0 of 2 object changes (2 filtered out)"));
assert!(!text.contains("cell \"CHIP\""));
assert_eq!(diff.outcome().exit_code(), 1);
}
#[test]
fn text_hides_empty_headings_and_counts_filtered_and_limited_changes() {
use pheon::review::selection::DisplayFilter;
let mut attributes = Vec::new();
record(&mut attributes, RTAG_STRCLASS, &[0, 1]);
let a = layout(&[
("changed", vec![square()]),
("other", vec![square()]),
("gone", vec![square(), triangle()]),
("header", vec![]),
]);
let b = layout(&[
("changed", vec![triangle()]),
("other", vec![triangle()]),
("new", vec![square()]),
("empty", vec![]),
("header", vec![attributes]),
]);
let diff = Comparison::between(&Document::parse(&a).unwrap(), &Document::parse(&b).unwrap());
let text = diff.text(0);
assert!(text.contains("cell \"gone\" (removed, 2 objects)\n"));
assert!(text.contains("cell \"new\" (added, 1 object)\n"));
assert!(text.contains("cell \"empty\" (added, 0 objects)\n"));
assert!(text.contains("cell \"header\" (attributes changed)\n"));
assert!(!text.contains("cell \"changed\""));
assert!(text.contains("Showing 0 of 7 object changes (7 over the limit)"));
let text = diff.text_filtered(
1,
&DisplayFilter {
cell: Some("changed".to_string()),
layer: Some((1, 0)),
},
);
assert!(text.contains(concat!(
"Display filter cell=\"changed\" layer=1/0\n",
"Showing 1 of 7 object changes (5 filtered out, 1 over the limit)\n",
"4 cell-level changes filtered out\n",
"cell \"changed\"\n 1/0: 1 rectangle removed",
)));
assert!(!text.contains("cell \"other\""));
assert!(!text.contains("cell \"new\""));
}
#[test]
fn reference_summaries_group_whole_cells_and_keep_full_placements_in_dumps() {
let empty = layout(&[("leaf", vec![]), ("other", vec![])]);
let with_chip = layout(&[
(
"CHIP",
vec![
reference("leaf", 0, 1),
reference("leaf", 5, 1),
reference("leaf", 2, 2),
reference("other", 10, 1),
],
),
("leaf", vec![]),
("other", vec![]),
]);
let existing_chip = layout(&[("CHIP", vec![]), ("leaf", vec![]), ("other", vec![])]);
let empty = Document::parse(&empty).unwrap();
let with_chip = Document::parse(&with_chip).unwrap();
let existing_chip = Document::parse(&existing_chip).unwrap();
for (old, new, sign) in [(&empty, &with_chip, "+"), (&with_chip, &empty, "-")] {
let diff = Comparison::between(old, new);
let text = diff.text(100);
assert!(text.contains(&format!(
" {sign} 2 references, 1 array reference to \"leaf\" (4 placements)\n"
)));
assert!(text.contains(&format!(" {sign} 1 reference to \"other\"\n")));
assert!(!text.contains("xy="));
let limited = diff.text(2);
assert!(limited.contains(&format!(" {sign} 2 references to \"leaf\"\n")));
assert!(limited.contains("Showing 2 of 4 object changes (2 over the limit)"));
}
let text = Comparison::between(&existing_chip, &with_chip).text(100);
assert!(text.contains(" + reference \"leaf\" xy=(0, 0)\n"));
let dump = with_chip.canonical_dump();
assert!(dump.contains(" reference \"leaf\" xy=(5, 0)\n"));
assert!(dump.contains(" array reference \"leaf\" (2 × 1) xy=[(2, 0), (102, 0), (2, 0)]\n"));
}
#[test]
fn git_external_driver_handles_changes_additions_deletions_and_errors() {
let directory = std::env::temp_dir().join(format!(
@ -1130,7 +1265,7 @@ fn git_external_driver_handles_changes_additions_deletions_and_errors() {
assert!(
String::from_utf8(changed.stdout)
.unwrap()
.contains("2 of 2 object changes shown")
.contains("Different · 1 changed cell · 1 removed, 1 added")
);
fs::write(&path, b"invalid").unwrap();
let invalid = Command::new("git")
@ -1169,7 +1304,10 @@ fn git_external_driver_handles_changes_additions_deletions_and_errors() {
let result = git(&["diff", "--cached", "--find-renames"]);
let text = String::from_utf8(result.stdout).unwrap();
assert!(text.contains("renamed layout.gds"), "{text}");
assert!(text.contains("2 of 2 object changes shown"), "{text}");
assert!(
text.contains("Different · 1 changed cell · 1 removed, 1 added"),
"{text}"
);
git(&["commit", "-qm", "rename fixture"]);
fs::write(directory.join(".gitattributes"), "*.gds diff=pheon-text\n").unwrap();
fs::write(&renamed, shared(square())).unwrap();
@ -1177,7 +1315,7 @@ fn git_external_driver_handles_changes_additions_deletions_and_errors() {
assert!(
String::from_utf8(result.stdout)
.unwrap()
.contains("- BOUNDARY")
.contains("- 1/0: polygon")
);
fs::remove_dir_all(directory).unwrap();
}
@ -1197,21 +1335,130 @@ fn replacement_explanations_are_unique_and_keep_full_evidence() {
let diff = Comparison::between(&old, &new);
assert_eq!(diff.cells[0].replacements.len(), 1);
let text = diff.text(100);
assert!(text.contains(" ~ "));
assert!(text.contains(" - @"));
assert!(text.contains(" + @"));
match pair.name {
"moved reference" => assert!(text.contains("delta=(5, 0) dbu")),
"array count" => assert!(text.contains("columns/rows 2 x 1 -> 3 x 1")),
"moved reference" => {
assert!(text.contains(" ~ reference \"leaf\" moved by (5, 0) dbu\n"))
}
"array count" => {
assert!(text.contains(" ~ array reference \"leaf\" columns/rows 2 x 1 -> 3 x 1\n"))
}
_ => assert_eq!(text, include_str!("golden/review-diff.txt")),
}
assert!(!text.contains(" - "));
assert!(!text.contains(" + "));
assert!(!text.contains("bbox="));
assert!(!diff.text(0).contains(" ~ "));
assert!(diff.text(1).contains("1 of 2 object changes shown"));
assert!(diff.text(1).contains("Showing 1 of 2 object changes"));
}
let a = layout(&[("CHIP", vec![square(), square()])]);
let b = layout(&[("CHIP", vec![triangle(), triangle()])]);
let diff = Comparison::between(&Document::parse(&a).unwrap(), &Document::parse(&b).unwrap());
assert!(diff.cells[0].replacements.is_empty());
let text = diff.text(100);
assert!(text.contains(" 1/0: 2 rectangles removed · 2 polygons added\n"));
assert!(!text.contains("bbox="));
assert!(!text.contains("xy="));
}
#[test]
fn polygon_diff_reports_moves_and_other_changes_without_individual_geometry() {
let before = layout(&[("CHIP", vec![with_property(square(), "old")])]);
let before = Document::parse(&before).unwrap();
for (shape, label) in [
(
polygon(&[[-5, 2], [5, 2], [5, 12], [-5, 12], [-5, 2]]),
"1 rectangle changed",
),
(
polygon(&[[0, 0], [20, 0], [20, 10], [0, 10], [0, 0]]),
"1 rectangle changed",
),
(
polygon(&[[5, 0], [10, 5], [5, 10], [0, 5], [5, 0]]),
"1 polygon changed",
),
(square(), "1 rectangle changed"),
] {
let after = layout(&[("CHIP", vec![with_property(shape, "new")])]);
let after = Document::parse(&after).unwrap();
let diff = Comparison::between(&before, &after);
let text = diff.text(100);
assert!(text.contains(&format!(" 1/0: {label}\n")), "{text}");
assert!(!text.contains("dbu"));
assert!(!text.contains("bbox="));
assert!(!text.contains("xy="));
}
for (shape, shifted, name) in [
(
triangle(),
polygon(&[[2, -1], [12, -1], [2, 9], [2, -1]]),
"polygon",
),
(
square(),
polygon(&[[2, -1], [12, -1], [12, 9], [2, 9], [2, -1]]),
"rectangle",
),
] {
let before = layout(&[("CHIP", vec![shape])]);
let after = layout(&[("CHIP", vec![shifted])]);
let diff = Comparison::between(
&Document::parse(&before).unwrap(),
&Document::parse(&after).unwrap(),
);
assert!(diff.text(100).contains(&format!(" 1/0: 1 {name} moved\n")));
}
}
#[test]
fn polygon_diff_groups_shapes_by_layer_across_dimensions() {
let shapes = [
square(),
polygon(&[[30, 0], [50, 0], [50, 5], [30, 5], [30, 0]]),
polygon(&[[70, 0], [80, 0], [71, 9], [70, 0]]),
ring(
RTAG_BOUNDARY,
RTAG_DATATYPE,
2,
&[[0, 0], [10, 0], [0, 10], [0, 0]],
),
];
let layout_with_property = |value| {
let objects = shapes
.iter()
.map(|shape| with_property(shape.clone(), value))
.collect();
layout(&[("CHIP", objects)])
};
let before = layout_with_property("old");
let after = layout_with_property("new");
let diff = Comparison::between(
&Document::parse(&before).unwrap(),
&Document::parse(&after).unwrap(),
);
assert_eq!(
diff.text(100),
concat!(
"Different · 1 changed cell · 4 removed, 4 added\n",
"cell \"CHIP\"\n",
" 1/0: 2 rectangles, 1 polygon changed\n",
" 2/0: 1 polygon changed\n",
)
);
}
#[test]
fn polygon_diff_distinguishes_boxes_from_rectangular_boundaries() {
let before = layout(&[("CHIP", vec![square()])]);
let points = [[0, 0], [10, 0], [10, 10], [0, 10], [0, 0]];
let after = layout(&[("CHIP", vec![ring(RTAG_BOX, RTAG_BOXTYPE, 1, &points)])]);
let diff = Comparison::between(
&Document::parse(&before).unwrap(),
&Document::parse(&after).unwrap(),
);
let text = diff.text(100);
assert!(text.contains(" 1/0: 1 rectangle removed · 1 box added\n"));
}
#[test]
@ -1231,7 +1478,21 @@ fn path_width_explanation_preserves_absent_and_zero() {
let a = path(None);
let b = path(Some(0));
let diff = Comparison::between(&Document::parse(&a).unwrap(), &Document::parse(&b).unwrap());
assert!(diff.text(100).contains("width absent -> 0 dbu"));
assert!(
diff.text(100)
.contains(" ~ 1/0: path width absent -> 0 dbu\n")
);
}
#[test]
fn label_replacements_show_only_changed_fields() {
let a = layout(&[("CHIP", vec![label((2, 0), "old")])]);
let b = layout(&[("CHIP", vec![label((2, 0), "new")])]);
let diff = Comparison::between(&Document::parse(&a).unwrap(), &Document::parse(&b).unwrap());
assert_eq!(
diff.text(100),
"Different · 1 changed cell · 1 removed, 1 added\ncell \"CHIP\"\n ~ 2/0: label text \"old\" -> \"new\"\n"
);
}
#[test]
@ -1250,8 +1511,20 @@ fn unchanged_layer_names_explain_geometry_changes_once() {
let b = named(triangle());
let diff = Comparison::between(&Document::parse(&a).unwrap(), &Document::parse(&b).unwrap());
let text = diff.text(100);
assert_eq!(text.matches("name=\"WG\"").count(), 1);
assert_eq!(text.matches("1/0: name \"WG\"").count(), 1);
assert!(diff.layer_names.is_empty());
let names = corpus()
.into_iter()
.find(|case| case.name == "layer name only")
.unwrap();
let old = Document::parse(&names.old).unwrap();
let new = Document::parse(&names.new).unwrap();
assert!(
Comparison::between(&old, &new)
.text(0)
.contains("1/0: name \"WG\" -> \"CORE:WG\"")
);
}
#[test]
@ -1261,19 +1534,11 @@ fn library_presence_changes_distinguish_empty_and_absent_revisions() {
let absent = doc.empty_like();
let added = Comparison::between(&absent, &doc);
assert_eq!(added.outcome().exit_code(), 1);
assert!(
added
.text(0)
.contains("library presence: [\"absent\"] -> [\"present\"]\n")
);
assert!(!added.text(0).contains("equal under"));
assert!(added.text(0).contains("library added\n"));
assert!(added.text(0).starts_with("Different · 1 library change\n"));
let removed = Comparison::between(&doc, &absent);
assert_eq!(removed.outcome().exit_code(), 1);
assert!(
removed
.text(0)
.contains("library presence: [\"present\"] -> [\"absent\"]\n")
);
assert!(removed.text(0).contains("library removed\n"));
}
#[test]