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

@ -1,7 +1,11 @@
//! Deterministic GDS object review without the normalization performed by the
//! Arrow reader. Raw payloads borrow the file; only polygon keys are copied.
use crate::{basic::decode_real8, record::RecordHeader, records::*};
use crate::{
basic::{ErrType, decode_name, decode_real8, encode_real8, unpad_ascii},
record::{Record as _, RecordFrame},
records::*,
};
use std::{
borrow::Cow,
collections::{BTreeMap, BTreeSet},
@ -162,12 +166,56 @@ impl Object<'_> {
fn format_object<'a>(
out: &mut fmt::Formatter<'_>,
kind: u16,
fields: impl IntoIterator<Item = (u16, &'a [u8])>,
layer: Option<Layer>,
fields: impl Iterator<Item = (u16, &'a [u8])> + Clone,
properties: impl IntoIterator<Item = (u16, &'a [u8])>,
) -> fmt::Result {
out.write_str(kind_name(kind))?;
let field = |tag| {
fields
.clone()
.find_map(|(t, data)| (t == tag).then_some(data))
};
let rectangle = if is_polygon(kind) {
field(RTAG_XY).and_then(XY::closed_rectangle)
} else {
None
};
let target = if matches!(kind, RTAG_SREF | RTAG_AREF) {
field(RTAG_SNAME)
} else {
None
};
let dimensions = if kind == RTAG_AREF {
field(RTAG_COLROW).and_then(array_dimensions)
} else {
None
};
if let Some((layer, datatype)) = layer {
write!(out, "{layer}/{datatype}: ")?;
}
out.write_str(object_name(kind, rectangle.is_some()))?;
if let Some(target) = target {
write!(out, " {}", quoted(target))?;
}
if let Some([columns, rows]) = dimensions {
write!(out, " ({columns} × {rows})")?;
}
for (tag, value) in fields {
write!(out, " {}={}", tag_name(tag), format_data(tag, value))?;
if (layer.is_some() && matches_layer_field(kind, tag))
|| (tag == RTAG_SNAME && target.is_some())
|| (tag == RTAG_COLROW && dimensions.is_some())
{
continue;
}
if tag == RTAG_XY && value.len().is_multiple_of(8) {
if let Some([xmin, ymin, xmax, ymax]) = rectangle {
write!(out, " ({xmin}, {ymin}) to ({xmax}, {ymax})")?;
} else {
write_points(out, value)?;
}
} else {
write!(out, " {}={}", tag_name(tag), format_exact_value(tag, value))?;
}
}
for (attribute, value) in properties {
write!(out, " property[{attribute}]={}", quoted(value))?;
@ -175,11 +223,36 @@ fn format_object<'a>(
Ok(())
}
fn array_dimensions(data: &[u8]) -> Option<[u16; 2]> {
let [column_hi, column_lo, row_hi, row_lo]: [u8; 4] = data.try_into().ok()?;
Some([
u16::from_be_bytes([column_hi, column_lo]),
u16::from_be_bytes([row_hi, row_lo]),
])
}
fn write_points(out: &mut fmt::Formatter<'_>, data: &[u8]) -> fmt::Result {
let mut points = XY::points(data);
if points.len() == 1 {
let [x, y] = points.next().unwrap();
return write!(out, " xy=({x}, {y})");
}
out.write_str(" xy=[")?;
for (index, [x, y]) in points.enumerate() {
if index != 0 {
out.write_str(if index % 4 == 0 { ",\n " } else { ", " })?;
}
write!(out, "({x}, {y})")?;
}
out.write_str("]")
}
impl fmt::Display for Object<'_> {
fn fmt(&self, out: &mut fmt::Formatter<'_>) -> fmt::Result {
format_object(
out,
self.kind,
self.layer,
self.key
.fields
.iter()
@ -217,10 +290,6 @@ fn error(offset: usize, message: impl std::fmt::Display) -> String {
format!("at byte {offset}: {message}")
}
fn string(data: &[u8], offset: usize) -> Result<String, String> {
decoded_string(unpad(data), offset)
}
fn decoded_string(data: &[u8], offset: usize) -> Result<String, String> {
if data.contains(&0) {
return Err(error(offset, "name or metadata contains an embedded NUL"));
@ -230,29 +299,16 @@ fn decoded_string(data: &[u8], offset: usize) -> Result<String, String> {
.map_err(|_| error(offset, "name or metadata is not UTF-8"))
}
fn unpad(data: &[u8]) -> &[u8] {
if data.last() == Some(&0) {
&data[..data.len() - 1]
} else {
data
}
}
fn integer(data: &[u8]) -> u16 {
u16::from_be_bytes(data.try_into().unwrap())
}
fn points(data: &[u8]) -> Vec<[i32; 2]> {
data.as_chunks::<8>()
.0
.iter()
.map(|&[x0, x1, x2, x3, y0, y1, y2, y3]| {
[
i32::from_be_bytes([x0, x1, x2, x3]),
i32::from_be_bytes([y0, y1, y2, y3]),
]
})
.collect()
fn coordinate_bounds(points: impl IntoIterator<Item = [i32; 2]>) -> Bounds {
let mut points = points.into_iter();
let [x, y] = points.next().unwrap();
points.fold([x, y, x, y], |[xmin, ymin, xmax, ymax], [x, y]| {
[xmin.min(x), ymin.min(y), xmax.max(x), ymax.max(y)]
})
}
// Booth's least-rotation algorithm avoids quadratic work on repeated vertices.
@ -282,8 +338,7 @@ fn least_rotation(points: &[[i32; 2]]) -> Vec<[i32; 2]> {
(0..n).map(|k| points[(start + k) % n]).collect()
}
fn canonical_ring(data: &[u8]) -> Vec<u8> {
let mut ring = points(data);
fn canonical_ring(mut ring: Vec<[i32; 2]>) -> Vec<u8> {
ring.pop(); // The caller has checked closure.
let forward = least_rotation(&ring);
ring.reverse();
@ -297,37 +352,30 @@ fn canonical_ring(data: &[u8]) -> Vec<u8> {
}
fn validate_payload(record: &Record<'_>) -> Result<(), String> {
let size = record.data.len();
let valid = match record.tag {
RTAG_BGNLIB | RTAG_BGNSTR => size == 24,
RTAG_UNITS => size == 16,
RTAG_HEADER | RTAG_LAYER | RTAG_DATATYPE | RTAG_TEXTTYPE | RTAG_NODETYPE | RTAG_BOXTYPE
| RTAG_PROPATTR | RTAG_PATHTYPE | RTAG_STRANS | RTAG_PRESENTATION | RTAG_ELFLAGS
| RTAG_STRCLASS | RTAG_GENERATIONS => size == 2,
RTAG_WIDTH | RTAG_BGNEXTN | RTAG_ENDEXTN | RTAG_PLEX | RTAG_COLROW => size == 4,
RTAG_MAG | RTAG_ANGLE => size == 8,
RTAG_XY => size.is_multiple_of(8),
RTAG_ENDLIB | RTAG_ENDSTR | RTAG_ENDEL | RTAG_BOUNDARY | RTAG_PATH | RTAG_SREF
| RTAG_AREF | RTAG_TEXT | RTAG_NODE | RTAG_BOX | RTAG_ENDMASKS => size == 0,
_ => true,
};
if !valid {
return Err(error(
let size = record.data.len() as u16;
check_payload_size(record.tag, size).map_err(|message| {
error(
record.offset,
format!("invalid payload size {size} for {}", tag_name(record.tag)),
));
}
if record.tag == RTAG_UNITS
&& record.data.as_chunks::<8>().0.iter().any(|&bytes| {
let value = decode_real8(u64::from_be_bytes(bytes));
!value.is_finite() || value <= 0.0
})
{
return Err(error(record.offset, "UNITS must be positive finite values"));
format!(
"invalid payload size {size} for {}: {message}",
tag_name(record.tag)
),
)
})?;
if record.tag == RTAG_UNITS {
UNITS::read_data(record.data, size)
.map_err(|(_, reason)| payload_error(record.offset, reason))?;
}
Ok(())
}
fn payload_error(offset: usize, reason: ErrType) -> String {
match reason {
ErrType::Failed(message) => error(offset, message),
ErrType::Incomplete(_) => error(offset, "truncated record payload"),
}
}
fn is_element(tag: u16) -> bool {
matches!(
tag,
@ -335,6 +383,23 @@ fn is_element(tag: u16) -> bool {
)
}
fn is_polygon(kind: u16) -> bool {
matches!(kind, RTAG_BOUNDARY | RTAG_BOX)
}
fn layer_type_tag(kind: u16) -> u16 {
match kind {
RTAG_TEXT => RTAG_TEXTTYPE,
RTAG_NODE => RTAG_NODETYPE,
RTAG_BOX => RTAG_BOXTYPE,
_ => RTAG_DATATYPE,
}
}
fn matches_layer_field(kind: u16, tag: u16) -> bool {
tag == RTAG_LAYER || tag == layer_type_tag(kind)
}
fn allowed_field(kind: u16, tag: u16) -> bool {
if matches!(tag, RTAG_ELFLAGS | RTAG_PLEX | RTAG_XY) {
return true;
@ -372,6 +437,7 @@ fn object<'a>(records: &[Record<'a>], issues: &mut Vec<Diagnostic>) -> Result<Ob
let first = &records[0];
let mut fields = Vec::new();
let mut properties = Vec::new();
let mut target = None;
let mut seen = BTreeSet::new();
let mut i = 1;
while i + 1 < records.len() {
@ -381,7 +447,7 @@ fn object<'a>(records: &[Record<'a>], issues: &mut Vec<Diagnostic>) -> Result<Ob
.get(i + 1)
.filter(|r| r.tag == RTAG_PROPVALUE)
.ok_or_else(|| error(record.offset, "PROPATTR without PROPVALUE"))?;
properties.push((integer(record.data), unpad(value.data)));
properties.push((integer(record.data), unpad_ascii(value.data)));
i += 2;
continue;
}
@ -400,18 +466,22 @@ fn object<'a>(records: &[Record<'a>], issues: &mut Vec<Diagnostic>) -> Result<Ob
return Err(error(record.offset, "duplicate element field"));
}
let mut data = Cow::Borrowed(record.data);
if record.tag & 0xff == 6 {
data = Cow::Borrowed(unpad(record.data));
if record.tag == RTAG_SNAME && matches!(first.tag, RTAG_SREF | RTAG_AREF) {
let name = decode_name(record.data).map_err(|message| error(record.offset, message))?;
target = Some(name.to_owned());
data = Cow::Borrowed(name.as_bytes());
} else if record.tag & 0xff == 6 {
data = Cow::Borrowed(unpad_ascii(record.data));
}
if record.tag == RTAG_XY && matches!(first.tag, RTAG_BOUNDARY | RTAG_BOX) {
let p = points(record.data);
if record.tag == RTAG_XY && is_polygon(first.tag) {
let p: Vec<_> = XY::points(record.data).collect();
if p.len() < 4 || p.first() != p.last() {
return Err(error(
record.offset,
"polygon must have at least three vertices and be closed",
));
}
data = Cow::Owned(canonical_ring(record.data));
data = Cow::Owned(canonical_ring(p));
}
fields.push((record.tag, data));
i += 1;
@ -424,7 +494,7 @@ fn object<'a>(records: &[Record<'a>], issues: &mut Vec<Diagnostic>) -> Result<Ob
kind: first.tag,
layer: None,
bounds: None,
target: None,
target,
multiplicity: 1,
key: Key {
kind: first.tag,
@ -437,49 +507,35 @@ fn object<'a>(records: &[Record<'a>], issues: &mut Vec<Diagnostic>) -> Result<Ob
let xy = object
.field(RTAG_XY)
.ok_or_else(|| error(first.offset, "element has no XY record"))?;
let p = points(xy);
let valid_count = match first.tag {
RTAG_TEXT | RTAG_SREF => p.len() == 1,
RTAG_AREF => p.len() == 3,
RTAG_PATH => p.len() >= 2,
RTAG_NODE => !p.is_empty(),
RTAG_BOX => p.len() == 5,
_ => true,
};
if !valid_count {
return Err(error(first.offset, "invalid coordinate count"));
}
object.bounds = Some([
p.iter().map(|p| p[0]).min().unwrap(),
p.iter().map(|p| p[1]).min().unwrap(),
p.iter().map(|p| p[0]).max().unwrap(),
p.iter().map(|p| p[1]).max().unwrap(),
]);
object.bounds = Some(if first.tag == RTAG_AREF {
let counts = object
.field(RTAG_COLROW)
.ok_or_else(|| error(first.offset, "array has no COLROW"))?;
let placement = ArrayPlacement::decode(counts, xy)
.map_err(|(_, reason)| payload_error(first.offset, reason))?;
let [columns, rows] = placement.counts;
object.multiplicity = u64::from(columns) * u64::from(rows);
coordinate_bounds(placement.points)
} else {
let points = XY::points(xy);
let valid_count = match first.tag {
RTAG_TEXT | RTAG_SREF => points.len() == 1,
RTAG_PATH => points.len() >= 2,
RTAG_NODE => points.len() > 0,
RTAG_BOX => points.len() == 5,
_ => true,
};
if !valid_count {
return Err(error(first.offset, "invalid coordinate count"));
}
coordinate_bounds(points)
});
if matches!(first.tag, RTAG_SREF | RTAG_AREF) {
object.target = Some(decoded_string(
object
.field(RTAG_SNAME)
.ok_or_else(|| error(first.offset, "reference has no SNAME"))?,
first.offset,
)?);
if first.tag == RTAG_AREF {
let counts = object
.field(RTAG_COLROW)
.ok_or_else(|| error(first.offset, "array has no COLROW"))?;
let a = i16::from_be_bytes(counts[..2].try_into().unwrap());
let b = i16::from_be_bytes(counts[2..].try_into().unwrap());
if a <= 0 || b <= 0 {
return Err(error(first.offset, "array counts must be positive"));
}
object.multiplicity = a as u64 * b as u64;
if object.target.is_none() {
return Err(error(first.offset, "reference has no SNAME"));
}
} else {
let dtype = match first.tag {
RTAG_TEXT => RTAG_TEXTTYPE,
RTAG_BOX => RTAG_BOXTYPE,
RTAG_NODE => RTAG_NODETYPE,
_ => RTAG_DATATYPE,
};
let dtype = layer_type_tag(first.tag);
object.layer = Some((
integer(
object
@ -511,28 +567,25 @@ fn read_records(input: &[u8]) -> Result<Vec<Record<'_>>, String> {
}
let mut records = Vec::new();
let mut offset = 0;
while offset < input.len() {
let (payload, header) = RecordHeader::read(&input[offset..])
.map_err(|(_, e)| error(offset, format!("invalid record header: {e:?}")))?;
let size = header.data_size as usize;
if !(size + 4).is_multiple_of(2) {
return Err(error(offset, "odd record length"));
}
let data = payload
.get(..size)
.ok_or_else(|| error(offset, "truncated record"))?;
let record = Record {
let mut remaining = input;
while !remaining.is_empty() {
let offset = input.len() - remaining.len();
let (next, frame) = RecordFrame::read(remaining).map_err(|(_, reason)| match reason {
ErrType::Incomplete(_) => error(offset, "truncated record header"),
ErrType::Failed(message) => error(offset, message),
})?;
records.push(Record {
offset,
tag: header.tag,
data,
};
offset += size + 4;
let ended = record.tag == RTAG_ENDLIB;
records.push(record);
if ended {
if input[offset..].iter().any(|b| *b != 0) {
return Err(error(offset, "nonzero data after ENDLIB"));
tag: frame.tag,
data: frame.data,
});
remaining = next;
if frame.tag == RTAG_ENDLIB {
if remaining.iter().any(|b| *b != 0) {
return Err(error(
input.len() - remaining.len(),
"nonzero data after ENDLIB",
));
}
break;
}
@ -602,8 +655,12 @@ impl<'a> Document<'a> {
}
doc.header.push((
record.tag,
if record.tag & 0xff == 6 {
unpad(record.data)
if record.tag == RTAG_LIBNAME {
decode_name(record.data)
.map_err(|message| error(record.offset, message))?
.as_bytes()
} else if record.tag & 0xff == 6 {
unpad_ascii(record.data)
} else {
record.data
},
@ -622,7 +679,9 @@ impl<'a> Document<'a> {
.get(i)
.filter(|r| r.tag == RTAG_STRNAME)
.ok_or_else(|| error(offset, "BGNSTR must be followed by STRNAME"))?;
let name = string(name_record.data, name_record.offset)?;
let name = decode_name(name_record.data)
.map_err(|message| error(name_record.offset, message))?
.to_owned();
if name.is_empty() {
return Err(error(name_record.offset, "empty cell name"));
}
@ -779,23 +838,58 @@ impl<'a> Document<'a> {
}
pub fn canonical_dump(&self) -> String {
if !self.present {
return "library absent\n".to_string();
}
let version_index = self
.header
.iter()
.position(|(tag, _)| *tag == RTAG_HEADER)
.unwrap();
let header = |tag| self.header.iter().find(|(t, _)| *t == tag).unwrap().1;
let (user, meters) = header(RTAG_UNITS).split_at(8);
let version = i16::from_be_bytes(self.header[version_index].1.try_into().unwrap());
let mut out = format!(
"profile {PROFILE}\n{}\ncoordinates: integer database units; bounds: coordinate envelopes\n",
self.scope.description()
"library {} (GDSII {version}, {} user units/dbu, {} m/dbu)\n",
quoted(header(RTAG_LIBNAME)),
format_exact_value(RTAG_UNITS, user),
format_exact_value(RTAG_UNITS, meters)
);
for (tag, data) in &self.header {
writeln!(out, "{}={}", tag_name(*tag), format_data(*tag, data)).unwrap();
self.scope.write_restrictions(&mut out);
for (index, (tag, data)) in self.header.iter().enumerate() {
if index != version_index && !matches!(*tag, RTAG_LIBNAME | RTAG_UNITS) {
writeln!(out, "{}={}", tag_name(*tag), format_exact_value(*tag, data)).unwrap();
}
}
for ((layer, dtype), name) in &self.layer_names {
writeln!(out, "layer {layer}/{dtype} name={name:?}").unwrap();
writeln!(out, "{layer}/{dtype}: name {name:?}").unwrap();
}
for (name, cell) in &self.cells {
writeln!(out, "cell {name:?}").unwrap();
for (tag, data) in &cell.attributes {
writeln!(out, " {}={}", tag_name(*tag), format_data(*tag, data)).unwrap();
writeln!(
out,
" {}={}",
tag_name(*tag),
format_exact_value(*tag, data)
)
.unwrap();
}
for object in cell.objects.iter().filter(|o| !o.metadata) {
writeln!(out, " {object}").unwrap();
let mut objects = cell
.objects
.iter()
.filter(|object| !object.metadata)
.peekable();
while let Some(object) = objects.next() {
let mut count = 1;
while objects.next_if(|next| next.key == object.key).is_some() {
count += 1;
}
if count == 1 {
writeln!(out, " {object}").unwrap();
} else {
writeln!(out, " {count}x {object}").unwrap();
}
}
}
for diagnostic in &self.diagnostics {
@ -827,7 +921,7 @@ fn format_records(records: &[Record<'_>]) -> String {
format_data(
record.tag,
if record.tag & 0xff == 6 {
unpad(record.data)
unpad_ascii(record.data)
} else {
record.data
}
@ -876,6 +970,7 @@ impl fmt::Display for ObjectChange {
format_object(
out,
self.kind,
self.layer,
self.fields
.iter()
.map(|field| (field.tag, field.data.as_slice())),
@ -895,21 +990,17 @@ impl From<&Object<'_>> for ObjectChange {
layer: object.layer,
bounds: object.bounds,
target: object.target.clone(),
origin: object.field(RTAG_XY).map(|data| {
[
i32::from_be_bytes(data[..4].try_into().unwrap()),
i32::from_be_bytes(data[4..8].try_into().unwrap()),
]
}),
origin: object
.field(RTAG_XY)
.and_then(|data| XY::points(data).next()),
width: object
.field(RTAG_WIDTH)
.map(|data| i32::from_be_bytes(data.try_into().unwrap())),
counts: object.target.as_ref().map(|_| {
counts: object.target.as_ref().and_then(|_| {
if object.kind == RTAG_AREF {
let data = object.field(RTAG_COLROW).unwrap();
[integer(&data[..2]), integer(&data[2..])]
object.field(RTAG_COLROW).and_then(array_dimensions)
} else {
[1, 1]
Some([1, 1])
}
}),
fields: owned_fields(
@ -986,19 +1077,34 @@ pub enum LibraryChange {
impl fmt::Display for LibraryChange {
fn fmt(&self, out: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Presence { old, new } => {
let label = |present| if present { "present" } else { "absent" };
write!(out, "presence: {:?} -> {:?}", [label(*old)], [label(*new)])
}
Self::Presence { old, new } => out.write_str(match (old, new) {
(false, true) => "added",
(true, false) => "removed",
(true, true) => "present",
(false, false) => "absent",
}),
Self::Record { tag, old, new } => {
write!(out, "{}: ", tag_name(*tag))?;
out.debug_list()
.entries(old.iter().map(|data| format_data(*tag, data)))
.finish()?;
out.write_str(" -> ")?;
out.debug_list()
.entries(new.iter().map(|data| format_data(*tag, data)))
.finish()
let values = if old.len() <= 1 && new.len() <= 1 {
format_field_change(
*tag,
old.first().map(Vec::as_slice),
new.first().map(Vec::as_slice),
)
} else {
let list = |items: &[Vec<u8>]| {
if items.is_empty() {
return "absent".to_string();
}
let values = items
.iter()
.map(|data| format_exact_value(*tag, data))
.collect::<Vec<_>>()
.join(", ");
format!("[{values}]")
};
format!("{} -> {}", list(old), list(new))
};
write!(out, "{} {values}", tag_name(*tag))
}
}
}
@ -1199,10 +1305,50 @@ impl Comparison {
}
pub fn text_filtered(&self, max_objects: usize, show: &DisplayFilter) -> String {
let mut out = format!("profile {PROFILE}\n{}\n", self.old.scope.description());
if show.cell.is_some() || show.layer.is_some() {
writeln!(out, "display-only filter cell={:?} layer={:?}; status, totals and hierarchy are unfiltered", show.cell, show.layer).unwrap();
let (removed, added) = self.cells.iter().fold((0, 0), |(removed, added), cell| {
(removed + cell.removed.len(), added + cell.added.len())
});
let total = removed + added;
let mut out = match self.outcome() {
Outcome::Equal => "Equal",
Outcome::Different => "Different",
Outcome::Incomplete { .. } => "Incomplete",
Outcome::Failed => "Failed",
}
.to_string();
if !self.cells.is_empty() {
let cells = if self.cells.len() == 1 {
"cell"
} else {
"cells"
};
write!(out, " · {} changed {cells}", self.cells.len()).unwrap();
}
if total != 0 {
write!(out, " · {removed} removed, {added} added").unwrap();
}
for (count, label) in [
(self.library_changes.len(), "library change"),
(self.layer_names.len(), "layer name change"),
] {
if count != 0 {
let plural = if count == 1 { "" } else { "s" };
write!(out, " · {count} {label}{plural}").unwrap();
}
}
out.push('\n');
self.old.scope.write_restrictions(&mut out);
if show.cell.is_some() || show.layer.is_some() {
out.push_str("Display filter");
if let Some(cell) = &show.cell {
write!(out, " cell={cell:?}").unwrap();
}
if let Some((layer, datatype)) = show.layer {
write!(out, " layer={layer}/{datatype}").unwrap();
}
out.push('\n');
}
let notice_start = out.len();
for change in &self.library_changes {
writeln!(out, "library {change}").unwrap();
}
@ -1215,111 +1361,141 @@ impl Comparison {
}
)
}) {
out.push_str("UNITS changed; coordinates remain in each file's database units\n");
out.push_str("Coordinates use each file's database units\n");
}
for change in &self.layer_names {
let name = |value: Option<&str>| {
value.map_or_else(|| "absent".to_string(), |value| format!("{value:?}"))
};
writeln!(
out,
"layer {}/{} name: {:?} -> {:?}",
change.layer.0, change.layer.1, change.old, change.new
"{}/{}: name {} -> {}",
change.layer.0,
change.layer.1,
name(change.old.as_deref()),
name(change.new.as_deref())
)
.unwrap();
}
let changed_layers: BTreeSet<_> = self
.cells
.iter()
.filter(|cell| show.cell.as_ref().is_none_or(|name| name == &cell.name))
.flat_map(|cell| cell.removed.iter().chain(&cell.added))
.filter_map(|object| object.layer)
.filter(|layer| show.layer.is_none_or(|selected| selected == *layer))
.collect();
for layer in changed_layers {
if let Some(name) = self.layer_legend.get(&layer) {
writeln!(out, "layer {}/{} name={name:?}", layer.0, layer.1).unwrap();
}
}
let total: usize = self
.cells
.iter()
.map(|c| c.removed.len() + c.added.len())
.sum();
let body_start = out.len();
let mut shown_layers = BTreeSet::new();
let mut selected = 0;
let mut shown: usize = 0;
let mut hidden_cell_changes = 0;
let visible =
|object: &ObjectChange| show.layer.is_none_or(|layer| object.layer == Some(layer));
let presence = |action, count| {
let objects = if count == 1 { "object" } else { "objects" };
Cow::Owned(format!(" ({action}, {count} {objects})"))
};
for cell in &self.cells {
let note = match (&cell.old, &cell.new) {
(None, Some(_)) => presence("added", cell.added.len()),
(Some(_), None) => presence("removed", cell.removed.len()),
_ if cell.attributes_changed() => Cow::Borrowed(" (attributes changed)"),
_ => Cow::Borrowed(""),
};
if show.cell.as_ref().is_some_and(|name| name != &cell.name) {
hidden_cell_changes += usize::from(!note.is_empty());
continue;
}
writeln!(
out,
"cell {:?} old@{:?} new@{:?}{}",
cell.name,
cell.old.as_ref().map(|revision| revision.offset),
cell.new.as_ref().map(|revision| revision.offset),
if cell.attributes_changed() {
" [cell/header changed]"
} else {
""
}
)
.unwrap();
let cell_start = out.len();
let previously_shown = shown;
writeln!(out, "cell {:?}{note}", cell.name).unwrap();
let whole_cell = cell.old.is_none() || cell.new.is_none();
let mut polygons = changes::PolygonChanges::default();
let mut paired_old = BTreeSet::new();
let mut paired_new = BTreeSet::new();
for replacement in &cell.replacements {
let a = &cell.removed[replacement.removed];
let b = &cell.added[replacement.added];
if shown.saturating_add(2) <= max_objects
&& show
.layer
.is_none_or(|layer| a.layer == Some(layer) && b.layer == Some(layer))
{
writeln!(out, " ~ {}", changes::explain(a, b)).unwrap();
writeln!(out, " - @{} bbox={:?} {}", a.offset, a.bounds, a).unwrap();
writeln!(out, " + @{} bbox={:?} {}", b.offset, b.bounds, b).unwrap();
if max_objects - shown >= 2 && visible(a) && visible(b) {
if is_polygon(a.kind) {
polygons.replacement(a, b);
} else {
writeln!(out, " ~ {}", changes::replacement_summary(a, b)).unwrap();
}
shown_layers.extend(a.layer);
paired_old.insert(replacement.removed);
paired_new.insert(replacement.added);
shown += 2;
}
}
for (sign, objects, paired) in [
("-", &cell.removed, &paired_old),
("+", &cell.added, &paired_new),
for (removed, objects, paired) in [
(true, &cell.removed, &paired_old),
(false, &cell.added, &paired_new),
] {
let sign = if removed { "-" } else { "+" };
let mut references = BTreeMap::<&str, changes::ReferenceCounts>::new();
for (index, object) in objects.iter().enumerate() {
if paired.contains(&index) {
if !visible(object) {
continue;
}
if show.layer.is_some_and(|layer| object.layer != Some(layer)) {
continue;
}
if shown < max_objects {
writeln!(
out,
" {sign} @{} bbox={:?} {}",
object.offset, object.bounds, object
)
.unwrap();
selected += 1;
if !paired.contains(&index) && shown < max_objects {
if is_polygon(object.kind) {
polygons.unmatched(object, removed);
} else if whole_cell && let Some(target) = object.target.as_deref() {
references.entry(target).or_default().add(object);
} else {
writeln!(out, " {sign} {object}").unwrap();
}
shown_layers.extend(object.layer);
shown += 1;
}
}
for (target, counts) in references {
counts.write(&mut out, sign, target);
}
}
polygons.write(&mut out);
if shown == previously_shown && note.is_empty() {
out.truncate(cell_start);
}
}
writeln!(
out,
"{} changed cells; {shown} of {total} object changes shown; {} layer-name changes",
self.cells.len(),
self.layer_names.len()
)
.unwrap();
// General comparison errors precede revision-specific errors in text.
for diagnostic in self
.diagnostics
.iter()
.filter(|d| d.side.is_none())
.chain(self.diagnostics.iter().filter(|d| d.side.is_some()))
{
writeln!(out, "INCOMPLETE: {diagnostic}").unwrap();
let mut legend = String::new();
for layer in shown_layers {
if let Some(name) = self.layer_legend.get(&layer) {
writeln!(legend, "{}/{}: name {name:?}", layer.0, layer.1).unwrap();
}
}
if self.outcome().is_equal() {
out.push_str("equal under this profile\n");
out.insert_str(body_start, &legend);
let mut notice = String::new();
if shown < total {
let mut hidden = Vec::new();
if total > selected {
hidden.push(format!("{} filtered out", total - selected));
}
if selected > shown {
hidden.push(format!("{} over the limit", selected - shown));
}
writeln!(
notice,
"Showing {shown} of {total} object changes ({})",
hidden.join(", ")
)
.unwrap();
}
if hidden_cell_changes != 0 {
let plural = if hidden_cell_changes == 1 { "" } else { "s" };
writeln!(
notice,
"{hidden_cell_changes} cell-level change{plural} filtered out"
)
.unwrap();
}
out.insert_str(notice_start, &notice);
// General comparison errors precede revision-specific errors in text.
if !self.diagnostics.is_empty() {
out.push_str("Diagnostics\n");
for diagnostic in self
.diagnostics
.iter()
.filter(|d| d.side.is_none())
.chain(self.diagnostics.iter().filter(|d| d.side.is_some()))
{
writeln!(out, " {diagnostic}").unwrap();
}
}
out
}
@ -1499,7 +1675,10 @@ fn render_hierarchy(
.enumerate()
.map(|(i, n)| (n, i))
.collect();
let mut out = format!("flowchart TD\n %% {}\n", new.scope.description());
let mut out = "flowchart TD\n".to_string();
if changes.is_none() && new.scope != Scope::default() {
writeln!(out, " %% {}", new.scope.description()).unwrap();
}
for (name, id) in &ids {
let label = format!("{name}{}", status(name));
let label = label
@ -1520,15 +1699,18 @@ fn render_hierarchy(
}
out
} else {
let mut out = format!("{}\n", new.scope.description());
let mut out = "Hierarchy\n".to_string();
if changes.is_none() {
new.scope.write_restrictions(&mut out);
}
for (name, depth, parent, seen, limited) in rows {
let count = parent
.map(|p| edge_label(&p, &name))
.unwrap_or_else(|| "x1".to_string());
write!(out, "{}{name:?}", " ".repeat(depth)).unwrap();
if let Some(parent) = parent {
write!(out, " {}", edge_label(&parent, &name)).unwrap();
}
writeln!(
out,
"{}{name:?} {count}{}{}{}",
" ".repeat(depth),
"{}{}{}",
status(&name),
if seen { " [shown above]" } else { "" },
if limited { " [children omitted]" } else { "" }
@ -1553,6 +1735,57 @@ fn quoted(data: &[u8]) -> String {
out
}
fn real8_words(data: &[u8]) -> impl ExactSizeIterator<Item = u64> + '_ {
data.as_chunks::<8>()
.0
.iter()
.copied()
.map(u64::from_be_bytes)
}
fn format_real8(data: &[u8], show_bytes: bool) -> String {
let mut values = real8_words(data).map(decode_real8);
let mut out = if values.len() == 1 {
format!("{:?}", values.next().unwrap())
} else {
format!("{:?}", values.collect::<Vec<_>>())
};
if show_bytes {
write!(out, " raw={}", hex(data)).unwrap();
}
out
}
fn is_real8(tag: u16, data: &[u8]) -> bool {
tag & 0xff == 5 && data.len().is_multiple_of(8)
}
fn format_exact_value(tag: u16, data: &[u8]) -> String {
if is_real8(tag, data) {
let show_bytes =
real8_words(data).any(|bits| encode_real8(decode_real8(bits)).ok() != Some(bits));
format_real8(data, show_bytes)
} else {
format_data(tag, data)
}
}
fn format_field_change(tag: u16, before: Option<&[u8]>, after: Option<&[u8]>) -> String {
let show_bytes = before.zip(after).is_some_and(|(a, b)| {
is_real8(tag, a)
&& is_real8(tag, b)
&& real8_words(a)
.zip(real8_words(b))
.any(|(a, b)| a != b && decode_real8(a).to_bits() == decode_real8(b).to_bits())
});
let format_value = |data: Option<&[u8]>| match data {
Some(data) if is_real8(tag, data) => format_real8(data, show_bytes),
Some(data) => format_data(tag, data),
None => "absent".to_string(),
};
format!("{} -> {}", format_value(before), format_value(after))
}
fn format_data(tag: u16, data: &[u8]) -> String {
if matches!(
tag,
@ -1583,11 +1816,7 @@ fn format_data(tag: u16, data: &[u8]) -> String {
),
5 if data.len().is_multiple_of(8) => format!(
"{:?} raw={}",
data.as_chunks::<8>()
.0
.iter()
.map(|&b| decode_real8(u64::from_be_bytes(b)))
.collect::<Vec<_>>(),
real8_words(data).map(decode_real8).collect::<Vec<_>>(),
hex(data)
),
6 => quoted(data),
@ -1595,6 +1824,20 @@ fn format_data(tag: u16, data: &[u8]) -> String {
}
}
fn object_name(kind: u16, rectangular: bool) -> &'static str {
match kind {
RTAG_BOUNDARY if rectangular => "rectangle",
RTAG_BOUNDARY => "polygon",
RTAG_BOX => "box",
RTAG_PATH => "path",
RTAG_SREF => "reference",
RTAG_AREF => "array reference",
RTAG_TEXT => "label",
RTAG_NODE => "node",
_ => kind_name(kind),
}
}
fn hex(data: &[u8]) -> String {
let mut out = "0x".to_string();
for byte in data {