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
20 changed files with 1717 additions and 828 deletions

View file

@ -28,10 +28,15 @@ pheon hierarchy chip.gds --format ascii --depth 4
pheon hierarchy chip.gds --format mermaid --top CHIP
```
`dump` shows a normalized layout description. Add `--records` to inspect raw
records, including timestamps. `hierarchy` shows cell references without
expanding array placements. Shared cells are marked rather than repeated
indefinitely. Mermaid output is diagram source for a Mermaid renderer.
`dump` lists objects by cell. A prefix such as `3x` counts copies of the same
normalized object within a cell. Coordinates are in database units. Rectangles
show two corners, and longer vertex lists wrap across lines. References show
the target and any array dimensions before the coordinates. Add `--records`
to inspect the original record sequence, including timestamps.
`hierarchy` shows direct placement counts on references. Shared cells are
marked where their contents appear earlier in the tree. Mermaid output is
diagram source for a Mermaid renderer.
## Compare revisions
@ -41,10 +46,13 @@ pheon diff before.gds after.gds --hierarchy ascii --max-changes 20
pheon diff before.gds after.gds --format json > report.json
```
Text reports group changes by cell. A `-` line shows an object from the old file
and a `+` line shows one from the new file. A `~` line explains a likely
replacement when Pheon can pair the two uniquely. Request a hierarchy view to
see which parent cells are affected.
Text reports start with the result and group changes by cell. Added and removed
cell headings give the object count within the comparison scope. Each layer
lists counts of rectangles, polygons, and boxes changed, moved, added, or removed.
Added or removed cells also count references by target.
Other objects use `-` for removals, `+` for additions, and `~` for likely
replacements. Use `--format json` for details of individual objects and
`--hierarchy` to see which parent cells are affected.
| Exit code | Meaning |
| --- | --- |
@ -86,8 +94,10 @@ pheon diff before.gds after.gds --hierarchy mermaid --depth 4
`--show-cell` and `--show-layer` filter displayed changes without changing the
comparison, totals, or exit status. A layer display filter hides references
without a numeric layer. `--max-changes N` limits displayed object occurrences;
JSON includes all changed objects unless this limit is explicit.
without a numeric layer. `--max-changes N` limits displayed object occurrences.
A paired replacement uses two occurrences. The text report counts omissions
from filters and limits before listing cell changes. JSON includes all changed
objects unless the limit is explicit.
The diff hierarchy marks directly changed cells, affected ancestors, and
unchanged targets of modified references. It includes references from both

View file

@ -98,6 +98,16 @@ pub(crate) fn decode_utf8(bytes: Vec<u8>) -> Result<String, ErrType> {
.map_err(|error| ErrType::Failed(format!("Invalid UTF-8 string: {}", error.utf8_error())))
}
pub(crate) fn decode_name(mut bytes: &[u8]) -> Result<&str, String> {
while let Some(unpadded) = bytes.strip_suffix(&[0]) {
bytes = unpadded;
}
if bytes.contains(&0) {
return Err("name contains an embedded NUL".to_string());
}
std::str::from_utf8(bytes).map_err(|error| format!("Invalid UTF-8 string: {error}"))
}
pub fn parse_bitarray(input: &[u8]) -> IResult<'_, [bool; 16]> {
let mut bits = [false; 16];
@ -110,11 +120,12 @@ pub fn parse_bitarray(input: &[u8]) -> IResult<'_, [bool; 16]> {
pub fn parse_ascii(input: &[u8], length: u16) -> IResult<'_, Vec<u8>> {
let length = length as usize;
let (input, data) = take_bytes(input, length)?;
let true_length = length - usize::from(data.last() == Some(&0));
let vec = data[..true_length].to_vec();
Ok((input, vec))
Ok((input, unpad_ascii(data).to_vec()))
}
pub(crate) fn unpad_ascii(data: &[u8]) -> &[u8] {
data.strip_suffix(&[0]).unwrap_or(data)
}

View file

@ -348,7 +348,7 @@ fn run(args: &[OsString]) -> Result<u8, String> {
output.push_str(&diff.text_filtered(max_changes, &show));
if format != "none" {
if format == "mermaid" {
output.push_str("```mermaid\n");
output.push_str("Hierarchy\n```mermaid\n");
}
output.push_str(&diff.hierarchy(format == "mermaid", depth));
if format == "mermaid" {

View file

@ -5,14 +5,14 @@
use crate::records::{
DATATYPE, PATHTYPE, BOXTYPE, NODETYPE, TEXTTYPE,
LAYER, XY, WIDTH, COLROW, PRESENTATION, STRING,
LAYER, XY, WIDTH, COLROW, PRESENTATION, STRING, ArrayPlacement,
STRANS, MAG, ANGLE, PROPATTR, PROPVALUE,
ENDEL, BGNEXTN, ENDEXTN, SNAME,
};
use crate::records;
use crate::record::{RecordHeader, Record};
use crate::basic::{IResult, decode_utf8, fail, take_bytes};
use crate::record::{Record, RecordFrame};
use crate::basic::{ErrType, IResult, decode_name, decode_utf8, fail};
use std::string::String;
use std::collections::HashMap;
@ -125,11 +125,11 @@ pub(crate) fn read_elements<'a>(
let mut boundaries = Vec::<BoundaryRecord>::new();
let mut srefs = Vec::<SRefRecord>::new();
let mut arefs = Vec::<ARefRecord>::new();
let (_, mut header) = RecordHeader::read(input)?; // don't consume tag
while header.tag != records::RTAG_ENDSTR {
(input, _) = match header.tag {
let input = loop {
let (remaining, record) = RecordFrame::read(input)?;
(input, ()) = match record.tag {
records::RTAG_SREF | records::RTAG_AREF => {
let (next_input, ref_record) = read_ref(input, header.tag, names)?;
let (next_input, ref_record) = read_ref(input, record.tag, names)?;
match ref_record {
RefRecord::SRef(sref) => srefs.push(sref),
RefRecord::ARef(aref) => arefs.push(aref),
@ -142,18 +142,17 @@ pub(crate) fn read_elements<'a>(
(next_input, ())
},
records::RTAG_PATH => read_path(input, cell_builder, layers)?,
records::RTAG_NODE => read_boxnode(input, cell_builder, header.tag, layers)?,
records::RTAG_BOX => read_boxnode(input, cell_builder, header.tag, layers)?,
records::RTAG_NODE | records::RTAG_BOX => {
read_boxnode(input, cell_builder, record.tag, layers)?
}
records::RTAG_TEXT => read_text(input, cell_builder, layers)?,
_ => {
// don't care, skip
let result = take_bytes(input, header.data_size + 4)?;
(result.0, ())
records::RTAG_ENDSTR => {
record.decode::<records::ENDSTR, _>()?;
break remaining;
}
_ => (remaining, ()),
};
};
(_, header) = RecordHeader::read(input)?; // don't consume tag
}
(input, _) = take_bytes(input, 4_usize)?; // consume endstr tag
let mut sref_plain = Vec::<SRefRecord>::with_capacity(srefs.len() + arefs.len() / 4);
let mut sref_props = Vec::<SRefRecord>::with_capacity(srefs.len() / 16 + arefs.len() / 64);
@ -220,7 +219,7 @@ pub(crate) fn read_elements<'a>(
let mut next_offset: u32 = 0;
for boundary in group {
if let Some(rect) = boundary_to_rect(&boundary.vertices) {
if let Some(rect) = XY::rectangle(boundary.vertices.as_chunks::<2>().0) {
rect_batch.rects.extend_from_slice(&rect);
} else {
boundary_batch.vertex_offsets.push(next_offset);
@ -297,11 +296,11 @@ pub(crate) fn scan_elements<'a>(
) -> IResult<'a, Vec<ScanRefRecord>> {
let mut input = input;
let mut refs = Vec::<ScanRefRecord>::new();
let (_, mut header) = RecordHeader::read(input)?;
while header.tag != records::RTAG_ENDSTR {
(input, _) = match header.tag {
let input = loop {
let (remaining, record) = RecordFrame::read(input)?;
(input, ()) = match record.tag {
records::RTAG_SREF | records::RTAG_AREF => {
let (next_input, ref_record) = read_ref(input, header.tag, names)?;
let (next_input, ref_record) = read_ref(input, record.tag, names)?;
refs.push(scan_ref_from_record(ref_record));
(next_input, ())
},
@ -310,15 +309,14 @@ pub(crate) fn scan_elements<'a>(
| records::RTAG_NODE
| records::RTAG_BOX
| records::RTAG_TEXT => skip_element(input)?,
_ => {
let result = take_bytes(input, header.data_size + 4)?;
(result.0, ())
records::RTAG_ENDSTR => {
record.decode::<records::ENDSTR, _>()?;
break remaining;
}
_ => (remaining, ()),
};
};
(_, header) = RecordHeader::read(input)?;
}
refs.sort_by_key(|scan_ref| scan_ref.target_id);
(input, _) = take_bytes(input, 4_usize)?;
Ok((input, refs))
}
@ -373,12 +371,12 @@ fn scan_ref_from_record(ref_record: RefRecord) -> ScanRefRecord {
fn skip_element<'a>(input: &'a [u8]) -> IResult<'a, ()> {
let mut input = input;
loop {
let (next_input, header) = RecordHeader::read(input)?;
let (next_input, _) = take_bytes(next_input, header.data_size)?;
input = next_input;
if header.tag == ENDEL::tag() {
return Ok((input, ()));
let (remaining, record) = RecordFrame::read(input)?;
if record.tag == ENDEL::tag() {
record.decode::<ENDEL, _>()?;
return Ok((remaining, ()));
}
input = remaining;
}
}
@ -600,33 +598,16 @@ fn read_path<'a>(
let mut bgn_ext = None;
let mut end_ext = None;
let (mut input, mut header) = RecordHeader::read(input)?;
while header.tag != records::RTAG_XY {
match header.tag {
records::RTAG_PATHTYPE => {
let _path_type;
(input, _path_type) = PATHTYPE::read_data(input, header.data_size)?;
path_type = Some(_path_type);
},
records::RTAG_WIDTH => {
let _width;
(input, _width) = WIDTH::read_data(input, header.data_size)?;
width = Some(_width);
},
records::RTAG_BGNEXTN => {
let _bgn_ext;
(input, _bgn_ext) = BGNEXTN::read_data(input, header.data_size)?;
bgn_ext = Some(_bgn_ext);
},
records::RTAG_ENDEXTN => {
let _end_ext;
(input, _end_ext) = ENDEXTN::read_data(input, header.data_size)?;
end_ext = Some(_end_ext);
},
_ =>
return fail(input, format!("Unexpected tag {:04x}", header.tag)),
};
(input, header) = RecordHeader::read(input)?;
let (mut input, mut record) = RecordFrame::read(input)?;
while record.tag != records::RTAG_XY {
match record.tag {
records::RTAG_PATHTYPE => path_type = Some(record.decode::<PATHTYPE, _>()?),
records::RTAG_WIDTH => width = Some(record.decode::<WIDTH, _>()?),
records::RTAG_BGNEXTN => bgn_ext = Some(record.decode::<BGNEXTN, _>()?),
records::RTAG_ENDEXTN => end_ext = Some(record.decode::<ENDEXTN, _>()?),
_ => return fail(record.data, format!("Unexpected tag {:04x}", record.tag)),
}
(input, record) = RecordFrame::read(input)?;
}
let path_type_builder = path_builder.field_builder::<Int16Builder>(1).unwrap();
path_type_builder.append_option(path_type);
@ -638,7 +619,7 @@ fn read_path<'a>(
width_builder.append_option(width);
let xys_builder = path_builder.field_builder::<DListBuilder>(5).unwrap();
let (input, xy_iter) = XY::read_data(input, header.data_size)?;
let xy_iter = record.decode::<XY, _>()?;
for xy in xy_iter {
let xy_builder = xys_builder.values().as_any_mut().downcast_mut::<Int32Builder>().unwrap();
xy_builder.append_value(xy);
@ -717,46 +698,27 @@ fn read_text<'a>(
let (input, dtype) = TEXTTYPE::read(input)?;
insert_layer(layer, dtype, layers, text_builder, 0);
let (mut input, mut header) = RecordHeader::read(input)?;
while header.tag != records::RTAG_XY {
match header.tag {
let (mut input, mut record) = RecordFrame::read(input)?;
while record.tag != records::RTAG_XY {
match record.tag {
// TODO warn if repeat tags?
records::RTAG_PRESENTATION => {
let _presentation;
(input, _presentation) = PRESENTATION::read_data(input, header.data_size)?;
pres_hori = Some(_presentation[14] as u8 * 2 + _presentation[15] as u8);
pres_vert = Some(_presentation[12] as u8 * 2 + _presentation[13] as u8);
pres_font = Some(_presentation[10] as u8 * 2 + _presentation[11] as u8);
},
records::RTAG_PATHTYPE => {
let _path_type;
(input, _path_type) = PATHTYPE::read_data(input, header.data_size)?;
path_type = Some(_path_type);
},
records::RTAG_WIDTH => {
let _width;
(input, _width) = WIDTH::read_data(input, header.data_size)?;
width = Some(_width);
},
records::RTAG_STRANS => {
let strans;
(input, strans) = STRANS::read_data(input, header.data_size)?;
invert_y = Some(strans[0]);
},
records::RTAG_MAG => {
let _mag;
(input, _mag) = MAG::read_data(input, header.data_size)?;
mag = Some(_mag);
},
records::RTAG_ANGLE => {
let _angle_deg;
(input, _angle_deg) = ANGLE::read_data(input, header.data_size)?;
angle_deg = Some(_angle_deg);
},
_ =>
return fail(input, format!("Unexpected tag {:04x}", header.tag)),
let presentation = record.decode::<PRESENTATION, _>()?;
pres_hori = Some(presentation[14] as u8 * 2 + presentation[15] as u8);
pres_vert = Some(presentation[12] as u8 * 2 + presentation[13] as u8);
pres_font = Some(presentation[10] as u8 * 2 + presentation[11] as u8);
}
(input, header) = RecordHeader::read(input)?;
records::RTAG_PATHTYPE => path_type = Some(record.decode::<PATHTYPE, _>()?),
records::RTAG_WIDTH => width = Some(record.decode::<WIDTH, _>()?),
records::RTAG_STRANS => {
let strans = record.decode::<STRANS, _>()?;
invert_y = Some(strans[0]);
}
records::RTAG_MAG => mag = Some(record.decode::<MAG, _>()?),
records::RTAG_ANGLE => angle_deg = Some(record.decode::<ANGLE, _>()?),
_ => return fail(record.data, format!("Unexpected tag {:04x}", record.tag)),
}
(input, record) = RecordFrame::read(input)?;
}
let pres_hori_builder = text_builder.field_builder::<UInt8Builder>(1).unwrap();
@ -776,10 +738,11 @@ fn read_text<'a>(
let angle_builder = text_builder.field_builder::<Float64Builder>(8).unwrap();
angle_builder.append_option(angle_deg);
let (input, mut xy_iter) = XY::read_data(input, header.data_size)?;
let xx = xy_iter.next().unwrap();
let yy = xy_iter.next().unwrap();
let xy = ((xx as u32 as u64) << 32) | (yy as u32 as u64);
let mut xy_iter = record.decode::<XY, _>()?;
let (Some(xx), Some(yy)) = (xy_iter.next(), xy_iter.next()) else {
return fail(record.data, "TEXT needs an XY coordinate pair".to_string());
};
let xy = pack_point([xx, yy]);
let xy_builder = text_builder.field_builder::<UInt64Builder>(9).unwrap();
xy_builder.append_value(xy);
@ -796,48 +759,11 @@ fn read_text<'a>(
}
fn boundary_to_rect(vertices: &[i32]) -> Option<[i32; 4]> {
if vertices.len() != 8 {
return None;
}
let xs = [vertices[0], vertices[2], vertices[4], vertices[6]];
let ys = [vertices[1], vertices[3], vertices[5], vertices[7]];
let min_x = *xs.iter().min().unwrap();
let max_x = *xs.iter().max().unwrap();
let min_y = *ys.iter().min().unwrap();
let max_y = *ys.iter().max().unwrap();
if min_x >= max_x || min_y >= max_y {
return None;
}
let mut seen_corners: u8 = 0;
for ii in 0..4 {
let jj = (ii + 1) % 4;
let same_x = xs[jj] == xs[ii];
let same_y = ys[jj] == ys[ii];
if same_x == same_y {
return None;
}
let x = xs[ii];
let y = ys[ii];
if (x != min_x && x != max_x) || (y != min_y && y != max_y) {
return None;
}
let code = ((x == max_x) as u8) << 1 | ((y == max_y) as u8);
seen_corners |= 1 << code;
}
if seen_corners != 0b1111 {
return None;
}
Some([min_x, min_y, max_x, max_y])
fn pack_point([x, y]: [i32; 2]) -> u64 {
((x as u32 as u64) << 32) | (y as u32 as u64)
}
fn read_ref<'a>(
input: &'a [u8],
tag: u16,
@ -856,65 +782,46 @@ fn read_ref<'a>(
let mut colrow = None;
let (input, struct_name_bytes) = SNAME::skip_and_read(input)?;
let struct_name = decode_utf8(struct_name_bytes).map_err(|error| (input, error))?;
let struct_name = decode_name(&struct_name_bytes)
.map_err(|error| (input, ErrType::Failed(error)))?;
let next_id = names.len();
let id = names.entry(struct_name).or_insert(next_id.try_into().unwrap());
let id = names.entry(struct_name.to_owned()).or_insert(next_id.try_into().unwrap());
let (mut input, mut header) = RecordHeader::read(input)?;
while header.tag != records::RTAG_XY {
match header.tag {
let (mut input, mut record) = RecordFrame::read(input)?;
while record.tag != records::RTAG_XY {
match record.tag {
records::RTAG_STRANS => {
let strans;
(input, strans) = STRANS::read_data(input, header.data_size)?;
let strans = record.decode::<STRANS, _>()?;
invert_y = strans[0];
},
records::RTAG_MAG => {
let _mag;
(input, _mag) = MAG::read_data(input, header.data_size)?;
scale = _mag;
},
}
records::RTAG_MAG => scale = record.decode::<MAG, _>()?,
records::RTAG_ANGLE => {
let _angle_deg;
(input, _angle_deg) = ANGLE::read_data(input, header.data_size)?;
angle_rad = _angle_deg.to_radians();
},
angle_rad = record.decode::<ANGLE, _>()?.to_radians();
}
records::RTAG_COLROW => {
if header.data_size != 4 {
return fail(input, "COLROW must contain two counts".to_string());
}
let mut _colrow;
(input, _colrow) = COLROW::read_data(input, header.data_size)?;
colrow = Some((_colrow.next().unwrap(), _colrow.next().unwrap()));
if !is_aref {
return fail(input, "Got a COLROW record inside an SREF".to_string());
return fail(record.data, "Got a COLROW record inside an SREF".to_string());
}
},
_ =>
return fail(input, format!("Unexpected tag {:04x}", header.tag)),
};
(input, header) = RecordHeader::read(input)?;
COLROW::check_size(record.data.len() as u16)
.map_err(|error| (record.data, ErrType::Failed(error)))?;
colrow = Some(record.data);
}
_ => return fail(record.data, format!("Unexpected tag {:04x}", record.tag)),
}
(input, record) = RecordFrame::read(input)?;
}
let expected_xy_size = if is_aref { 24 } else { 8 };
if header.data_size != expected_xy_size {
return fail(input, format!("Expected {expected_xy_size} bytes of reference coordinates, got {}", header.data_size));
}
let (input, mut xy_iter) = XY::read_data(input, header.data_size)?;
let xx = xy_iter.next().unwrap();
let yy = xy_iter.next().unwrap();
let xy = ((xx as u32 as u64) << 32) | (yy as u32 as u64);
let (input, properties) = read_properties_vec(input)?;
if is_aref {
if colrow.is_none() {
return fail(input, "AREF without COLROW before XY".to_string())
}
let (count0, count1) = colrow.unwrap();
if count0 <= 0 || count1 <= 0 {
return fail(input, "AREF column and row counts must be positive".to_string());
}
let Some(colrow) = colrow else {
return fail(input, "AREF without COLROW before XY".to_string());
};
let placement = ArrayPlacement::decode(colrow, record.data)?;
let [columns, rows] = placement.counts;
let [origin, column_end, row_end] = placement.points;
let pitch = |end: i32, origin: i32, count: i16| {
let pitch = |end: i32, origin: i32, count: u16| {
let delta = i64::from(end) - i64::from(origin);
let divisor = i64::from(count);
if delta % divisor != 0 {
@ -927,32 +834,35 @@ fn read_ref<'a>(
"AREF pitch exceeds signed 32-bit coordinate range".to_string()
)))
};
let x0a = pitch(xy_iter.next().unwrap(), xx, count0)?;
let y0a = pitch(xy_iter.next().unwrap(), yy, count0)?;
let x1a = pitch(xy_iter.next().unwrap(), xx, count1)?;
let y1a = pitch(xy_iter.next().unwrap(), yy, count1)?;
let xy0a = ((x0a as u32 as u64) << 32) | (y0a as u32 as u64);
let xy1a = ((x1a as u32 as u64) << 32) | (y1a as u32 as u64);
let counts = ((count0 as u16 as u32) << 16) | (count1 as u16 as u32);
let axis_pitch = |end: [i32; 2], count| -> Result<_, _> {
Ok(pack_point([
pitch(end[0], origin[0], count)?,
pitch(end[1], origin[1], count)?,
]))
};
let counts = (u32::from(columns) << 16) | u32::from(rows);
Ok((input, RefRecord::ARef(ARefRecord {
target_id: *id,
invert_y,
scale,
angle_rad,
xy,
xy0: xy0a,
xy1: xy1a,
xy: pack_point(origin),
xy0: axis_pitch(column_end, columns)?,
xy1: axis_pitch(row_end, rows)?,
counts,
properties,
})))
} else {
if record.data.len() != 8 {
return fail(record.data, "SREF needs exactly one XY coordinate pair".to_string());
}
let origin = XY::points(record.data).next().unwrap();
Ok((input, RefRecord::SRef(SRefRecord {
target_id: *id,
invert_y,
scale,
angle_rad,
xy,
xy: pack_point(origin),
properties,
})))
}
@ -976,22 +886,25 @@ fn walk_properties<'a>(
input: &'a [u8],
mut on_property: impl FnMut(PropertyRecord),
) -> IResult<'a, ()> {
let (mut input, mut header) = RecordHeader::read(input)?;
while header.tag != ENDEL::tag() {
if header.tag == PROPATTR::tag() {
let key;
let mut input = input;
loop {
let (remaining, record) = RecordFrame::read(input)?;
match record.tag {
records::RTAG_ENDEL => {
record.decode::<ENDEL, _>()?;
return Ok((remaining, ()));
}
records::RTAG_PROPATTR => {
let key = record.decode::<PROPATTR, _>()?;
let value_bytes;
(input, key) = PROPATTR::read_data(input, header.data_size)?;
(input, value_bytes) = PROPVALUE::read(input)?;
on_property(PropertyRecord {
key,
value: decode_utf8(value_bytes).map_err(|error| (input, error))?,
});
(input, value_bytes) = PROPVALUE::read(remaining)?;
let value = decode_utf8(value_bytes).map_err(|error| (input, error))?;
on_property(PropertyRecord { key, value });
}
_ if record.data.is_empty() => input = remaining,
_ => return fail(record.data, format!("Unexpected tag {:04x}", record.tag)),
}
(input, header) = RecordHeader::read(input)?;
}
Ok((input, ()))
}

View file

@ -4,10 +4,10 @@
use std::io::Write;
use crate::record::{RecordHeader, Record};
use crate::record::{Record, RecordFrame};
use crate::records;
use crate::elements::{read_elements, scan_elements, DListBuilder, FListBuilder, ScanRefRecord};
use crate::basic::{ErrType, IResult, OResult, decode_utf8, take_bytes, fail};
use crate::basic::{ErrType, IResult, OResult, decode_name, fail};
use std::string::String;
use std::collections::HashMap;
@ -325,7 +325,7 @@ fn append_file_header(
lib_builder: &mut StructBuilder,
header: &FileHeader,
) -> Result<(), ErrType> {
let name = decode_utf8(header.name.clone())?;
let name = decode_name(&header.name).map_err(ErrType::Failed)?;
let dbu_builder = lib_builder.field_builder::<Float64Builder>(0).unwrap();
dbu_builder.append_value(header.meters_per_db_unit);
let uu_builder = lib_builder.field_builder::<Float64Builder>(1).unwrap();
@ -409,6 +409,24 @@ fn intern_name(names: &mut HashMap<String, u32>, name: String) -> u32 {
}
fn read_cell_header<'a>(begin: &RecordFrame<'a>, input: &'a [u8]) -> IResult<'a, String> {
if begin.tag != records::RTAG_BGNSTR {
return fail(
begin.data,
format!(
"Unexpected record! Got tag 0x{:04x}, expected 0x{:04x}",
begin.tag,
records::RTAG_BGNSTR
),
);
}
begin.decode::<records::BGNSTR, _>()?;
let (input, name_bytes) = records::STRNAME::read(input)?;
let name = decode_name(&name_bytes).map_err(|error| (input, ErrType::Failed(error)))?;
Ok((input, name.to_owned()))
}
fn append_full_cell<'a>(
cells_builder: &mut DListBuilder,
names: &mut HashMap<String, u32>,
@ -440,23 +458,23 @@ where
F: FnMut(&'a [u8], usize, u32, &mut HashMap<String, u32>) -> IResult<'a, ()>,
{
let input_size = input.len();
let (mut input, mut header) = RecordHeader::read(input)?;
while header.tag != records::RTAG_ENDLIB {
let record_start = base_offset + input_size - input.len() - 4;
if header.tag == records::RTAG_BGNSTR {
(input, _) = records::BGNSTR::read_data(input, header.data_size)?;
let name_bytes;
(input, name_bytes) = records::STRNAME::read(input)?;
let name = decode_utf8(name_bytes).map_err(|error| (input, error))?;
let mut input = input;
loop {
let (remaining, record) = RecordFrame::read(input)?;
match record.tag {
records::RTAG_BGNSTR => {
let record_start = base_offset + input_size - input.len();
let (after_name, name) = read_cell_header(&record, remaining)?;
let id = intern_name(names, name);
let (next_input, ()) = on_struct(input, record_start, id, names)?;
input = next_input;
} else {
(input, _) = take_bytes(input, header.data_size)?;
(input, ()) = on_struct(after_name, record_start, id, names)?;
}
records::RTAG_ENDLIB => {
record.decode::<records::ENDLIB, _>()?;
return Ok((remaining, ()));
}
_ => input = remaining,
}
(input, header) = RecordHeader::read(input)?;
}
Ok((input, ()))
}
@ -559,10 +577,8 @@ pub fn read_selected_library<'a>(input: &'a [u8], ranges: &[(u64, u64)]) -> IRes
let start = usize::try_from(*start_u64).unwrap();
let end = usize::try_from(*end_u64).unwrap();
let struct_input = &input[start..end];
let (struct_input, _) = records::BGNSTR::read(struct_input)?;
let (struct_input, name_bytes) = records::STRNAME::read(struct_input)?;
let name = decode_utf8(name_bytes).map_err(|error| (struct_input, error))?;
let (struct_input, begin) = RecordFrame::read(&input[start..end])?;
let (struct_input, name) = read_cell_header(&begin, struct_input)?;
let file_offset = end - struct_input.len();
let id = intern_name(&mut names, name);

View file

@ -6,7 +6,7 @@ use std::io::{self, Write};
use crate::basic::{pack_datetime, pack_bitarray, pack_ascii, pack_int2, pack_int4, pack_real8};
use crate::basic::{parse_datetime, parse_bitarray, parse_ascii, parse_int2, parse_int4, parse_real8};
use crate::basic::{OResult, IResult, fail, parse_u16, take_bytes};
use crate::basic::{ErrType, IResult, OResult, fail, incomplete, take_bytes};
use crate::records;
@ -19,12 +19,15 @@ pub struct RecordHeader {
impl RecordHeader {
pub fn read(input: &[u8]) -> IResult<'_, RecordHeader> {
let (input, size) = parse_u16(input)?;
let (input, tag) = parse_u16(input)?;
let Some((&[size_hi, size_lo, tag_hi, tag_lo], payload)) = input.split_first_chunk::<4>() else {
return incomplete(input, Some(4));
};
let size = u16::from_be_bytes([size_hi, size_lo]);
let Some(data_size) = size.checked_sub(4) else {
return fail(input, format!("Record size {size} is smaller than its header"));
};
Ok((input, RecordHeader{tag, data_size}))
let tag = u16::from_be_bytes([tag_hi, tag_lo]);
Ok((payload, RecordHeader { tag, data_size }))
}
pub fn pack_into(&self) -> io::Result<[u8; 4]> {
@ -43,6 +46,39 @@ impl RecordHeader {
}
/// A complete record borrowing its payload from the input.
pub(crate) struct RecordFrame<'a> {
pub tag: u16,
pub data: &'a [u8],
}
impl<'a> RecordFrame<'a> {
pub fn read(input: &'a [u8]) -> IResult<'a, Self> {
let (_, header) = RecordHeader::read(input)?;
let (remaining, data) = read_payload(input, header.data_size)?;
Ok((remaining, Self { tag: header.tag, data }))
}
/// Decode the payload after the caller has matched the record tag.
pub fn decode<R: Record<D> + ?Sized, D: RecordData>(
&self,
) -> Result<D::BareData<'a>, (&'a [u8], ErrType)> {
let (_, value) = R::read_data(self.data, self.data.len() as u16)?;
Ok(value)
}
}
fn read_payload(input: &[u8], size: u16) -> IResult<'_, &[u8]> {
if !size.is_multiple_of(2) {
return fail(input, "odd record length".to_string());
}
let Some((record, remaining)) = input.split_at_checked(4 + usize::from(size)) else {
return fail(input, "truncated record".to_string());
};
Ok((remaining, &record[4..]))
}
pub trait RecordData {
type BareData<'a>;
type InData : ?Sized;
@ -91,25 +127,28 @@ pub trait Record<RData: RecordData> {
///
fn skip_past(input: &[u8]) -> IResult<'_, bool> {
let original_input = input;
let (mut input, mut header) = RecordHeader::read(input)?;
while header.tag != Self::tag() {
(input, _) = take_bytes(input, header.data_size)?;
if header.tag == records::RTAG_ENDLIB {
return Ok((original_input, false))
let mut input = input;
loop {
let (remaining, record) = RecordFrame::read(input)?;
if record.tag == Self::tag() {
return Ok((remaining, true));
}
(input, header) = RecordHeader::read(input)?;
if record.tag == records::RTAG_ENDLIB {
return Ok((original_input, false));
}
input = remaining;
}
(input, _) = take_bytes(input, header.data_size)?;
Ok((input, true))
}
fn skip_and_read(input: &[u8]) -> IResult<'_, RData::BareData<'_>> {
let (mut input, mut header) = RecordHeader::read(input)?;
while header.tag != Self::tag() {
(input, _) = take_bytes(input, header.data_size)?;
(input, header) = RecordHeader::read(input)?;
let mut input = input;
loop {
let (remaining, record) = RecordFrame::read(input)?;
if record.tag == Self::tag() {
return Ok((remaining, record.decode::<Self, RData>()?));
}
input = remaining;
}
Self::read_data(input, header.data_size)
}
fn expect_header(input: &[u8]) -> IResult<'_, u16> {
@ -122,8 +161,10 @@ pub trait Record<RData: RecordData> {
}
fn read(input: &[u8]) -> IResult<'_, RData::BareData<'_>> {
let (input, size) = Self::expect_header(input)?;
Self::read_data(input, size)
let (_, size) = Self::expect_header(input)?;
let (remaining, data) = read_payload(input, size)?;
let (_, value) = Self::read_data(data, size)?;
Ok((remaining, value))
}
fn write<W: Write>(ww: &mut W, data: &RData::InData) -> OResult {

View file

@ -2,7 +2,11 @@
/// Record type and tag definitions
///
use crate::record::{Record, Int2, Int4, Int2Array, Int4Array, Real8, Real8Pair, DateTimePair, BitArray, ASCII, Empty};
use crate::basic::{ErrType, IResult, fail};
use crate::record::{
ASCII, BitArray, DateTimePair, Empty, Int2, Int2Array, Int4, Int4Array, Real8, Real8Pair,
Record, RecordData,
};
// record tags
pub const RTAG_HEADER: u16 = 0x0002;
@ -77,16 +81,64 @@ pub const RTAG_CONTACT: u16 = 0x4500;
pub struct HEADER;
impl Record<Int2> for HEADER {
fn tag() -> u16 { RTAG_HEADER }
fn expected_size() -> Option<u16> { Some(2) }
// Define fixed-size records once for typed decoding and raw-record validation.
macro_rules! fixed_size_records {
($($(#[$attr:meta])* $name:ident => $tag:ident, $data:ty, $size:expr;)+) => {
$(
$(#[$attr])*
pub struct $name;
impl Record<$data> for $name {
fn tag() -> u16 { $tag }
fn expected_size() -> Option<u16> { Some($size) }
}
)+
pub(crate) fn check_payload_size(tag: u16, size: u16) -> Result<(), String> {
match tag {
$($tag => $name::check_size(size),)+
RTAG_UNITS => UNITS::check_size(size),
RTAG_XY => XY::check_size(size),
_ => Ok(()),
}
}
};
}
pub struct BGNLIB;
impl Record<DateTimePair> for BGNLIB {
fn tag() -> u16 { RTAG_BGNLIB }
fn expected_size() -> Option<u16> { Some(2 * 2 * 6) }
fixed_size_records! {
HEADER => RTAG_HEADER, Int2, 2;
BGNLIB => RTAG_BGNLIB, DateTimePair, 2 * 2 * 6;
ENDLIB => RTAG_ENDLIB, Empty, 0;
BGNSTR => RTAG_BGNSTR, DateTimePair, 2 * 6 * 2;
ENDSTR => RTAG_ENDSTR, Empty, 0;
BOUNDARY => RTAG_BOUNDARY, Empty, 0;
PATH => RTAG_PATH, Empty, 0;
SREF => RTAG_SREF, Empty, 0;
AREF => RTAG_AREF, Empty, 0;
TEXT => RTAG_TEXT, Empty, 0;
LAYER => RTAG_LAYER, Int2, 2;
DATATYPE => RTAG_DATATYPE, Int2, 2;
WIDTH => RTAG_WIDTH, Int4, 4;
ENDEL => RTAG_ENDEL, Empty, 0;
COLROW => RTAG_COLROW, Int2Array, 4;
NODE => RTAG_NODE, Empty, 0;
TEXTTYPE => RTAG_TEXTTYPE, Int2, 2;
PRESENTATION => RTAG_PRESENTATION, BitArray, 2;
STRANS => RTAG_STRANS, BitArray, 2;
MAG => RTAG_MAG, Real8, 8;
ANGLE => RTAG_ANGLE, Real8, 8;
PATHTYPE => RTAG_PATHTYPE, Int2, 2;
GENERATIONS => RTAG_GENERATIONS, Int2, 2;
ELFLAGS => RTAG_ELFLAGS, BitArray, 2;
NODETYPE => RTAG_NODETYPE, Int2, 2;
PROPATTR => RTAG_PROPATTR, Int2, 2;
BOX => RTAG_BOX, Empty, 0;
BOXTYPE => RTAG_BOXTYPE, Int2, 2;
PLEX => RTAG_PLEX, Int4, 4;
BGNEXTN => RTAG_BGNEXTN, Int4, 4;
ENDEXTN => RTAG_ENDEXTN, Int4, 4;
STRCLASS => RTAG_STRCLASS, Int2, 2;
/// End of MASKS records
ENDMASKS => RTAG_ENDMASKS, Empty, 0;
}
pub struct LIBNAME;
@ -100,18 +152,14 @@ impl Record<Real8Pair> for UNITS {
// (user_units_per_db_unit, db_units_per_meter)
fn tag() -> u16 { RTAG_UNITS }
fn expected_size() -> Option<u16> { Some(2 * 8) }
}
pub struct ENDLIB;
impl Record<Empty> for ENDLIB {
fn tag() -> u16 { RTAG_ENDLIB }
fn expected_size() -> Option<u16> { Some(0) }
}
pub struct BGNSTR;
impl Record<DateTimePair> for BGNSTR {
fn tag() -> u16 { RTAG_BGNSTR }
fn expected_size() -> Option<u16> { Some(2 * 6 * 2) }
fn read_data(input: &[u8], size: u16) -> IResult<'_, (f64, f64)> {
let (remaining, (user, meters)) = Real8Pair::read(input, size)?;
if !user.is_finite() || user <= 0.0 || !meters.is_finite() || meters <= 0.0 {
return fail(input, "UNITS must be positive finite values".to_string());
}
Ok((remaining, (user, meters)))
}
}
pub struct STRNAME;
@ -120,70 +168,102 @@ impl Record<ASCII> for STRNAME {
fn expected_size() -> Option<u16> { None }
}
pub struct ENDSTR;
impl Record<Empty> for ENDSTR {
fn tag() -> u16 { RTAG_ENDSTR }
fn expected_size() -> Option<u16> { Some(0) }
}
pub struct BOUNDARY;
impl Record<Empty> for BOUNDARY {
fn tag() -> u16 { RTAG_BOUNDARY }
fn expected_size() -> Option<u16> { Some(0) }
}
pub struct PATH;
impl Record<Empty> for PATH {
fn tag() -> u16 { RTAG_PATH }
fn expected_size() -> Option<u16> { Some(0) }
}
pub struct SREF;
impl Record<Empty> for SREF {
fn tag() -> u16 { RTAG_SREF }
fn expected_size() -> Option<u16> { Some(0) }
}
pub struct AREF;
impl Record<Empty> for AREF {
fn tag() -> u16 { RTAG_AREF }
fn expected_size() -> Option<u16> { Some(0) }
}
pub struct TEXT;
impl Record<Empty> for TEXT {
fn tag() -> u16 { RTAG_TEXT }
fn expected_size() -> Option<u16> { Some(0) }
}
pub struct LAYER;
impl Record<Int2> for LAYER {
fn tag() -> u16 { RTAG_LAYER }
fn expected_size() -> Option<u16> { Some(2) }
}
pub struct DATATYPE;
impl Record<Int2> for DATATYPE {
fn tag() -> u16 { RTAG_DATATYPE }
fn expected_size() -> Option<u16> { Some(2) }
}
pub struct WIDTH;
impl Record<Int4> for WIDTH {
fn tag() -> u16 { RTAG_WIDTH }
fn expected_size() -> Option<u16> { Some(4) }
}
pub struct XY;
impl Record<Int4Array> for XY {
fn tag() -> u16 { RTAG_XY }
fn expected_size() -> Option<u16> { None }
fn check_size(size: u16) -> Result<(), String> {
if !size.is_multiple_of(8) {
return Err("XY payload must contain whole coordinate pairs".to_string());
}
Ok(())
}
}
pub struct ENDEL;
impl Record<Empty> for ENDEL {
fn tag() -> u16 { RTAG_ENDEL }
fn expected_size() -> Option<u16> { Some(0) }
impl XY {
/// Iterate over a validated XY payload.
pub(crate) fn points(data: &[u8]) -> impl ExactSizeIterator<Item = [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]),
]
})
}
/// Bounds of a rectangle encoded as a closed XY ring.
pub(crate) fn closed_rectangle(data: &[u8]) -> Option<[i32; 4]> {
if data.len() != 40 {
return None;
}
let mut points = Self::points(data);
let corners: [[i32; 2]; 4] = std::array::from_fn(|_| points.next().unwrap());
if points.next() != Some(corners[0]) {
return None;
}
Self::rectangle(&corners)
}
/// Bounds of four corners in ring order, with the closing point omitted.
pub(crate) fn rectangle(corners: &[[i32; 2]]) -> Option<[i32; 4]> {
let corners: &[[i32; 2]; 4] = corners.try_into().ok()?;
let min_x = corners.iter().map(|point| point[0]).min().unwrap();
let max_x = corners.iter().map(|point| point[0]).max().unwrap();
let min_y = corners.iter().map(|point| point[1]).min().unwrap();
let max_y = corners.iter().map(|point| point[1]).max().unwrap();
if min_x == max_x || min_y == max_y {
return None;
}
let mut seen = 0_u8;
for (index, &[x, y]) in corners.iter().enumerate() {
let [next_x, next_y] = corners[(index + 1) % corners.len()];
if (x == next_x) == (y == next_y) {
return None;
}
if (x != min_x && x != max_x) || (y != min_y && y != max_y) {
return None;
}
let corner = u8::from(x == max_x) << 1 | u8::from(y == max_y);
seen |= 1 << corner;
}
(seen == 0b1111).then_some([min_x, min_y, max_x, max_y])
}
}
/// The array dimensions and its three coordinates in GDSII record order.
pub(crate) struct ArrayPlacement {
pub counts: [u16; 2],
pub points: [[i32; 2]; 3],
}
impl ArrayPlacement {
pub fn decode<'a>(colrow: &'a [u8], xy: &'a [u8]) -> Result<Self, (&'a [u8], ErrType)> {
let (_, mut counts) = COLROW::read_data(colrow, colrow.len() as u16)?;
let columns = counts.next().unwrap();
let rows = counts.next().unwrap();
if columns <= 0 || rows <= 0 {
return Err((
colrow,
ErrType::Failed("AREF column and row counts must be positive".to_string()),
));
}
if xy.len() != 24 {
return Err((
xy,
ErrType::Failed("AREF needs exactly three XY coordinate pairs".to_string()),
));
}
let mut points = XY::points(xy);
Ok(Self {
counts: [columns as u16, rows as u16],
points: std::array::from_fn(|_| points.next().unwrap()),
})
}
}
pub struct SNAME;
@ -192,30 +272,6 @@ impl Record<ASCII> for SNAME {
fn expected_size() -> Option<u16> { None }
}
pub struct COLROW;
impl Record<Int2Array> for COLROW {
fn tag() -> u16 { RTAG_COLROW }
fn expected_size() -> Option<u16> { Some(4) }
}
pub struct NODE;
impl Record<Empty> for NODE {
fn tag() -> u16 { RTAG_NODE }
fn expected_size() -> Option<u16> { Some(0) }
}
pub struct TEXTTYPE;
impl Record<Int2> for TEXTTYPE {
fn tag() -> u16 { RTAG_TEXTTYPE }
fn expected_size() -> Option<u16> { Some(2) }
}
pub struct PRESENTATION;
impl Record<BitArray> for PRESENTATION {
fn tag() -> u16 { RTAG_PRESENTATION }
fn expected_size() -> Option<u16> { Some(2) }
}
pub struct SPACING;
impl Record<Int2> for SPACING {
fn tag() -> u16 { RTAG_SPACING }
@ -228,24 +284,6 @@ impl Record<ASCII> for STRING {
fn expected_size() -> Option<u16> { None }
}
pub struct STRANS;
impl Record<BitArray> for STRANS {
fn tag() -> u16 { RTAG_STRANS }
fn expected_size() -> Option<u16> { Some(2) }
}
pub struct MAG;
impl Record<Real8> for MAG {
fn tag() -> u16 { RTAG_MAG }
fn expected_size() -> Option<u16> { Some(8) }
}
pub struct ANGLE;
impl Record<Real8> for ANGLE {
fn tag() -> u16 { RTAG_ANGLE }
fn expected_size() -> Option<u16> { Some(8) }
}
pub struct UINTEGER;
impl Record<Int2> for UINTEGER {
fn tag() -> u16 { RTAG_UINTEGER }
@ -286,18 +324,6 @@ impl Record<ASCII> for FONTS {
}
}
pub struct PATHTYPE;
impl Record<Int2> for PATHTYPE {
fn tag() -> u16 { RTAG_PATHTYPE }
fn expected_size() -> Option<u16> { Some(2) }
}
pub struct GENERATIONS;
impl Record<Int2> for GENERATIONS {
fn tag() -> u16 { RTAG_GENERATIONS }
fn expected_size() -> Option<u16> { Some(2) }
}
pub struct ATTRTABLE;
impl Record<ASCII> for ATTRTABLE {
fn tag() -> u16 { RTAG_ATTRTABLE }
@ -324,12 +350,6 @@ impl Record<Int2> for STRTYPE {
fn expected_size() -> Option<u16> { None }
}
pub struct ELFLAGS;
impl Record<BitArray> for ELFLAGS {
fn tag() -> u16 { RTAG_ELFLAGS }
fn expected_size() -> Option<u16> { Some(2) }
}
pub struct ELKEY;
impl Record<Int2> for ELKEY {
fn tag() -> u16 { RTAG_ELKEY }
@ -348,54 +368,12 @@ impl Record<Int2> for LINKKEYS {
fn expected_size() -> Option<u16> { Some(2) }
}
pub struct NODETYPE;
impl Record<Int2> for NODETYPE {
fn tag() -> u16 { RTAG_NODETYPE }
fn expected_size() -> Option<u16> { Some(2) }
}
pub struct PROPATTR;
impl Record<Int2> for PROPATTR {
fn tag() -> u16 { RTAG_PROPATTR }
fn expected_size() -> Option<u16> { Some(2) }
}
pub struct PROPVALUE;
impl Record<ASCII> for PROPVALUE {
fn tag() -> u16 { RTAG_PROPVALUE }
fn expected_size() -> Option<u16> { None }
}
pub struct BOX;
impl Record<Empty> for BOX {
fn tag() -> u16 { RTAG_BOX }
fn expected_size() -> Option<u16> { Some(0) }
}
pub struct BOXTYPE;
impl Record<Int2> for BOXTYPE {
fn tag() -> u16 { RTAG_BOXTYPE }
fn expected_size() -> Option<u16> { Some(2) }
}
pub struct PLEX;
impl Record<Int4> for PLEX {
fn tag() -> u16 { RTAG_PLEX }
fn expected_size() -> Option<u16> { Some(4) }
}
pub struct BGNEXTN;
impl Record<Int4> for BGNEXTN {
fn tag() -> u16 { RTAG_BGNEXTN }
fn expected_size() -> Option<u16> { Some(4) }
}
pub struct ENDEXTN;
impl Record<Int4> for ENDEXTN {
fn tag() -> u16 { RTAG_ENDEXTN }
fn expected_size() -> Option<u16> { Some(4) }
}
pub struct TAPENUM;
impl Record<Int2> for TAPENUM {
fn tag() -> u16 { RTAG_TAPENUM }
@ -408,12 +386,6 @@ impl Record<Int2Array> for TAPECODE {
fn expected_size() -> Option<u16> { Some(2 * 6) }
}
pub struct STRCLASS;
impl Record<Int2> for STRCLASS {
fn tag() -> u16 { RTAG_STRCLASS }
fn expected_size() -> Option<u16> { Some(2) }
}
pub struct RESERVED;
impl Record<Int2Array> for RESERVED {
fn tag() -> u16 { RTAG_RESERVED }
@ -432,13 +404,6 @@ impl Record<ASCII> for MASK {
fn expected_size() -> Option<u16> { None }
}
/// End of MASKS records
pub struct ENDMASKS;
impl Record<Empty> for ENDMASKS {
fn tag() -> u16 { RTAG_ENDMASKS }
fn expected_size() -> Option<u16> { Some(0) }
}
pub struct LIBDIRSIZE;
impl Record<Int2> for LIBDIRSIZE {
fn tag() -> u16 { RTAG_LIBDIRSIZE }

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]),
]
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)]
})
.collect()
}
// 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);
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 => p.len() == 1,
RTAG_AREF => p.len() == 3,
RTAG_PATH => p.len() >= 2,
RTAG_NODE => !p.is_empty(),
RTAG_BOX => p.len() == 5,
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"));
}
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(),
]);
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) {
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;
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();
}
if shown < max_objects {
writeln!(
out,
" {sign} @{} bbox={:?} {}",
object.offset, object.bounds, 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);
}
}
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();
}
}
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!(
out,
"{} changed cells; {shown} of {total} object changes shown; {} layer-name changes",
self.cells.len(),
self.layer_names.len()
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, "INCOMPLETE: {diagnostic}").unwrap();
writeln!(out, " {diagnostic}").unwrap();
}
if self.outcome().is_equal() {
out.push_str("equal under this profile\n");
}
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 {

View file

@ -74,54 +74,305 @@ pub(super) fn replacements(old: &[ObjectChange], new: &[ObjectChange]) -> Vec<Re
result
}
pub(super) fn explain(old: &ObjectChange, new: &ObjectChange) -> String {
let mut parts = vec![format!(
"{} modified{}",
kind_name(old.kind),
old.target
.as_ref()
.map(|t| format!(" target={t:?}"))
.unwrap_or_default()
)];
if old.target.is_some()
&& old.origin != new.origin
&& let (Some(a), Some(b)) = (old.origin, new.origin)
{
parts.push(format!(
"origin {a:?} -> {b:?}, delta=({}, {}) dbu",
i64::from(b[0]) - i64::from(a[0]),
i64::from(b[1]) - i64::from(a[1])
));
pub(super) fn field_changes<'a>(
old: &'a ObjectChange,
new: &'a ObjectChange,
) -> impl Iterator<Item = (u16, Option<&'a [u8]>, Option<&'a [u8]>)> {
let mut fields = BTreeMap::<u16, (Option<&[u8]>, Option<&[u8]>)>::new();
for field in &old.fields {
fields.entry(field.tag).or_default().0 = Some(&field.data);
}
if old.counts != new.counts {
let counts = |c: Option<[u16; 2]>| {
c.map(|[a, b]| format!("{a} x {b}"))
.unwrap_or_else(|| "absent".to_string())
for field in &new.fields {
fields.entry(field.tag).or_default().1 = Some(&field.data);
}
fields
.into_iter()
.filter_map(|(tag, (before, after))| (before != after).then_some((tag, before, after)))
}
fn polygon_heading(object: &ObjectChange, rectangular: bool) -> String {
object_heading(object, object_name(object.kind, rectangular))
}
fn object_heading(object: &ObjectChange, kind: &str) -> String {
match object.layer {
Some((layer, datatype)) => format!("{layer}/{datatype}: {kind}"),
None => kind.to_string(),
}
}
fn polygon_xy(object: &ObjectChange) -> &[u8] {
object
.fields
.iter()
.find(|field| field.tag == RTAG_XY)
.map_or(&[], |field| field.data.as_slice())
}
fn vertex_count(xy: &[u8]) -> usize {
XY::points(xy).len().saturating_sub(1)
}
fn rectangle_size(xy: &[u8]) -> Option<[i64; 2]> {
let [xmin, ymin, xmax, ymax] = XY::closed_rectangle(xy)?;
Some([
i64::from(xmax) - i64::from(xmin),
i64::from(ymax) - i64::from(ymin),
])
}
fn translation(before: &[u8], after: &[u8]) -> Option<[i64; 2]> {
let mut old = XY::points(before);
let mut new = XY::points(after);
if old.len() != new.len() {
return None;
}
let (Some(a), Some(b)) = (old.next(), new.next()) else {
return None;
};
parts.push(format!(
let displacement = |[x0, y0]: [i32; 2], [x1, y1]: [i32; 2]| {
[i64::from(x1) - i64::from(x0), i64::from(y1) - i64::from(y0)]
};
let shift = displacement(a, b);
(shift != [0, 0] && old.zip(new).all(|(a, b)| displacement(a, b) == shift)).then_some(shift)
}
#[derive(Default)]
struct ShapeCounts {
rectangles: usize,
polygons: usize,
boxes: usize,
}
impl ShapeCounts {
fn add(&mut self, object: &ObjectChange, replacement: Option<&ObjectChange>) {
let is_rectangle =
|object: &ObjectChange| XY::closed_rectangle(polygon_xy(object)).is_some();
match object.kind {
RTAG_BOX => self.boxes += 1,
_ if is_rectangle(object) && replacement.is_none_or(is_rectangle) => {
self.rectangles += 1
}
_ => self.polygons += 1,
}
}
}
#[derive(Default)]
struct LayerPolygonChanges {
changed: ShapeCounts,
moved: ShapeCounts,
removed: ShapeCounts,
added: ShapeCounts,
}
#[derive(Default)]
pub(super) struct PolygonChanges(BTreeMap<Option<Layer>, LayerPolygonChanges>);
impl PolygonChanges {
pub fn replacement(&mut self, old: &ObjectChange, new: &ObjectChange) {
let counts = self.0.entry(old.layer).or_default();
let moved = old.properties == new.properties
&& translation(polygon_xy(old), polygon_xy(new)).is_some()
&& field_changes(old, new).all(|(tag, _, _)| tag == RTAG_XY);
let shapes = if moved {
&mut counts.moved
} else {
&mut counts.changed
};
shapes.add(old, Some(new));
}
pub fn unmatched(&mut self, object: &ObjectChange, removed: bool) {
let counts = self.0.entry(object.layer).or_default();
let shapes = if removed {
&mut counts.removed
} else {
&mut counts.added
};
shapes.add(object, None);
}
pub fn write(&self, out: &mut String) {
for (layer, counts) in &self.0 {
if let Some((layer, datatype)) = layer {
write!(out, " {layer}/{datatype}: ").unwrap();
} else {
out.push_str(" unknown layer: ");
}
let mut separator = "";
for (action, shapes) in [
("changed", &counts.changed),
("moved", &counts.moved),
("removed", &counts.removed),
("added", &counts.added),
] {
let terms = [
(shapes.rectangles, "rectangle", "rectangles"),
(shapes.polygons, "polygon", "polygons"),
(shapes.boxes, "box", "boxes"),
]
.into_iter()
.filter(|(count, _, _)| *count != 0)
.map(|(count, singular, plural)| {
let shape = if count == 1 { singular } else { plural };
format!("{count} {shape}")
})
.collect::<Vec<_>>();
if !terms.is_empty() {
write!(out, "{separator}{} {action}", terms.join(", ")).unwrap();
separator = " · ";
}
}
out.push('\n');
}
}
}
#[derive(Default)]
pub(super) struct ReferenceCounts {
singles: usize,
arrays: usize,
placements: u128,
}
impl ReferenceCounts {
pub fn add(&mut self, object: &ObjectChange) {
if object.kind == RTAG_AREF {
self.arrays += 1;
if let Some([columns, rows]) = object.counts {
self.placements += u128::from(columns) * u128::from(rows);
}
} else {
self.singles += 1;
self.placements += 1;
}
}
pub fn write(&self, out: &mut String, sign: &str, target: &str) {
write!(out, " {sign} ").unwrap();
if self.singles > 0 {
let suffix = if self.singles == 1 { "" } else { "s" };
write!(out, "{} reference{suffix}", self.singles).unwrap();
}
if self.arrays > 0 {
if self.singles > 0 {
out.push_str(", ");
}
let suffix = if self.arrays == 1 { "" } else { "s" };
write!(out, "{} array reference{suffix}", self.arrays).unwrap();
}
write!(out, " to {target:?}").unwrap();
if self.arrays > 0 {
let suffix = if self.placements == 1 { "" } else { "s" };
write!(out, " ({} placement{suffix})", self.placements).unwrap();
}
out.push('\n');
}
}
fn polygon_replacement(old: &ObjectChange, new: &ObjectChange) -> String {
let before = polygon_xy(old);
let after = polygon_xy(new);
let old_size = rectangle_size(before);
let new_size = rectangle_size(after);
let mut out = polygon_heading(old, old_size.is_some() && new_size.is_some());
if before == after {
out.push_str(" modified");
} else {
match (old_size, new_size) {
(Some(a), Some(b)) if a == b => out.push_str(" moved"),
(Some([w0, h0]), Some([w1, h1])) => {
write!(out, " dimensions changed ({w0} x {h0} -> {w1} x {h1} dbu)").unwrap();
}
_ if translation(before, after).is_some() => out.push_str(" moved"),
_ => {
out.push_str(" vertices changed");
let (a, b) = (vertex_count(before), vertex_count(after));
if a != b {
write!(out, " ({a} -> {b} vertices)").unwrap();
}
}
}
}
out
}
pub(super) fn replacement_summary(old: &ObjectChange, new: &ObjectChange) -> String {
let polygon = is_polygon(old.kind);
let mut out = if polygon {
polygon_replacement(old, new)
} else {
let mut heading = object_heading(old, object_name(old.kind, false));
if let Some(target) = &old.target {
write!(heading, " {target:?}").unwrap();
}
heading
};
let mut separator = if polygon { "\n " } else { " " };
for (tag, before, after) in field_changes(old, new) {
if polygon && tag == RTAG_XY {
continue;
}
let change = match (tag, before, after) {
(RTAG_XY, Some(a), Some(b)) => {
if let Some([dx, dy]) = translation(a, b) {
format!("moved by ({dx}, {dy}) dbu")
} else if matches!(old.kind, RTAG_PATH | RTAG_NODE) {
let (a, b) = (XY::points(a).len(), XY::points(b).len());
if a == b {
"vertices changed".to_string()
} else {
format!("vertices changed ({a} -> {b} vertices)")
}
} else {
let points = |data: &[u8]| XY::points(data).collect::<Vec<_>>();
format!("coordinates {:?} -> {:?} dbu", points(a), points(b))
}
}
(RTAG_COLROW, _, _) if old.kind == RTAG_AREF => {
let counts = |dimensions: Option<[u16; 2]>| {
dimensions.map_or_else(|| "absent".to_string(), |[a, b]| format!("{a} x {b}"))
};
format!(
"columns/rows {} -> {}",
counts(old.counts),
counts(new.counts)
));
)
}
if old.width != new.width {
let width = |n: Option<i32>| {
n.map(|n| n.to_string())
.unwrap_or_else(|| "absent".to_string())
};
parts.push(format!(
"width {} -> {} dbu",
width(old.width),
width(new.width)
));
(RTAG_WIDTH, _, _) => {
let width =
|n: Option<i32>| n.map_or_else(|| "absent".to_string(), |n| n.to_string());
format!("width {} -> {} dbu", width(old.width), width(new.width))
}
if old.bounds == new.bounds {
parts.push("coordinate bounds unchanged".to_string());
_ => {
let name = if tag == RTAG_STRING {
"text".to_string()
} else {
parts.push(format!(
"coordinate bounds {:?} -> {:?}",
old.bounds, new.bounds
));
tag_name(tag)
};
format!("{name} {}", format_field_change(tag, before, after))
}
parts.join("; ")
};
write!(out, "{separator}{change}").unwrap();
separator = "\n ";
}
if old.properties != new.properties {
let properties = |items: &[(u16, Vec<u8>)]| {
items
.iter()
.map(|(attribute, value)| format!("{attribute}={}", quoted(value)))
.collect::<Vec<_>>()
.join(", ")
};
write!(
out,
"{separator}properties [{}] -> [{}]",
properties(&old.properties),
properties(&new.properties)
)
.unwrap();
}
out
}

View file

@ -97,7 +97,7 @@ fn diagnostic(value: &Diagnostic) -> Value {
fn field(tag: u16, data: &[u8]) -> Value {
let (encoding, value) = if tag == RTAG_XY && data.len().is_multiple_of(8) {
("xy", json!(points(data)))
("xy", json!(XY::points(data).collect::<Vec<_>>()))
} else if matches!(
tag,
RTAG_LAYER | RTAG_DATATYPE | RTAG_TEXTTYPE | RTAG_NODETYPE | RTAG_BOXTYPE
@ -185,19 +185,12 @@ fn object(value: &ObjectChange, id: &str, side: &str) -> Value {
}
fn field_changes(old: &ObjectChange, new: &ObjectChange) -> Vec<Value> {
let a: BTreeMap<_, _> = old.fields.iter().map(|f| (f.tag, &f.data)).collect();
let b: BTreeMap<_, _> = new.fields.iter().map(|f| (f.tag, &f.data)).collect();
a.keys()
.chain(b.keys())
.copied()
.collect::<BTreeSet<_>>()
.into_iter()
.filter(|tag| a.get(tag) != b.get(tag))
.map(|tag| {
changes::field_changes(old, new)
.map(|(tag, before, after)| {
json!({
"tag": tag,
"before": a.get(&tag).map(|data| field(tag, data)),
"after": b.get(&tag).map(|data| field(tag, data))
"before": before.map(|data| field(tag, data)),
"after": after.map(|data| field(tag, data))
})
})
.collect()
@ -292,7 +285,7 @@ impl Comparison {
let b = format!("c{cell_index}:new:{}", r.added);
(shown.contains(&a) && shown.contains(&b)).then(|| json!({
"removed": a, "added": b, "match_rule": r.match_rule,
"explanation": changes::explain(&cell.removed[r.removed], &cell.added[r.added]),
"explanation": changes::replacement_summary(&cell.removed[r.removed], &cell.added[r.added]),
"field_changes": field_changes(&cell.removed[r.removed], &cell.added[r.added]),
"properties_changed": cell.removed[r.removed].properties != cell.added[r.added].properties
}))

View file

@ -17,6 +17,23 @@ pub struct DisplayFilter {
}
impl Scope {
pub(super) fn write_restrictions(&self, out: &mut String) {
if self == &Self::default() {
return;
}
out.push_str("Scope");
if let Some(top) = &self.top {
write!(out, " top={top:?}").unwrap();
}
if let Some(cell) = &self.cell {
write!(out, " cell={cell:?}").unwrap();
}
if let Some((layer, datatype)) = self.layer {
write!(out, " layer={layer}/{datatype}").unwrap();
}
out.push('\n');
}
pub fn description(&self) -> String {
let top = self
.top

View file

@ -160,7 +160,7 @@
"replacements": [
{
"added": "c0:new:0",
"explanation": "BOUNDARY modified; coordinate bounds unchanged",
"explanation": "1/0: polygon vertices changed (4 -> 3 vertices)",
"field_changes": [
{
"after": {

View file

@ -1,9 +1,4 @@
profile pheon-structural-v1
scope top=all cell=all layer=all
coordinates: integer database units; bounds: coordinate envelopes
HEADER=[600]
LIBNAME="test"
UNITS=[0.001, 1e-9] raw=0x3e4189374bc6a7f03944b82fa09b5a54
layer 1/0 name="WG"
library "test" (GDSII 600, 0.001 user units/dbu, 1e-9 m/dbu)
1/0: name "WG"
cell "CHIP"
BOUNDARY layer=1 datatype=0 xy=[0, 0, 0, 10, 10, 10, 10, 0, 0, 0]
1/0: rectangle (0, 0) to (10, 10)

View file

@ -1,7 +1,3 @@
profile pheon-structural-v1
scope top=all cell=all layer=all
cell "CHIP" old@Some(62) new@Some(62)
~ BOUNDARY modified; coordinate bounds unchanged
- @98 bbox=Some([0, 0, 10, 10]) BOUNDARY layer=1 datatype=0 xy=[0, 0, 0, 10, 10, 10, 10, 0, 0, 0]
+ @98 bbox=Some([0, 0, 10, 10]) BOUNDARY layer=1 datatype=0 xy=[0, 0, 0, 10, 10, 0, 0, 0]
1 changed cells; 2 of 2 object changes shown; 0 layer-name changes
Different · 1 changed cell · 1 removed, 1 added
cell "CHIP"
1/0: 1 polygon changed

View file

@ -1,5 +1,4 @@
flowchart TD
%% scope top=all cell=all layer=all
n0["CHIP [affected]"]
n1["leaf [cell changed]"]
n2["left [affected]"]

View file

@ -1,5 +1,5 @@
scope top=all cell=all layer=all
"CHIP" x1 [affected]
Hierarchy
"CHIP" [affected]
"left" x1 [affected]
"leaf" x2 [cell changed]
"right" x1 [affected]

View file

@ -2,12 +2,14 @@ mod common;
use arrow_array::ffi::{FFI_ArrowArray, FFI_ArrowSchema};
use pheon::{
basic::ErrType,
basic::{ErrType, encode_real8},
last_error_message,
library::{read_library, read_selected_library, scan_library},
read_bytes, read_cells_bytes,
record::RecordHeader,
records, scan_bytes,
records,
review::{Comparison, Document, dump_records},
scan_bytes,
};
use std::ffi::CStr;
@ -126,6 +128,136 @@ fn invalid_utf8_returns_located_parse_errors_in_each_reader() {
}
}
#[test]
fn extra_name_padding_is_ignored_by_comparison_and_arrow_readers() {
let (mut bytes, (start, end)) = fixture();
let target = payload_range(&bytes, records::RTAG_SNAME);
bytes[target].copy_from_slice(b"leaf");
bytes.truncate(end as usize);
common::record(&mut bytes, records::RTAG_BGNSTR, &[0; 24]);
common::record(&mut bytes, records::RTAG_STRNAME, b"leaf");
common::record(&mut bytes, records::RTAG_ENDSTR, &[]);
common::record(&mut bytes, records::RTAG_ENDLIB, &[]);
let mut padded = bytes.clone();
for tag in [
records::RTAG_LIBNAME,
records::RTAG_STRNAME,
records::RTAG_SNAME,
] {
let payload = payload_range(&padded, tag);
let mut data = padded[payload.clone()].to_vec();
data.extend_from_slice(&[0; 2]);
let mut record = Vec::new();
common::record(&mut record, tag, &data);
padded.splice(payload.start - 4..payload.end, record);
}
let baseline = Document::parse(&bytes).unwrap();
let document = Document::parse(&padded).unwrap();
assert_eq!(document.cells["töp"].objects.len(), 2);
assert!(
Comparison::between(&baseline, &document)
.outcome()
.is_equal()
);
let full = read_library(&padded).unwrap().1;
let scan = scan_library(&padded).unwrap().1;
let ranges = [(start + 2, end + 6), (end + 6, padded.len() as u64 - 4)];
let selected = read_selected_library(&padded, &ranges).unwrap().1;
let expected = read_library(&bytes).unwrap().1;
for actual in [&full, &scan, &selected] {
for field in ["lib_name", "cell_names"] {
assert_eq!(actual.column_by_name(field), expected.column_by_name(field));
}
}
assert_eq!(&full, &selected);
}
#[test]
fn interior_nuls_in_names_are_rejected_by_comparison_and_arrow_readers() {
let (bytes, range) = fixture();
for tag in [
records::RTAG_LIBNAME,
records::RTAG_STRNAME,
records::RTAG_SNAME,
] {
let mut malformed = bytes.clone();
let payload = payload_range(&malformed, tag);
malformed[payload.start] = 0;
let offset = if tag == records::RTAG_LIBNAME {
payload_range(&malformed, records::RTAG_UNITS).end
} else {
payload.end
};
let message = "name contains an embedded NUL";
check_read_errors(&malformed, range, offset, message, true);
assert_eq!(
Document::parse(&malformed).unwrap_err(),
format!("at byte {}: {message}", payload.start - 4)
);
}
}
#[test]
fn malformed_record_frames_are_rejected_at_the_same_offset() {
let (bytes, range) = fixture();
// STRING also exercises framing in the Arrow scan's geometry skip path.
for tag in [
records::RTAG_STRNAME,
records::RTAG_XY,
records::RTAG_STRING,
] {
let start = payload_range(&bytes, tag).start - 4;
for (size, message) in [
(2_u16, "Record size 2 is smaller than its header"),
(5, "odd record length"),
(u16::MAX - 1, "truncated record"),
] {
let mut malformed = bytes.clone();
malformed[start..start + 2].copy_from_slice(&size.to_be_bytes());
check_read_errors(&malformed, range, start, message, true);
assert_eq!(
dump_records(&malformed).unwrap_err(),
format!("at byte {start}: {message}")
);
}
}
}
#[test]
fn nonpositive_unit_scales_are_rejected_by_comparison_and_arrow_readers() {
let (bytes, range) = fixture();
let units = payload_range(&bytes, records::RTAG_UNITS);
let message = "UNITS must be positive finite values";
for field in [units.start, units.start + 8] {
for scale in [0.0, -1.0] {
let mut malformed = bytes.clone();
malformed[field..field + 8]
.copy_from_slice(&encode_real8(scale).unwrap().to_be_bytes());
check_read_errors(&malformed, range, units.start, message, true);
assert_eq!(
Document::parse(&malformed).unwrap_err(),
format!("at byte {}: {message}", units.start - 4)
);
}
}
}
#[test]
fn coordinate_payloads_require_whole_pairs() {
let (mut bytes, (start, end)) = fixture();
let after_text = payload_range(&bytes, records::RTAG_TEXT).end;
let xy = payload_range(&bytes[after_text..], records::RTAG_XY);
let payload_start = after_text + xy.start;
let mut replacement = Vec::new();
common::record(&mut replacement, records::RTAG_XY, &[0; 12]);
bytes.splice(payload_start - 4..after_text + xy.end, replacement);
let message = "XY payload must contain whole coordinate pairs";
check_read_errors(&bytes, (start, end + 4), payload_start, message, false);
assert!(Document::parse(&bytes).unwrap_err().contains(message));
}
#[test]
fn large_invalid_utf8_strings_do_not_dump_their_contents() {
let (mut bytes, range) = fixture();

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]

View file

@ -75,7 +75,7 @@ fn comparison_reports_survive_dropped_documents_and_overwritten_inputs() {
assert_eq!(limited["hierarchy"], actual["hierarchy"]);
assert_eq!(limited["summary"], actual["summary"]);
assert_eq!(limited["exit_code"], 1);
assert!(comparison.text(0).contains("0 of 2 object changes shown"));
assert!(comparison.text(0).contains("Showing 0 of 2 object changes"));
}
#[test]
@ -118,9 +118,10 @@ fn comparison_diagnostics_capture_both_sides_and_scope_mismatch_once() {
assert_eq!(report["diagnostics"][1]["side"], "new");
assert_eq!(report["diagnostics"][2]["code"], "scope_mismatch");
let text = comparison.text(0);
let scope = text.find("INCOMPLETE: comparison scopes differ").unwrap();
let old = text.find("INCOMPLETE: old:").unwrap();
let new = text.find("INCOMPLETE: new:").unwrap();
assert!(text.starts_with("Incomplete\nScope cell=\"CHIP\"\nDiagnostics\n"));
let scope = text.find("\n comparison scopes differ").unwrap();
let old = text.find("\n old:").unwrap();
let new = text.find("\n new:").unwrap();
assert!(scope < old && old < new);
}
@ -259,7 +260,7 @@ fn future_layer_metadata_remains_incomplete_under_geometry_scopes() {
let diff = Comparison::between(&doc, &doc);
assert_eq!(diff.outcome().exit_code(), 2);
assert!(!diff.outcome().is_equal());
assert!(diff.text(0).contains("INCOMPLETE"));
assert!(diff.text(0).starts_with("Incomplete\n"));
let json = diff.json_report(&inputs(), &ReportOptions::default());
assert_eq!(json["status"], "incomplete");
assert_eq!(json["result"], "unknown");
@ -348,6 +349,21 @@ fn typed_evidence_preserves_defaults_properties_bytes_and_real_encodings() {
b[within..within + 8].copy_from_slice(&0x4201000000000000_u64.to_be_bytes());
let a = layout(&[("CHIP", vec![a]), ("leaf", vec![])]);
let b = layout(&[("CHIP", vec![b]), ("leaf", vec![])]);
let before = Document::parse(&a).unwrap();
let after = Document::parse(&b).unwrap();
assert!(before.canonical_dump().contains("mag=1.0 property"));
assert!(
after
.canonical_dump()
.contains("mag=1.0 raw=0x4201000000000000")
);
let text = Comparison::between(&before, &after).text(100);
assert!(text.contains("mag 1.0 raw=0x4110000000000000 -> 1.0 raw=0x4201000000000000"));
let omitted = layout(&[("CHIP", vec![reference("leaf", 0, 1)]), ("leaf", vec![])]);
let omitted = Document::parse(&omitted).unwrap();
let text = Comparison::between(&before, &omitted).text(100);
assert!(text.contains("mag 1.0 -> absent"));
assert!(!text.contains("raw="));
let r = report(&a, &b, &ReportOptions::default());
let replacement = &r["cell_changes"][0]["replacements"][0];
assert_eq!(replacement["match_rule"], "unique_kind_target");
@ -587,11 +603,7 @@ fn cli_preserves_non_utf8_path_bytes_and_literal_option_names() {
.output()
.unwrap();
assert_eq!(output.status.code(), Some(0));
assert!(
String::from_utf8(output.stdout)
.unwrap()
.starts_with("profile ")
);
assert_eq!(output.stdout, b"Equal\n");
}
#[test]

View file

@ -122,11 +122,41 @@ fn aref_pitches_use_wide_coordinate_differences_in_full_reads_and_scans() {
#[test]
fn aref_invalid_counts_and_unrepresentable_pitches_return_errors() {
let check_array_error = |bytes: &[u8], message: &str| {
let ranges = [(library_header().len() as u64, bytes.len() as u64 - 4)];
for result in [
read_library(bytes),
scan_library(bytes),
read_selected_library(bytes, &ranges),
] {
let (_, error) = result.unwrap_err();
assert!(matches!(error, pheon::basic::ErrType::Failed(reason)
if reason.contains(message)));
}
assert!(
pheon::review::Document::parse(bytes)
.unwrap_err()
.contains(message)
);
};
for counts in [[0, 1], [-1, 1], [1, 0], [1, -1]] {
let bytes = aref_fixture([0, 0], [10, 0], [0, 10], counts);
assert!(read_library(&bytes).is_err());
assert!(scan_library(&bytes).is_err());
check_array_error(&bytes, "AREF column and row counts must be positive");
}
let mut bytes = aref_fixture([0, 0], [10, 0], [0, 10], [1, 2]);
let xy_offset = pheon::review::Document::parse(&bytes)
.unwrap()
.records
.iter()
.find(|record| record.tag == records::RTAG_XY)
.unwrap()
.offset;
let mut replacement = Vec::new();
record(&mut replacement, records::RTAG_XY, &[0; 16]);
bytes.splice(xy_offset..xy_offset + 28, replacement);
check_array_error(&bytes, "AREF needs exactly three XY coordinate pairs");
let bytes = aref_fixture([i32::MIN, 0], [i32::MAX, 0], [i32::MIN, 10], [1, 2]);
assert!(read_library(&bytes).is_err());
assert!(scan_library(&bytes).is_err());
@ -472,7 +502,7 @@ fn selections_support_subsets_reordering_and_empty_results() {
#[test]
fn id_tables_preserve_encounter_order_and_selection_local_ids() {
let mut bytes = library_header();
for (name, layer, datatype) in [("zeta", 9_i16, 4_i16), ("middle", 2, 8), ("alpha", 9, 4)] {
for (name, layer, datatype) in [("zeta", 9_i16, 4_i16), ("middle", 2, 8), ("alpha\0", 9, 4)] {
record(&mut bytes, records::RTAG_BGNSTR, &[0; 24]);
record(&mut bytes, records::RTAG_STRNAME, name.as_bytes());
if name == "zeta" {