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