Introduce Pheon GDSII parsing and layout review tools

Implement native GDSII reading and writing with Arrow interchange, hierarchy
scans and selective cell loading. Validate numeric packing and array pitches,
and preserve normalized REAL8 encodings exactly within the supported range.

Add standalone dump, structural diff, hierarchy, layer-name annotation and Git
diff commands. Provide scoped text and JSON reports, deterministic comparison,
and record-preserving metadata edits.

Package the shared library and optional Python Arrow readers as pheon using
CFFI ABI mode and Python-independent platform wheels. Include locked source
builds, native API checks, cross-platform artifact workflows, documentation
and regression coverage. Require Rust 1.88 or newer and Python 3.11 or newer.

Validation: 87 native tests, 14 binding tests on Python 3.11/3.13/3.14,
and 71 installed companion integration tests pass. Full, scanned and selected
Arrow layouts match the previous schemas.
This commit is contained in:
Jan Petykiewicz 2026-11-16 16:21:21 -08:00
commit b3346b40a4
13 changed files with 12956 additions and 1 deletions

353
src/basic.rs Normal file
View file

@ -0,0 +1,353 @@
///
/// Functionality for parsing and writing basic data types
///
use std::io;
use std::fmt;
pub type OResult = Result<usize, io::Error>;
pub type IResult<'a, O> = Result<(&'a [u8], O), (&'a [u8], ErrType)>;
#[derive(Debug)]
pub enum ErrType {
Incomplete(Option<usize>),
Failed(String),
}
pub fn fail<O>(input: &[u8], msg: String) -> IResult<'_, O> {
Err((input, ErrType::Failed(msg)))
}
pub fn incomplete<O>(input: &[u8], size: Option<usize>) -> IResult<'_, O> {
Err((input, ErrType::Incomplete(size)))
}
pub fn take_bytes<CC: Into<usize>>(input: &[u8], count: CC) -> IResult<'_, &[u8]> {
let cc = count.into();
if input.len() < cc {
incomplete(input, Some(cc))
} else {
let (taken, input) = input.split_at(cc);
Ok((input, taken))
}
}
/*
* Parse functions
*/
fn take_array<const N: usize>(input: &[u8]) -> IResult<'_, [u8; N]> {
match input.split_first_chunk::<N>() {
Some((bytes, remaining)) => Ok((remaining, *bytes)),
None => incomplete(input, Some(N)),
}
}
pub fn parse_u16(input: &[u8]) -> IResult<'_, u16> {
let (input, bytes) = take_array(input)?;
Ok((input, u16::from_be_bytes(bytes)))
}
pub fn parse_int2(input: &[u8]) -> IResult<'_, i16> {
let (input, bytes) = take_array(input)?;
Ok((input, i16::from_be_bytes(bytes)))
}
pub fn parse_int4(input: &[u8]) -> IResult<'_, i32> {
let (input, bytes) = take_array(input)?;
Ok((input, i32::from_be_bytes(bytes)))
}
/// Convert GDS REAL8 to IEEE float64
pub fn decode_real8(int: u64) -> f64 {
let neg = int & 0x8000_0000_0000_0000;
let exp = (int >> 56) & 0x7f;
let mut mant = (int & 0x00ff_ffff_ffff_ffff) as f64;
if neg != 0 {
mant *= -1.0
}
let exp2 = 4 * (exp as i32 - 64) - 56;
mant * 2_f64.powi(exp2)
}
pub fn parse_real8(input: &[u8]) -> IResult<'_, f64> {
let (input, bytes) = take_array(input)?;
let data = u64::from_be_bytes(bytes);
Ok((input, decode_real8(data)))
}
pub fn parse_datetime(input: &[u8]) -> IResult<'_, [i16; 6]> {
let mut buf = [0_i16; 6];
let mut input = input;
for bb in &mut buf {
(input, *bb) = parse_int2(input)?;
}
buf[0] += 1900; // Year is from 1900
Ok((input, buf))
}
pub fn parse_bitarray(input: &[u8]) -> IResult<'_, [bool; 16]> {
let mut bits = [false; 16];
let (input, val) = parse_int2(input)?;
for (ii, bit) in bits.iter_mut().enumerate() {
*bit = ((val >> (16 - 1 - ii)) & 0x01) == 1;
}
Ok((input, bits))
}
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))
}
/*
* Pack functions
*/
pub fn bitarray2int(bits: &[bool; 16]) -> u16 {
let mut int: u16 = 0;
for (ii, bit) in bits.iter().enumerate() {
int |= (*bit as u16) << (16 - 1 - ii);
}
int
}
pub fn pack_bitarray(buf: &mut [u8], bits: &[bool; 16]) {
buf[..2].copy_from_slice(&bitarray2int(bits).to_be_bytes());
}
pub fn pack_int2(buf: &mut [u8], int: i16) {
buf[..2].copy_from_slice(&int.to_be_bytes());
}
pub fn pack_int4(buf: &mut [u8], int: i32) {
buf[..4].copy_from_slice(&int.to_be_bytes());
}
pub fn pack_real8(buf: &mut [u8], fnum: f64) -> Result<(), FloatTooBigError> {
let bytes = encode_real8(fnum)?.to_be_bytes();
buf[..8].copy_from_slice(&bytes);
Ok(())
}
pub fn pack_ascii(buf: &mut [u8], data: &[u8]) -> usize {
let len = data.len();
buf[..len].copy_from_slice(data);
if len % 2 == 1 {
buf[len] = 0;
len + 1
} else {
len
}
}
pub fn pack_datetime(buf: &mut [u8], date: &[i16; 6]) {
assert!(buf.len() >= 6 * 2);
let year = date[0] - 1900;
pack_int2(buf, year);
for ii in 1..6 {
pack_int2(&mut buf[(2 * ii)..], date[ii]);
}
}
#[derive(Debug, Clone)]
pub struct FloatTooBigError {
float_value: f64,
}
impl fmt::Display for FloatTooBigError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Float {0} is too large for Real8", self.float_value)
}
}
/// Convert float64 to a normalized GDS REAL8 representation.
/// Finite magnitudes below 2^-260 underflow to canonical zero, including -0.0.
/// Nonfinite values and magnitudes at or above 2^252 return an error.
pub fn encode_real8(fnum: f64) -> Result<u64, FloatTooBigError> {
let ieee = fnum.to_bits();
let ieee_exp = ((ieee >> 52) & 0x7ff) as i32;
if ieee_exp == 0x7ff {
return Err(FloatTooBigError { float_value: fnum });
}
let exp2 = ieee_exp - 1023;
if exp2 < -260 {
// This also handles both signed zeros and every IEEE subnormal.
return Ok(0);
}
let gds_exp = exp2.div_euclid(4) + 65;
if gds_exp > 0x7f {
return Err(FloatTooBigError { float_value: fnum });
}
// The 53-bit IEEE significand needs only 0..3 left shifts to become
// a normalized 56-bit GDS mantissa. Every in-range f64 is exact in REAL8.
let significand = (ieee & 0x000f_ffff_ffff_ffff) | (1_u64 << 52);
let mantissa = significand << exp2.rem_euclid(4);
let sign = ieee & 0x8000_0000_0000_0000;
Ok(sign | ((gds_exp as u64) << 56) | mantissa)
}
#[cfg(test)]
mod tests {
#[test]
fn test_parse_bitarray() {
use crate::basic::parse_bitarray;
//assert!(parse_bitarray(b"59") == 13625);
assert_eq!(parse_bitarray(b"\x00\x00").unwrap().1, [false; 16]);
assert_eq!(parse_bitarray(b"\xff\xff").unwrap().1, [true; 16]);
let arr_0001 = parse_bitarray(b"\x00\x01").unwrap().1;
for (ii, &vv) in arr_0001.iter().enumerate() {
assert_eq!(ii == 15, vv);
}
let arr_8000 = parse_bitarray(b"\x80\x00").unwrap().1;
for (ii, &vv) in arr_8000.iter().enumerate() {
assert_eq!(ii == 0, vv);
}
}
#[test]
fn test_parse_int2() {
use crate::basic::parse_int2;
assert_eq!(parse_int2(b"59").unwrap().1, 13625);
assert_eq!(parse_int2(b"\0\0").unwrap().1, 0);
assert_eq!(parse_int2(b"\xff\xff").unwrap().1, -1);
}
#[test]
fn test_parse_int4() {
use crate::basic::parse_int4;
assert_eq!(parse_int4(b"4321").unwrap().1, 875770417);
}
#[test]
fn test_decode_real8() {
use crate::basic::decode_real8;
// zeroes
assert_eq!(decode_real8(0x0), 0.0);
assert_eq!(decode_real8(1<<63), 0.0); // negative
assert_eq!(decode_real8(0xff << 56), 0.0); // denormalized
assert_eq!(decode_real8(0x4110 << 48), 1.0);
assert_eq!(decode_real8(0xC120 << 48), -2.0);
}
#[test]
#[should_panic]
fn test_encode_real8_panic() {
use crate::basic::encode_real8;
encode_real8(1e80).unwrap();
}
#[test]
fn test_parse_real8() {
use crate::basic:: parse_real8;
assert_eq!(0.0, parse_real8(&[0; 8]).unwrap().1);
assert_eq!(1.0, parse_real8(&[0x41, 0x10, 0, 0, 0, 0, 0, 0]).unwrap().1);
assert_eq!(-2.0, parse_real8(&[0xC1, 0x20, 0, 0, 0, 0, 0, 0]).unwrap().1);
}
#[test]
fn test_parse_ascii() {
use crate::basic::parse_ascii;
assert_eq!(parse_ascii(b"12345", 5).unwrap().1, b"12345");
assert_eq!(parse_ascii(b"12345\0", 6).unwrap().1, b"12345"); // strips trailing null byte
assert_eq!(parse_ascii(b"123456", 6).unwrap().1, b"123456");
}
#[test]
fn test_pack_bitarray() {
use crate::basic::pack_bitarray;
let mut buf = [10; 3];
let mut bools = [false; 16];
bools[1] = true;
bools[2] = true;
bools[11] = true;
pack_bitarray(&mut buf, &bools);
assert_eq!(buf[0], 0b0110_0000);
assert_eq!(buf[1], 0b0001_0000);
assert_eq!(buf[2], 10);
}
#[test]
fn test_pack_int2() {
use crate::basic::pack_int2;
let mut buf = [10; 3 * 2];
pack_int2(&mut buf, -3);
pack_int2(&mut buf[2..], 2);
pack_int2(&mut buf[4..], -1);
assert_eq!(buf[0..2], [0xFF, 0xFD]);
assert_eq!(buf[2..4], [0x00, 0x02]);
assert_eq!(buf[4..6], [0xFF, 0xFF]);
}
#[test]
fn test_pack_int4() {
use crate::basic::pack_int4;
let mut buf = [10; 3 * 4];
pack_int4(&mut buf, -3);
pack_int4(&mut buf[4..], 2);
pack_int4(&mut buf[8..], -1);
assert_eq!(buf[0..4], [0xFF, 0xFF, 0xFF, 0xFD]);
assert_eq!(buf[4..8], [0x00, 0x00, 0x00, 0x02]);
assert_eq!(buf[8..12], [0xFF, 0xFF, 0xFF, 0xFF]);
}
#[test]
fn test_encode_real8() {
use crate::basic::{encode_real8, decode_real8};
const REALS: [f64; 5] = [1.0, -2.0, 1e-9, 1e-3, 1e-12];
for vv in REALS {
print!("{vv}\n");
assert!((decode_real8(encode_real8(vv).unwrap()) - vv).abs() < f64::EPSILON);
}
}
#[test]
fn test_pack_real8() {
use crate::basic::{pack_real8, parse_real8};
const COUNT: usize = 7;
const REALS: [f64; COUNT] = [0.0, 1.0, -1.0, 0.5, 1e-9, 1e-3, 1e-12];
let mut buf = [10; 8 * COUNT];
for (ii, &vv) in REALS.iter().enumerate() {
pack_real8(&mut buf[ii * 8..], vv).unwrap();
}
for (ii, &vv) in REALS.iter().enumerate() {
print!("{vv}\n");
let parsed_val = parse_real8(&buf[ii * 8..]).unwrap().1;
assert!((parsed_val - vv).abs() < f64::EPSILON);
}
}
#[test]
fn test_pack_ascii() {
use crate::basic::pack_ascii;
let mut buf = [10; 12];
pack_ascii(&mut buf[0..], "4321".as_bytes());
pack_ascii(&mut buf[6..], "321".as_bytes());
assert_eq!(&buf[0..4], "4321".as_bytes());
assert_eq!(&buf[4..6], [10, 10]);
assert_eq!(&buf[6..9], "321".as_bytes());
assert_eq!(&buf[9..], [0, 10, 10]);
}
}

379
src/bin/pheon.rs Normal file
View file

@ -0,0 +1,379 @@
use pheon::review::report::{ReportInputs, ReportOptions, failure_report};
use pheon::review::{
Comparison, DisplayFilter, Document, Scope, dump_records, hierarchy, parse_layer,
parse_layer_name, write_layer_names,
};
use std::{
collections::BTreeMap,
ffi::OsString,
fs,
io::{self, Write},
path::PathBuf,
process::ExitCode,
};
const HELP: &str = "Usage:
pheon dump FILE [--records]
pheon diff OLD NEW [--top NAME] [--cell NAME] [--layer L/T]\n [--show-cell NAME] [--show-layer L/T] [--hierarchy ascii|mermaid|none|graph]\n [--max-changes N] [--depth N] [--format text|json]
pheon hierarchy FILE [--format ascii|mermaid] [--depth N] [--top NAME]\n pheon git-diff [OPTIONS] -- PATH OLD OLD_OID OLD_MODE NEW NEW_OID NEW_MODE
pheon name-layers INPUT OUTPUT --cell NAME --carrier L/T --name L:T:NAME ...
Object profile: ignore timestamps and element order, normalize polygon start/winding,
preserve duplicates, compare all retained fields exactly. No geometric XOR or tolerance.
Exit status: 0 equal/success, 1 different, 2 invalid/unsupported/incomplete.
";
fn value(args: &[OsString], index: &mut usize) -> Result<String, String> {
*index += 1;
args.get(*index)
.and_then(|s| s.to_str())
.map(str::to_owned)
.ok_or_else(|| "missing UTF-8 option value".to_string())
}
fn name_layers(args: &[OsString]) -> Result<u8, String> {
if args.len() < 2 {
return Err(HELP.to_string());
}
let (mut cell, mut carrier) = (None, None);
let mut names = BTreeMap::new();
let mut i = 2;
while i < args.len() {
match args[i].to_str() {
Some("--cell") => cell = Some(value(args, &mut i)?),
Some("--carrier") => carrier = Some(parse_layer(&value(args, &mut i)?)?),
Some("--name") => {
let (pair, name) = parse_layer_name(&value(args, &mut i)?)?;
if names.get(&pair).is_some_and(|old| old != &name) {
return Err("conflicting --name arguments".to_string());
}
names.insert(pair, name);
}
_ => {
return Err(
"name-layers requires --cell, --carrier and --name arguments".to_string(),
);
}
}
i += 1;
}
if names.is_empty() {
return Err("provide at least one --name".to_string());
}
let input = fs::read(&args[0]).map_err(|e| format!("{:?}: {e}", args[0]))?;
let output = write_layer_names(
&input,
&cell.ok_or("provide --cell")?,
carrier.ok_or("provide --carrier")?,
&names,
)?;
let mut file = fs::OpenOptions::new()
.write(true)
.create_new(true)
.open(&args[1])
.map_err(|e| format!("{:?}: {e}", args[1]))?;
if let Err(e) = file.write_all(&output).and_then(|_| file.sync_all()) {
drop(file);
let _ = fs::remove_file(&args[1]);
return Err(e.to_string());
}
Ok(0)
}
fn stdout(output: &str) -> Result<(), String> {
io::stdout()
.lock()
.write_all(output.as_bytes())
.map_err(|e| e.to_string())
}
fn json_stdout(report: &serde_json::Value) -> Result<(), String> {
let mut output = io::stdout().lock();
serde_json::to_writer_pretty(&mut output, report).map_err(|e| e.to_string())?;
output.write_all(b"\n").map_err(|e| e.to_string())
}
fn json_diff(paths: &[OsString], scope: &Scope, options: &ReportOptions) -> Result<u8, String> {
let inputs = ReportInputs {
old: Some(PathBuf::from(&paths[0])),
new: Some(PathBuf::from(&paths[1])),
};
let comparison = || {
let read = |index, side| {
fs::read(&paths[index])
.map_err(|e| ("io_error", format!("{:?}: {e}", paths[index]), Some(side)))
};
let old_bytes = read(0, "old")?;
let new_bytes = read(1, "new")?;
let old = Document::parse(&old_bytes).map_err(|e| ("invalid_input", e, Some("old")))?;
let new = Document::parse(&new_bytes).map_err(|e| ("invalid_input", e, Some("new")))?;
scope
.validate_pair(&old, &new)
.map_err(|e| ("invalid_scope", e, None))?;
let old = old
.selected(scope, true)
.map_err(|e| ("invalid_scope", e, Some("old")))?;
let new = new
.selected(scope, true)
.map_err(|e| ("invalid_scope", e, Some("new")))?;
let diff = Comparison::between(&old, &new);
Ok::<_, (&str, String, Option<&str>)>((
diff.json_report(&old, &new, &inputs, options),
diff.exit_code(),
))
};
let (report, status) = match comparison() {
Ok(result) => result,
Err((code, message, side)) => (
failure_report(&inputs, scope, options, code, &message, side),
2,
),
};
if let Err(error) = json_stdout(&report) {
// Once output starts, never append a second report on a write failure.
eprintln!("pheon: {error}");
return Ok(2);
}
Ok(status)
}
fn run(args: &[OsString]) -> Result<u8, String> {
if args.len() == 1 && matches!(args[0].to_str(), Some("--version" | "-V")) {
stdout(&format!("pheon {}\n", env!("CARGO_PKG_VERSION")))?;
return Ok(0);
}
if args.is_empty() || args[0] == "--help" || args[0] == "-h" {
stdout(HELP)?;
return Ok(0);
}
let command = args[0].to_str().ok_or("invalid command")?;
if command == "name-layers" {
return name_layers(&args[1..]);
}
let expected = match command {
"dump" | "hierarchy" => 1,
"diff" => 2,
"git-diff" => 7,
_ => return Err(HELP.to_string()),
};
let compare = command == "diff" || command == "git-diff";
let mut scope = Scope::default();
let mut show = DisplayFilter::default();
let mut raw = false;
let mut format = if compare {
"none".to_string()
} else {
"ascii".to_string()
};
let mut max_changes = 100;
let mut explicit_max = None;
let mut output_format = "text".to_string();
let mut depth = 8;
let mut explicit_depth = false;
let mut paths = Vec::new();
let mut literal = false;
let mut i = 1;
while i < args.len() {
if literal {
paths.push(args[i].clone());
i += 1;
continue;
}
match args[i].to_str() {
Some("--") => literal = true,
Some("--help" | "-h") => {
stdout(HELP)?;
return Ok(0);
}
Some("--records") if command == "dump" => raw = true,
Some("--top") => scope.top = Some(value(args, &mut i)?),
Some("--cell") => scope.cell = Some(value(args, &mut i)?),
Some("--layer") => scope.layer = Some(parse_layer(&value(args, &mut i)?)?),
Some("--show-cell") if compare => show.cell = Some(value(args, &mut i)?),
Some("--show-layer") if compare => {
show.layer = Some(parse_layer(&value(args, &mut i)?)?)
}
Some("--hierarchy") if compare => format = value(args, &mut i)?,
Some("--format") if command == "hierarchy" => format = value(args, &mut i)?,
Some("--format") if command == "diff" => output_format = value(args, &mut i)?,
Some("--max-changes") if compare => {
max_changes = value(args, &mut i)?
.parse()
.map_err(|_| "invalid change limit")?;
explicit_max = Some(max_changes);
}
Some("--depth") if command != "dump" => {
depth = value(args, &mut i)?.parse().map_err(|_| "invalid depth")?;
explicit_depth = true;
}
Some(s) if s.starts_with('-') => return Err(format!("unknown option {s:?}")),
_ => paths.push(args[i].clone()),
}
i += 1;
}
if paths.len() != expected && !(command == "git-diff" && paths.len() == 9) {
return Err(HELP.to_string());
}
if !matches!(output_format.as_str(), "text" | "json") {
return Err("diff format must be text or json".to_string());
}
if output_format == "json" {
if !matches!(format.as_str(), "none" | "graph") || explicit_depth {
return Err(
"JSON accepts --hierarchy graph|none; --depth applies to text output".to_string(),
);
}
return json_diff(
&paths,
&scope,
&ReportOptions {
filter: show,
max_objects: explicit_max,
hierarchy: format == "graph",
},
);
}
if !matches!(format.as_str(), "ascii" | "mermaid" | "none")
|| (command == "hierarchy" && format == "none")
{
return Err("format must be ascii or mermaid (diff also accepts none)".to_string());
}
let read = |path: &OsString| fs::read(path).map_err(|e| format!("{path:?}: {e}"));
if raw {
if scope != Scope::default() {
return Err("record dumps do not accept object scope selectors".to_string());
}
stdout(&dump_records(&read(&paths[0])?)?)?;
return Ok(0);
}
if !compare {
let bytes = read(&paths[0])?;
let doc = Document::parse(&bytes)?.selected(&scope, false)?;
let output = if command == "dump" {
doc.canonical_dump()
} else {
hierarchy(&doc, &doc, None, format == "mermaid", depth)
};
stdout(&output)?;
if command == "hierarchy" {
for issue in &doc.issues {
eprintln!("INCOMPLETE: {issue}");
}
}
return Ok(if doc.issues.is_empty() { 0 } else { 2 });
}
let git = command == "git-diff";
let old_missing = git && (paths[3] == "000000" || paths[3] == ".");
let new_missing = git && (paths[6] == "000000" || paths[6] == ".");
if old_missing && new_missing {
return Err("both Git revisions are absent".to_string());
}
let old_bytes = if old_missing {
None
} else {
Some(read(&paths[if git { 1 } else { 0 }])?)
};
let new_bytes = if new_missing {
None
} else {
Some(read(&paths[if git { 4 } else { 1 }])?)
};
let old = old_bytes.as_ref().map(|b| Document::parse(b)).transpose()?;
let new = new_bytes.as_ref().map(|b| Document::parse(b)).transpose()?;
let old = old.unwrap_or_else(|| new.as_ref().unwrap().empty_like());
let new = new.unwrap_or_else(|| old.empty_like());
scope.validate_pair(&old, &new)?;
let old = old.selected(&scope, true)?;
let new = new.selected(&scope, true)?;
let diff = Comparison::between(&old, &new);
let mut output = String::new();
if git {
if paths.len() == 9 {
output.push_str(&format!("layout {:?} -> {:?}\n", paths[0], paths[7]));
} else {
output.push_str(&format!("layout {:?}\n", paths[0]));
}
if old_missing {
output.push_str("layout added\n");
} else if new_missing {
output.push_str("layout deleted\n");
}
if paths[3] != paths[6] {
output.push_str(&format!("mode {:?} -> {:?}\n", paths[3], paths[6]));
}
}
output.push_str(&diff.text_filtered(max_changes, &show));
if format != "none" {
if format == "mermaid" {
output.push_str("```mermaid\n");
}
output.push_str(&hierarchy(
&old,
&new,
Some(&diff),
format == "mermaid",
depth,
));
if format == "mermaid" {
output.push_str("```\n");
}
}
stdout(&output)?;
// Git's external driver expects successful rendering for either equality
// or differences. Actual comparison/input failures remain nonzero.
Ok(if git && diff.exit_code() == 1 {
0
} else {
diff.exit_code()
})
}
fn wants_json(args: &[OsString]) -> bool {
if args.first().is_none_or(|arg| arg != "diff") {
return false;
}
let mut format = None;
let mut i = 1;
while i < args.len() {
match args[i].to_str() {
Some("--") => break,
Some("--format") => {
format = args.get(i + 1).and_then(|v| v.to_str());
i += 1;
}
Some(
"--top" | "--cell" | "--layer" | "--show-cell" | "--show-layer" | "--hierarchy"
| "--max-changes" | "--depth",
) => i += 1,
_ => (),
}
i += 1;
}
format == Some("json")
}
fn main() -> ExitCode {
let args: Vec<_> = std::env::args_os().skip(1).collect();
let json = wants_json(&args);
match run(&args) {
Ok(status) => ExitCode::from(status),
Err(message) => {
if json {
let report = failure_report(
&ReportInputs::default(),
&Scope::default(),
&ReportOptions::default(),
"command_error",
&message,
None,
);
if let Err(error) = json_stdout(&report) {
eprintln!("pheon: {error}");
}
} else {
eprintln!("pheon: {message}");
}
ExitCode::from(2)
}
}
}

1004
src/elements.rs Normal file

File diff suppressed because it is too large Load diff

307
src/lib.rs Normal file
View file

@ -0,0 +1,307 @@
pub mod basic;
pub mod record;
pub mod records;
pub mod elements;
pub mod library;
pub mod review;
use crate::library::{read_library, read_selected_library, scan_library};
use std::any::Any;
use std::cell::RefCell;
use std::mem::size_of;
use std::ffi::{CStr, CString, c_char};
use std::panic::{AssertUnwindSafe, catch_unwind};
use std::path::{Path, PathBuf};
use std::ptr;
#[cfg(unix)]
use std::os::unix::ffi::OsStringExt;
use std::str;
use std::fs;
use std::slice;
use arrow_array::ffi::{to_ffi, FFI_ArrowArray, FFI_ArrowSchema};
use arrow_array::{Array, StructArray};
thread_local! {
static LAST_ERROR: RefCell<Option<CString>> = const { RefCell::new(None) };
}
/// Stable C API version. Increment for incompatible function/ownership changes.
#[unsafe(no_mangle)]
pub extern "C" fn pheon_abi_version() -> u32 {
1
}
/// Layout of the full-read and hierarchy-scan Arrow payloads.
#[unsafe(no_mangle)]
pub extern "C" fn pheon_arrow_schema_version() -> u32 {
1
}
fn clear_last_error() {
LAST_ERROR.with(|last_error| *last_error.borrow_mut() = None);
}
fn set_last_error(message: String) -> i32 {
let sanitized = message.replace('\0', "\\0");
LAST_ERROR.with(|last_error| *last_error.borrow_mut() = Some(CString::new(sanitized).unwrap()));
-1
}
fn describe_panic(payload: Box<dyn Any + Send>) -> String {
if let Some(message) = payload.downcast_ref::<&str>() {
(*message).to_string()
} else if let Some(message) = payload.downcast_ref::<String>() {
message.clone()
} else {
"unexpected panic".to_string()
}
}
fn describe_parse_error(input: &[u8], (remaining, reason): (&[u8], crate::basic::ErrType)) -> String {
// Selected-cell errors may contain a bounded range rather than a file suffix.
let offset = remaining.as_ptr() as usize - input.as_ptr() as usize;
let message = match reason {
crate::basic::ErrType::Failed(message) => message,
crate::basic::ErrType::Incomplete(Some(size)) => format!("Unexpected end of input; need {size} bytes"),
crate::basic::ErrType::Incomplete(None) => "Unexpected end of input".to_string(),
};
format!("at byte {offset}: {message}")
}
fn export_struct_array(
op_name: &str,
arr: *mut FFI_ArrowArray,
schema: *mut FFI_ArrowSchema,
build: impl FnOnce() -> Result<StructArray, String>,
) -> i32 {
clear_last_error();
if arr.is_null() || schema.is_null() {
return set_last_error(format!("{op_name}: Arrow output pointer was null"));
}
match catch_unwind(AssertUnwindSafe(build)) {
Ok(Ok(struct_arr)) => match to_ffi(&struct_arr.to_data()) {
Ok((ffi_arr, ffi_schema)) => {
unsafe {
*arr = ffi_arr;
*schema = ffi_schema;
}
0
},
Err(err) => set_last_error(format!("{op_name}: failed to export Arrow data: {err}")),
},
Ok(Err(message)) => set_last_error(format!("{op_name}: {message}")),
Err(payload) => set_last_error(format!("{op_name}: {}", describe_panic(payload))),
}
}
fn pathbuf_from_c(cpath: *const c_char) -> Result<PathBuf, String> {
if cpath.is_null() {
return Err("path pointer was null".to_string());
}
let cstr = unsafe { CStr::from_ptr(cpath) };
#[cfg(unix)]
{
return Ok(PathBuf::from(std::ffi::OsString::from_vec(cstr.to_bytes().to_vec())));
}
#[cfg(windows)]
{
let ustr = str::from_utf8(cstr.to_bytes())
.map_err(|_| "Non-UTF8 paths are not supported".to_string())?;
return Ok(PathBuf::from(ustr));
}
#[allow(unreachable_code)]
Err("Unsupported OS".to_string())
}
/// Return this thread's last parser error, or null if its last call succeeded.
/// The returned string remains valid until this thread's next parser call or exit.
#[unsafe(no_mangle)]
pub extern "C" fn last_error_message() -> *const c_char {
LAST_ERROR.with(|last_error| last_error.borrow()
.as_ref()
.map_or(ptr::null(), |message| message.as_ptr()))
}
/// Read a GDSII file and export its Arrow data. Return 0 on success or -1 on error.
///
/// # Safety
/// `cpath` must point to a null-terminated path. Non-null output pointers must
/// point to initialized, writable Arrow FFI structures. Null pointers are rejected.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn read_path(
cpath: *const c_char,
arr: *mut FFI_ArrowArray,
schema: *mut FFI_ArrowSchema,
) -> i32 {
export_struct_array("read_path", arr, schema, || {
let path = pathbuf_from_c(cpath)?;
let input = fs::read(Path::new(&path))
.map_err(|err| format!("file read failed for {}: {err}", path.display()))?;
let (_input, struct_arr) = read_library(&input)
.map_err(|err| format!("read failed {}", describe_parse_error(&input, err)))?;
Ok(struct_arr)
})
}
unsafe fn input_slice<'a, T>(data: *const T, size: usize, name: &str) -> Result<&'a [T], String> {
if size == 0 {
return Ok(&[]);
}
if data.is_null() {
return Err(format!("{name} pointer was null"));
}
if !data.is_aligned() {
return Err(format!("{name} pointer was misaligned"));
}
if size.checked_mul(size_of::<T>()).is_none_or(|bytes| bytes > isize::MAX as usize) {
return Err(format!("{name} length is too large"));
}
Ok(unsafe { slice::from_raw_parts(data, size) })
}
/// Read a borrowed GDSII buffer, exporting Arrow data that owns its storage.
///
/// # Safety
/// `data` must cover `size` readable bytes (null is allowed for zero bytes).
/// Outputs must be initialized Arrow FFI structures, as for `read_path`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn read_bytes(
data: *const u8,
size: usize,
arr: *mut FFI_ArrowArray,
schema: *mut FFI_ArrowSchema,
) -> i32 {
export_struct_array("read_bytes", arr, schema, || {
let input = unsafe { input_slice(data, size, "data") }?;
let (_input, struct_arr) = read_library(input)
.map_err(|err| format!("read failed {}", describe_parse_error(input, err)))?;
Ok(struct_arr)
})
}
/// Scan cell ranges and hierarchy references. Return 0 on success or -1 on error.
///
/// # Safety
/// Non-null `data` must point to `size` readable bytes. Null is allowed when
/// `size` is zero. Output pointers have the same requirements as `read_path`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn scan_bytes(
data: *const u8,
size: usize,
arr: *mut FFI_ArrowArray,
schema: *mut FFI_ArrowSchema,
) -> i32 {
export_struct_array("scan_bytes", arr, schema, || {
let input = unsafe { input_slice(data, size, "data") }?;
let (_input, struct_arr) = scan_library(input)
.map_err(|err| format!("scan failed {}", describe_parse_error(input, err)))?;
Ok(struct_arr)
})
}
/// Read the cells in half-open byte ranges. Return 0 on success or -1 on error.
///
/// # Safety
/// `data` has the same requirements as `scan_bytes`. Non-null `ranges` must
/// point to `range_count * 2` readable, aligned u64 values, alternating start
/// and end offsets. Null is allowed for zero ranges. Output pointers have the
/// same requirements as `read_path`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn read_cells_bytes(
data: *const u8,
size: usize,
ranges: *const u64,
range_count: usize,
arr: *mut FFI_ArrowArray,
schema: *mut FFI_ArrowSchema,
) -> i32 {
export_struct_array("read_cells_bytes", arr, schema, || {
let input = unsafe { input_slice(data, size, "data") }?;
let range_size = range_count.checked_mul(2)
.ok_or_else(|| "range count is too large".to_string())?;
let flat_ranges = unsafe { input_slice(ranges, range_size, "ranges") }?;
let cell_ranges: Vec<(u64, u64)> = flat_ranges
.as_chunks::<2>()
.0
.iter()
.map(|&[start, end]| (start, end))
.collect();
let (_input, struct_arr) = read_selected_library(input, &cell_ranges)
.map_err(|err| format!("selected-cell read failed {}", describe_parse_error(input, err)))?;
Ok(struct_arr)
})
}
macro_rules! impl_intbe {
($source:ty, $target:ty, $arr:ident, $size:ident) => {{
let values = unsafe { std::slice::from_raw_parts_mut($arr, $size) };
for value in values {
// All source types represent the target bounds exactly in f64.
let number = *value as f64;
if !number.is_finite()
|| number < <$target>::MIN as f64
|| number > <$target>::MAX as f64
{
return *value;
}
let mut slot = [0; size_of::<$source>()];
slot[..size_of::<$target>()].copy_from_slice(&(*value as $target).to_be_bytes());
*value = <$source>::from_ne_bytes(slot);
}
0 as $source
}};
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn f64_to_i16(arr: *mut f64, size: usize) -> f64 { impl_intbe!(f64, i16, arr, size) }
#[unsafe(no_mangle)]
pub unsafe extern "C" fn f64_to_i32(arr: *mut f64, size: usize) -> f64 { impl_intbe!(f64, i32, arr, size) }
#[unsafe(no_mangle)]
pub unsafe extern "C" fn f32_to_i16(arr: *mut f32, size: usize) -> f32 { impl_intbe!(f32, i16, arr, size) }
#[unsafe(no_mangle)]
pub unsafe extern "C" fn f32_to_i32(arr: *mut f32, size: usize) -> f32 { impl_intbe!(f32, i32, arr, size) }
#[unsafe(no_mangle)]
pub unsafe extern "C" fn u64_to_i16(arr: *mut u64, size: usize) -> u64 { impl_intbe!(u64, i16, arr, size) }
#[unsafe(no_mangle)]
pub unsafe extern "C" fn u64_to_i32(arr: *mut u64, size: usize) -> u64 { impl_intbe!(u64, i32, arr, size) }
#[unsafe(no_mangle)]
pub unsafe extern "C" fn i64_to_i16(arr: *mut i64, size: usize) -> i64 { impl_intbe!(i64, i16, arr, size) }
#[unsafe(no_mangle)]
pub unsafe extern "C" fn i64_to_i32(arr: *mut i64, size: usize) -> i64 { impl_intbe!(i64, i32, arr, size) }
#[unsafe(no_mangle)]
pub unsafe extern "C" fn u32_to_i16(arr: *mut u32, size: usize) -> u32 { impl_intbe!(u32, i16, arr, size) }
#[unsafe(no_mangle)]
pub unsafe extern "C" fn u32_to_i32(arr: *mut u32, size: usize) -> u32 { impl_intbe!(u32, i32, arr, size) }
#[unsafe(no_mangle)]
pub unsafe extern "C" fn i32_to_i16(arr: *mut i32, size: usize) -> i32 { impl_intbe!(i32, i16, arr, size) }
#[unsafe(no_mangle)]
pub unsafe extern "C" fn i32_to_i32(arr: *mut i32, size: usize) -> i32 { impl_intbe!(i32, i32, arr, size) }
#[unsafe(no_mangle)]
pub unsafe extern "C" fn u16_to_i16(arr: *mut u16, size: usize) -> u16 { impl_intbe!(u16, i16, arr, size) }
#[unsafe(no_mangle)]
pub unsafe extern "C" fn i16_to_i16(arr: *mut i16, size: usize) -> i16 { impl_intbe!(i16, i16, arr, size) }

588
src/library.rs Normal file
View file

@ -0,0 +1,588 @@
///
/// File-level read/write functionality.
///
use std::io::Write;
//use std::collections::HashMap;
pub use crate::record;
pub use crate::record::{RecordHeader, Record};
pub use crate::records;
pub use crate::elements;
pub use crate::elements::{read_elements, scan_elements, DListBuilder, FListBuilder, ScanRefRecord};
pub use crate::basic::{IResult, OResult, take_bytes, fail};
use std::string::String;
use std::collections::HashMap;
use std::sync::Arc;
use arrow_schema::{DataType, Field, Fields};
use arrow_array::{
StructArray,
builder::{
BooleanBuilder, Float64Builder, Int16Builder, StringBuilder, StructBuilder,
UInt32Builder, UInt64Builder,
},
};
///
/// Representation of the GDS file header.
///
/// File header records: HEADER BGNLIB LIBNAME UNITS
/// Optional records are ignored if present and never written.
///
/// Version is written as `600`.
///
#[derive(Debug, Clone)]
pub struct FileHeader {
/// Number of user units in one database unit
user_units_per_db_unit: f64,
/// Number of meters in one database unit
meters_per_db_unit: f64,
/// Last-modified time [y, m, d, h, m, s]
mod_time: [i16; 6],
/// Last-accessed time [y, m, d, h, m, s]
acc_time: [i16; 6],
/// Library name
name: Vec<u8>,
}
impl FileHeader {
pub fn new(name: &[u8], meters_per_db_unit: f64, user_units_per_db_unit: f64) -> Self {
FileHeader{
mod_time: [0, 1, 1, 0, 0, 0],
acc_time: [0, 1, 1, 0, 0, 0],
name: name.to_owned(),
user_units_per_db_unit,
meters_per_db_unit,
}
}
/// Read and construct a header from the provided input.
///
/// Args:
/// input: Seekable input to read from
///
/// Returns:
/// FileHeader object
///
pub fn read(input: &[u8]) -> IResult<'_, Self> {
let (input, _version) = records::HEADER::read(input)?;
let (input, [mod_time, acc_time]) = records::BGNLIB::read(input)?;
let (input, name) = records::LIBNAME::skip_and_read(input)?;
let (input, (uu, dbu)) = records::UNITS::skip_and_read(input)?;
Ok((input, FileHeader{
mod_time,
acc_time,
name,
user_units_per_db_unit: uu,
meters_per_db_unit: dbu,
}))
}
/// Write the header to a input
///
/// Args:
/// input: input to write to
///
/// Returns:
/// number of bytes written
///
pub fn write<W: Write>(&self, ww: &mut W) -> OResult {
let mut size = 0;
size += records::HEADER::write(ww, &600)?;
size += records::BGNLIB::write(ww, &[self.mod_time, self.acc_time])?;
size += records::LIBNAME::write(ww, &self.name)?;
size += records::UNITS::write(ww, &(self.user_units_per_db_unit, self.meters_per_db_unit))?;
Ok(size)
}
}
fn make_name_list_type() -> DataType {
DataType::List(Arc::new(
Field::new_list_field(DataType::Utf8, false)
))
}
fn make_time_type() -> DataType {
DataType::FixedSizeList(Arc::new(
Field::new_list_field(DataType::Int16, false),
),
6,
)
}
fn make_top_level_builder(cells_field: Field, extra_fields: Vec<Field>) -> StructBuilder {
let mut fields = vec![
Field::new("meters_per_db_unit", DataType::Float64, false),
Field::new("user_units_per_db_unit", DataType::Float64, false),
Field::new("lib_name", DataType::Utf8, false),
Field::new("mod_time", make_time_type(), false),
Field::new("acc_time", make_time_type(), false),
cells_field,
Field::new("cell_names", make_name_list_type(), false),
];
fields.extend(extra_fields);
StructBuilder::from_fields(fields, 0)
}
fn reference_type(is_array: bool, properties: Option<&DataType>) -> DataType {
let mut fields = vec![
Field::new("target", DataType::UInt32, false),
Field::new("invert_y", DataType::Boolean, false),
Field::new("scale", DataType::Float64, false),
Field::new("angle_rad", DataType::Float64, false),
Field::new("xy", DataType::UInt64, false),
];
if is_array {
fields.extend([
Field::new("xy0", DataType::UInt64, false),
Field::new("xy1", DataType::UInt64, false),
Field::new("counts", DataType::UInt32, false),
]);
}
if let Some(properties) = properties {
fields.push(Field::new("properties", properties.clone(), true));
}
DataType::Struct(Fields::from(fields))
}
fn make_library_builder() -> StructBuilder {
let property_t = DataType::Struct(Fields::from(vec![
Field::new("key", DataType::Int16, false),
Field::new("value", DataType::Utf8, false),
]));
let property_list_t = DataType::List(Arc::new(
Field::new_list_field(property_t, false)
));
let sref_struct_t = reference_type(false, None);
let aref_struct_t = reference_type(true, None);
let sref_prop_struct_t = reference_type(false, Some(&property_list_t));
let aref_prop_struct_t = reference_type(true, Some(&property_list_t));
let text_struct_t = DataType::Struct(Fields::from(vec![
Field::new("layer", DataType::UInt32, false),
Field::new("presentation_horiz", DataType::UInt8, true),
Field::new("presentation_vert", DataType::UInt8, true),
Field::new("presentation_font", DataType::UInt8, true),
Field::new("path_type", DataType::Int16, true),
Field::new("width", DataType::Int32, true),
Field::new("invert_y", DataType::Boolean, true),
Field::new("mag", DataType::Float64, true),
Field::new("angle_deg", DataType::Float64, true),
Field::new("xy", DataType::UInt64, false),
Field::new("string", DataType::Utf8, false),
Field::new("properties", property_list_t.clone(), true),
]));
let coords_t = DataType::List(Arc::new(
Field::new_list_field(DataType::Int32, false)
));
let boundary_batch_struct_t = DataType::Struct(Fields::from(vec![
Field::new("layer", DataType::UInt32, false),
Field::new("vertices", coords_t.clone(), false),
Field::new(
"vertex_offsets",
DataType::List(Arc::new(Field::new_list_field(DataType::UInt32, false))),
false,
),
]));
let rect_batch_struct_t = DataType::Struct(Fields::from(vec![
Field::new("layer", DataType::UInt32, false),
Field::new("rects", coords_t.clone(), false),
]));
let boundary_prop_struct_t = DataType::Struct(Fields::from(vec![
Field::new("layer", DataType::UInt32, false),
Field::new("vertices", coords_t.clone(), false),
Field::new("properties", property_list_t.clone(), true),
]));
let path_struct_t = DataType::Struct(Fields::from(vec![
Field::new("layer", DataType::UInt32, false),
Field::new("path_type", DataType::Int16, true),
Field::new("extension_start", DataType::Int32, true),
Field::new("extension_end", DataType::Int32, true),
Field::new("width", DataType::Int32, true),
Field::new("xy", coords_t.clone(), false),
Field::new("properties", property_list_t.clone(), true),
]));
let boxnode_struct_t = DataType::Struct(Fields::from(vec![
Field::new("layer", DataType::UInt32, false),
Field::new("xy", coords_t.clone(), false),
Field::new("properties", property_list_t.clone(), true),
]));
let sref_list_t = DataType::List(Arc::new(
Field::new_list_field(sref_struct_t, false)
));
let aref_list_t = DataType::List(Arc::new(
Field::new_list_field(aref_struct_t, false)
));
let sref_prop_list_t = DataType::List(Arc::new(
Field::new_list_field(sref_prop_struct_t, false)
));
let aref_prop_list_t = DataType::List(Arc::new(
Field::new_list_field(aref_prop_struct_t, false)
));
let text_list_t = DataType::List(Arc::new(
Field::new_list_field(text_struct_t, false)
));
let boundary_batch_list_t = DataType::List(Arc::new(
Field::new_list_field(boundary_batch_struct_t, false)
));
let rect_batch_list_t = DataType::List(Arc::new(
Field::new_list_field(rect_batch_struct_t, false)
));
let boundary_prop_list_t = DataType::List(Arc::new(
Field::new_list_field(boundary_prop_struct_t, false)
));
let path_list_t = DataType::List(Arc::new(
Field::new_list_field(path_struct_t, false)
));
let boxnode_list_t = DataType::List(Arc::new(
Field::new_list_field(boxnode_struct_t, false)
));
let layer_list_t = DataType::List(Arc::new(
Field::new_list_field(DataType::UInt32, false)
));
let cell_struct_t = DataType::Struct(Fields::from(vec![
Field::new("id", DataType::UInt32, false),
Field::new("file_offset", DataType::UInt64, false),
Field::new("srefs", sref_list_t, false),
Field::new("arefs", aref_list_t, false),
Field::new("sref_props", sref_prop_list_t, false),
Field::new("aref_props", aref_prop_list_t, false),
Field::new("rect_batches", rect_batch_list_t, false),
Field::new("boundary_batches", boundary_batch_list_t, false),
Field::new("boundary_props", boundary_prop_list_t, false),
Field::new("paths", path_list_t, false),
Field::new("nodes", boxnode_list_t.clone(), true),
Field::new("boxes", boxnode_list_t.clone(), true),
Field::new("texts", text_list_t, false),
]));
let cells_list_t = DataType::List(Arc::new(
Field::new_list_field(cell_struct_t, false)
));
make_top_level_builder(
Field::new("cells", cells_list_t, false),
vec![
Field::new("layers", layer_list_t, false),
],
)
}
fn make_scan_builder() -> StructBuilder {
let scan_ref_struct_t = reference_type(true, None);
let scan_ref_list_t = DataType::List(Arc::new(
Field::new_list_field(scan_ref_struct_t, false)
));
let scan_cell_struct_t = DataType::Struct(Fields::from(vec![
Field::new("id", DataType::UInt32, false),
Field::new("struct_start_offset", DataType::UInt64, false),
Field::new("struct_end_offset", DataType::UInt64, false),
Field::new("refs", scan_ref_list_t, false),
]));
let scan_cells_list_t = DataType::List(Arc::new(
Field::new_list_field(scan_cell_struct_t, false)
));
make_top_level_builder(
Field::new("cells", scan_cells_list_t, false),
Vec::new(),
)
}
fn append_file_header(
lib_builder: &mut StructBuilder,
header: &FileHeader,
) {
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();
uu_builder.append_value(header.user_units_per_db_unit);
let libname_builder = lib_builder.field_builder::<StringBuilder>(2).unwrap();
libname_builder.append_value(String::from_utf8(header.name.clone()).unwrap());
let mtl_builder = lib_builder.field_builder::<FListBuilder>(3).unwrap();
let mt_builder = mtl_builder.values().as_any_mut().downcast_mut::<Int16Builder>().unwrap();
mt_builder.append_values(&header.mod_time, &[true; 6]);
mtl_builder.append(true);
let atl_builder = lib_builder.field_builder::<FListBuilder>(4).unwrap();
let at_builder = atl_builder.values().as_any_mut().downcast_mut::<Int16Builder>().unwrap();
at_builder.append_values(&header.acc_time, &[true; 6]);
atl_builder.append(true);
}
fn append_names(
lib_builder: &mut StructBuilder,
field_index: usize,
names: HashMap<String, u32>,
) {
// Interned IDs are consecutive, so a vector directly represents ID order.
let mut ordered_names = vec![String::new(); names.len()];
for (name, id) in names {
ordered_names[id as usize] = name;
}
let names_builder = lib_builder.field_builder::<DListBuilder>(field_index).unwrap();
let name_builder = names_builder.values().as_any_mut().downcast_mut::<StringBuilder>().unwrap();
for name in ordered_names {
name_builder.append_value(name);
}
names_builder.append(true);
}
fn append_layers(
lib_builder: &mut StructBuilder,
layers: HashMap<u32, u32>,
) {
let mut ordered_layers = vec![0; layers.len()];
for (layer, id) in layers {
ordered_layers[id as usize] = layer;
}
let layers_builder = lib_builder.field_builder::<DListBuilder>(7).unwrap();
let layer_builder = layers_builder.values().as_any_mut().downcast_mut::<UInt32Builder>().unwrap();
layer_builder.append_slice(&ordered_layers);
layers_builder.append(true);
}
fn append_scan_ref(
refs_builder: &mut DListBuilder,
scan_ref: ScanRefRecord,
) {
let ref_builder = refs_builder.values().as_any_mut().downcast_mut::<StructBuilder>().unwrap();
let target_builder = ref_builder.field_builder::<UInt32Builder>(0).unwrap();
target_builder.append_value(scan_ref.target_id);
let invert_builder = ref_builder.field_builder::<BooleanBuilder>(1).unwrap();
invert_builder.append_value(scan_ref.invert_y);
let scale_builder = ref_builder.field_builder::<Float64Builder>(2).unwrap();
scale_builder.append_value(scan_ref.scale);
let angle_builder = ref_builder.field_builder::<Float64Builder>(3).unwrap();
angle_builder.append_value(scan_ref.angle_rad);
let xy_builder = ref_builder.field_builder::<UInt64Builder>(4).unwrap();
xy_builder.append_value(scan_ref.xy);
let xy0_builder = ref_builder.field_builder::<UInt64Builder>(5).unwrap();
xy0_builder.append_value(scan_ref.xy0);
let xy1_builder = ref_builder.field_builder::<UInt64Builder>(6).unwrap();
xy1_builder.append_value(scan_ref.xy1);
let counts_builder = ref_builder.field_builder::<UInt32Builder>(7).unwrap();
counts_builder.append_value(scan_ref.counts);
ref_builder.append(true);
}
fn intern_name(names: &mut HashMap<String, u32>, name: String) -> u32 {
let next_id = names.len();
*names.entry(name).or_insert(next_id.try_into().unwrap())
}
fn append_full_cell<'a>(
cells_builder: &mut DListBuilder,
names: &mut HashMap<String, u32>,
layers: &mut HashMap<u32, u32>,
struct_input: &'a [u8],
id: u32,
file_offset: u64,
) -> IResult<'a, ()> {
let cell_builder = cells_builder.values().as_any_mut().downcast_mut::<StructBuilder>().unwrap();
let id_builder = cell_builder.field_builder::<UInt32Builder>(0).unwrap();
id_builder.append_value(id);
let offset_builder = cell_builder.field_builder::<UInt64Builder>(1).unwrap();
offset_builder.append_value(file_offset);
let (next_input, _) = read_elements(struct_input, cell_builder, names, layers)?;
cell_builder.append(true);
Ok((next_input, ()))
}
fn walk_structs<'a, F>(
input: &'a [u8],
base_offset: usize,
names: &mut HashMap<String, u32>,
mut on_struct: F,
) -> IResult<'a, ()>
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;
(input, _) = take_bytes(input, header.data_size)?;
if header.tag == records::RTAG_BGNSTR {
let name_bytes;
(input, name_bytes) = records::STRNAME::read(input)?;
let name = String::from_utf8(name_bytes).unwrap();
let id = intern_name(names, name);
let (next_input, ()) = on_struct(input, record_start, id, names)?;
input = next_input;
}
(input, header) = RecordHeader::read(input)?;
}
Ok((input, ()))
}
pub fn read_library(input: &[u8]) -> IResult<'_, StructArray> {
let full_input_size = input.len();
let mut lib_builder = make_library_builder();
let (input, header) = FileHeader::read(input)?;
append_file_header(&mut lib_builder, &header);
let base_offset = full_input_size - input.len();
let mut names = HashMap::<String, u32>::new();
let mut layers = HashMap::<u32, u32>::new();
let cells_builder = lib_builder.field_builder::<DListBuilder>(5).unwrap();
let walk_input_size = input.len();
let (input, ()) = walk_structs(input, base_offset, &mut names, |struct_input, _record_start, id, names| {
let position = base_offset + walk_input_size - struct_input.len();
append_full_cell(
cells_builder,
names,
&mut layers,
struct_input,
id,
position.try_into().unwrap(),
)
})?;
cells_builder.append(true);
append_names(&mut lib_builder, 6, names);
append_layers(&mut lib_builder, layers);
lib_builder.append(true);
let lib = lib_builder.finish();
Ok((input, lib))
}
/// Scan cell byte ranges and references without decoding geometry.
pub fn scan_library(input: &[u8]) -> IResult<'_, StructArray> {
let full_input_size = input.len();
let mut lib_builder = make_scan_builder();
let (input, header) = FileHeader::read(input)?;
append_file_header(&mut lib_builder, &header);
let base_offset = full_input_size - input.len();
let mut names = HashMap::<String, u32>::new();
let cells_builder = lib_builder.field_builder::<DListBuilder>(5).unwrap();
let walk_input_size = input.len();
let (input, ()) = walk_structs(input, base_offset, &mut names, |struct_input, record_start, id, names| {
let (after_cell, refs) = scan_elements(struct_input, names)?;
let struct_end = base_offset + walk_input_size - after_cell.len();
let cell_builder = cells_builder.values().as_any_mut().downcast_mut::<StructBuilder>().unwrap();
let id_builder = cell_builder.field_builder::<UInt32Builder>(0).unwrap();
id_builder.append_value(id);
let start_builder = cell_builder.field_builder::<UInt64Builder>(1).unwrap();
start_builder.append_value(record_start.try_into().unwrap());
let end_builder = cell_builder.field_builder::<UInt64Builder>(2).unwrap();
end_builder.append_value(struct_end.try_into().unwrap());
let refs_builder = cell_builder.field_builder::<DListBuilder>(3).unwrap();
for scan_ref in refs {
append_scan_ref(refs_builder, scan_ref);
}
refs_builder.append(true);
cell_builder.append(true);
Ok((after_cell, ()))
})?;
cells_builder.append(true);
append_names(&mut lib_builder, 6, names);
lib_builder.append(true);
let lib = lib_builder.finish();
Ok((input, lib))
}
/// Read exact half-open cell ranges returned by `scan_library`, in caller order.
/// Each range includes BGNSTR through ENDSTR. Cell and layer IDs are local to
/// this result. This random-access operation returns the original input slice
/// as its remainder; it does not advance a sequential parsing cursor.
pub fn read_selected_library<'a>(input: &'a [u8], ranges: &[(u64, u64)]) -> IResult<'a, StructArray> {
let mut lib_builder = make_library_builder();
let (_, header) = FileHeader::read(input)?;
append_file_header(&mut lib_builder, &header);
let mut names = HashMap::<String, u32>::new();
let mut layers = HashMap::<u32, u32>::new();
let cells_builder = lib_builder.field_builder::<DListBuilder>(5).unwrap();
for (start_u64, end_u64) in ranges {
if start_u64 >= end_u64 || *end_u64 > input.len() as u64 {
return fail(input, format!("Cell byte range out of bounds: [{start_u64}, {end_u64})"));
}
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 = String::from_utf8(name_bytes).unwrap();
let file_offset = end - struct_input.len();
let id = intern_name(&mut names, name);
let (remaining_struct, _) = append_full_cell(
cells_builder,
&mut names,
&mut layers,
struct_input,
id,
file_offset.try_into().unwrap(),
)?;
if !remaining_struct.is_empty() {
return fail(remaining_struct, "Cell byte range extends past ENDSTR".to_string());
}
}
cells_builder.append(true);
append_names(&mut lib_builder, 6, names);
append_layers(&mut lib_builder, layers);
lib_builder.append(true);
let lib = lib_builder.finish();
Ok((input, lib))
}

39
src/misc.py Normal file
View file

@ -0,0 +1,39 @@
'''
https://github.com/apache/arrow/blob/main/python/pyarrow/tests/test_cffi.py
'''
# -*- coding: utf-8 -*-
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
import ctypes
import pyarrow as pa
from pyarrow.cffi import ffi
c_schema = ffi.new('struct ArrowSchema*')
ptr_schema = int(ffi.cast('uintptr_t', c_schema))
c_array = ffi.new('struct ArrowArray*')
ptr_array = int(ffi.cast('uintptr_t', c_array))
# pyarrow.Array._import_from_c(ptr_array, pa.list_(pa.int32()))
# import gc
# gc.collect() # Make sure no Arrow data dangles in a ref cycle
# pyarrow.Array._export_from_c(arr, ptr_array, ptr_schema)
arr_new = pyarrow.Array._import_from_c(ptr_array, ptr_schema)

430
src/record.rs Normal file
View file

@ -0,0 +1,430 @@
///
/// Generic record-level read/write functionality.
///
use std::io::{self, Write};
use crate::basic::{pack_datetime, pack_bitarray, pack_ascii, pack_int2, pack_int4, pack_real8}; #[warn(unused_imports)]
use crate::basic::{parse_datetime, parse_bitarray, parse_ascii, parse_int2, parse_int4, parse_real8}; #[warn(unused_imports)]
use crate::basic::{OResult, IResult, fail, parse_u16, take_bytes}; //ErrType,
use crate::records;
//#[no_mangle]
//pub extern "C" fn write_record_header(
#[repr(C)]
#[derive(Debug, Clone)]
pub struct RecordHeader {
pub tag: u16,
pub data_size: u16,
}
impl RecordHeader {
pub fn read(input: &[u8]) -> IResult<'_, RecordHeader> {
let (input, size) = parse_u16(input)?;
let (input, tag) = parse_u16(input)?;
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}))
}
pub fn pack_into(&self) -> [u8; 4] {
let size = self.data_size.checked_add(4).expect("Record too big!");
let [size_hi, size_lo] = size.to_be_bytes();
let [tag_hi, tag_lo] = self.tag.to_be_bytes();
[size_hi, size_lo, tag_hi, tag_lo]
}
pub fn write<W: Write>(&self, ww: &mut W) -> OResult {
let bytes = self.pack_into();
ww.write_all(&bytes)?;
Ok(bytes.len())
}
}
pub trait RecordData {
type BareData<'a>;
type InData : ?Sized;
type ByteData : AsRef<[u8]>;
fn read(input: &[u8], size: u16) -> IResult<'_, Self::BareData<'_>>;
fn pack_into(buf: &mut [u8], data: &Self::InData);
//fn size(data: &Self::BareData<'_>) -> u16;
fn pack(data: &Self::InData) -> Self::ByteData;
}
pub trait Record<RData: RecordData> {
fn tag() -> u16;
fn expected_size() -> Option<u16>;
fn check_size(actual_size: u16) -> Result<(), String> {
match Self::expected_size() {
Some(size) => if size == actual_size {
Ok(())
} else {
Err(format!("Expected record size {}, got {}", size, actual_size))
},
None => Ok(()),
}
}
fn read_header(input: &[u8]) -> IResult<'_, RecordHeader> {
RecordHeader::read(input)
}
fn write_header<W: Write>(ww: &mut W, data_size: u16) -> OResult {
RecordHeader{tag: Self::tag(), data_size}.write(ww)
}
fn read_data(input: &[u8], size: u16) -> IResult<'_, RData::BareData<'_>> {
RData::read(input, size)
}
fn pack_data(buf: &mut [u8], data: &RData::InData) {
RData::pack_into(buf, data)
}
///
/// Skip to the end of the next occurence of this record.
///
/// Return:
/// True if the record was encountered and skipped.
/// False if the end of the library was reached.
///
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))
}
(input, header) = RecordHeader::read(input)?;
}
(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 (input, data) = Self::read_data(input, header.data_size)?;
Ok((input, data))
}
fn expect_header(input: &[u8]) -> IResult<'_, u16> {
let (input, header) = RecordHeader::read(input)?;
if header.tag != Self::tag() {
fail(input, format!("Unexpected record! Got tag 0x{:04x}, expected 0x{:04x}", header.tag, Self::tag()))
} else {
Ok((input, header.data_size))
}
}
fn read(input: &[u8]) -> IResult<'_, RData::BareData<'_>> {
let (input, size) = Self::expect_header(input)?;
Self::check_size(size).unwrap();
let (input, data) = Self::read_data(input, size)?;
Ok((input, data))
}
fn write<W: Write>(ww: &mut W, data: &RData::InData) -> OResult {
let packed_data = RData::pack(data);
let data_bytes = packed_data.as_ref();
let len = u16::try_from(data_bytes.len()).ok()
.filter(|len| *len <= u16::MAX - 4)
.ok_or_else(|| io::Error::new(io::ErrorKind::InvalidInput, "Record longer than max size (u16)"))?;
Self::write_header(ww, len)?;
ww.write_all(data_bytes)?;
Ok(usize::from(len) + 4)
}
}
pub struct BitArray;
impl RecordData for BitArray {
type BareData<'a> = [bool; 16];
type InData = [bool; 16];
type ByteData = [u8; 2];
fn read(input: &[u8], size: u16) -> IResult<'_, Self::BareData<'_>> {
assert!(size == 2);
parse_bitarray(input)
}
fn pack_into(buf: &mut [u8], data: &Self::InData) {
pack_bitarray(buf, data);
}
fn pack(data: &Self::InData) -> Self::ByteData {
let mut buf = [0; 2];
Self::pack_into(&mut buf, data);
buf
}
}
pub struct Int2;
impl RecordData for Int2 {
type BareData<'a> = i16;
type InData = i16;
type ByteData = [u8; 2];
fn read(input: &[u8], size: u16) -> IResult<'_, Self::BareData<'_>> {
assert!(size == 2);
parse_int2(input)
}
fn pack_into(buf: &mut [u8], data: &Self::InData) {
pack_int2(buf, *data)
}
fn pack(data: &Self::InData) -> Self::ByteData {
let mut buf = [0; 2];
Self::pack_into(&mut buf, data);
buf
}
}
pub struct Int4;
impl RecordData for Int4 {
type BareData<'a> = i32;
type InData = i32;
type ByteData = [u8; 4];
fn read(input: &[u8], size: u16) -> IResult<'_, Self::BareData<'_>> {
assert!(size == 4);
parse_int4(input)
}
fn pack_into(buf: &mut [u8], data: &Self::InData) {
pack_int4(buf, *data)
}
fn pack(data: &Self::InData) -> Self::ByteData {
let mut buf = [0; 4];
Self::pack_into(&mut buf, data);
buf
}
}
pub struct Int2Array;
impl RecordData for Int2Array {
type BareData<'a> = Int2ArrayReader<'a>;
type InData = [i16];
type ByteData = Vec<u8>;
fn read(input: &[u8], size: u16) -> IResult<'_, Self::BareData<'_>> {
assert!(size.is_multiple_of(2), "Record must contain an integer quantity of integers");
//let mut input = input;
let (input, bytes) = take_bytes(input, size)?;
Ok((input, Int2ArrayReader{bytes}))
}
fn pack_into(buf: &mut [u8], data: &Self::InData) {
assert_eq!(buf.len(), data.len() * 2);
for (bytes, value) in buf.as_chunks_mut::<2>().0.iter_mut().zip(data) {
*bytes = value.to_be_bytes();
}
}
fn pack(data: &Self::InData) -> Self::ByteData {
let mut buf = vec![0; data.len() * 2];
Self::pack_into(&mut buf, data);
buf
}
}
pub struct Int2ArrayReader<'a> {
bytes: &'a [u8],
}
impl Iterator for Int2ArrayReader<'_> {
type Item = i16;
fn next(&mut self) -> Option<Self::Item> {
if self.bytes.len() < 2 {
None
} else {
let (remaining, val) = parse_int2(self.bytes).unwrap();
self.bytes = remaining;
Some(val)
}
}
}
pub struct Int4Array;
impl RecordData for Int4Array {
type BareData<'a> = Int4ArrayReader<'a>;
type InData = [i32];
type ByteData = Vec<u8>;
fn read(input: &[u8], size: u16) -> IResult<'_, Self::BareData<'_>> {
assert!(size.is_multiple_of(4), "Record must contain an integer quantity of integers");
//let mut input = input;
let (input, bytes) = take_bytes(input, size)?;
Ok((input, Int4ArrayReader{bytes}))
}
fn pack_into(buf: &mut [u8], data: &Self::InData) {
assert_eq!(buf.len(), data.len() * 4);
for (bytes, value) in buf.as_chunks_mut::<4>().0.iter_mut().zip(data) {
*bytes = value.to_be_bytes();
}
}
fn pack(data: &Self::InData) -> Self::ByteData {
let mut buf = vec![0; data.len() * 4];
Self::pack_into(&mut buf, data);
buf
}
}
pub struct Int4ArrayReader<'a> {
bytes: &'a [u8],
}
impl Iterator for Int4ArrayReader<'_> {
type Item = i32;
fn next(&mut self) -> Option<Self::Item> {
if self.bytes.len() < 4 {
None
} else {
let (remaining, val) = parse_int4(self.bytes).unwrap();
self.bytes = remaining;
Some(val)
}
}
}
pub struct Real8;
impl RecordData for Real8 {
type BareData<'a> = f64;
type InData = f64;
type ByteData = [u8; 8];
fn read(input: &[u8], size: u16) -> IResult<'_, Self::BareData<'_>> {
assert!(size == 8);
parse_real8(input)
}
fn pack_into(buf: &mut [u8], data: &Self::InData) {
pack_real8(buf, *data).unwrap_or_else(|_| panic!("Float {0} too big for Real8", data))
}
fn pack(data: &Self::InData) -> Self::ByteData {
let mut buf = [0; 8];
Self::pack_into(&mut buf, data);
buf
}
}
pub struct Real8Pair;
impl RecordData for Real8Pair {
type BareData<'a> = (f64, f64);
type InData = (f64, f64);
type ByteData = [u8; 2 * 8];
fn read(input: &[u8], size: u16) -> IResult<'_, Self::BareData<'_>> {
assert!(size == 2 * 8);
let (input, data0) = parse_real8(input)?;
let (input, data1) = parse_real8(input)?;
Ok((input, (data0, data1)))
}
fn pack_into(buf: &mut [u8], data: &Self::InData) {
pack_real8(buf, data.0).unwrap_or_else(|_| panic!("Float.0 {0} too big for Real8", data.0));
pack_real8(&mut buf[8 * 1..], data.1).unwrap_or_else(|_| panic!("Float.1 {0} too big for Real8", data.1));
}
fn pack(data: &Self::InData) -> Self::ByteData {
let mut buf = [0; 2 * 8];
Self::pack_into(&mut buf, data);
buf
}
//fn write<W: Write>(ww: &mut W, data: &Self::BareData) -> OResult {
// let mut buf = [u8; 2 * 6 * 2];
// Self::pack_into(&mut buf, data)
//}
}
pub struct ASCII;
impl RecordData for ASCII {
type BareData<'a> = Vec<u8>;
type InData = [u8];
type ByteData = Vec<u8>;
fn read(input: &[u8], size: u16) -> IResult<'_, Self::BareData<'_>> {
parse_ascii(input, size)
}
fn pack_into(buf: &mut [u8], data: &Self::InData) {
pack_ascii(buf, data);
}
fn pack(data: &Self::InData) -> Self::ByteData {
let mut buf = vec![0; data.len() + data.len() % 2];
Self::pack_into(&mut buf, data);
buf
}
}
pub struct DateTimePair;
impl RecordData for DateTimePair {
type BareData<'a> = [[i16; 6]; 2];
type InData = [[i16; 6]; 2];
type ByteData = [u8; 2 * 6 * 2];
fn read(input: &[u8], size: u16) -> IResult<'_, Self::BareData<'_>> {
assert!(size == 2 * 6 * 2);
let (input, data0) = parse_datetime(input)?;
let (input, data1) = parse_datetime(input)?;
Ok((input, [data0, data1]))
}
fn pack_into(buf: &mut [u8], data: &Self::InData) {
pack_datetime(buf, &data[0]);
pack_datetime(&mut buf[6 * 2 * 1..], &data[1]);
}
fn pack(data: &Self::InData) -> Self::ByteData {
let mut buf = [0; 2 * 6 * 2];
Self::pack_into(&mut buf, data);
buf
}
//fn write<W: Write>(ww: &mut W, data: &Self::BareData) -> OResult {
// let mut buf = [u8; 2 * 6 * 2];
// Self::pack_into(&mut buf, data)
//}
}
pub struct Empty;
impl RecordData for Empty {
type BareData<'a> = ();
type InData = ();
type ByteData = [u8; 0];
fn read(input: &[u8], size: u16) -> IResult<'_, Self::BareData<'_>> {
assert!(size == 0);
Ok((input, ()))
}
fn pack_into(_buf: &mut [u8], _data: &Self::InData) {
}
fn pack(_data: &Self::InData) -> Self::ByteData {
[]
}
//fn write<W: Write>(ww: &mut W, data: &Self::BareData) {
//}
}

567
src/records.rs Normal file
View file

@ -0,0 +1,567 @@
///
/// Record type and tag definitions
///
use crate::record::{Record, Int2, Int4, Int2Array, Int4Array, Real8, Real8Pair, DateTimePair, BitArray, ASCII, Empty};
//use std::io::Write;
// record tags
pub const RTAG_HEADER: u16 = 0x0002;
pub const RTAG_BGNLIB: u16 = 0x0102;
pub const RTAG_LIBNAME: u16 = 0x0206;
pub const RTAG_UNITS: u16 = 0x0305; // (user_units_per_db_unit, db_units_per_meter)
pub const RTAG_ENDLIB: u16 = 0x0400;
pub const RTAG_BGNSTR: u16 = 0x0502;
pub const RTAG_STRNAME: u16 = 0x0606;
pub const RTAG_ENDSTR: u16 = 0x0700;
pub const RTAG_BOUNDARY: u16 = 0x0800;
pub const RTAG_PATH: u16 = 0x0900;
pub const RTAG_SREF: u16 = 0x0a00;
pub const RTAG_AREF: u16 = 0x0b00;
pub const RTAG_TEXT: u16 = 0x0c00;
pub const RTAG_LAYER: u16 = 0x0d02;
pub const RTAG_DATATYPE: u16 = 0x0e02;
pub const RTAG_WIDTH: u16 = 0x0f03;
pub const RTAG_XY: u16 = 0x1003;
pub const RTAG_ENDEL: u16 = 0x1100;
pub const RTAG_SNAME: u16 = 0x1206;
pub const RTAG_COLROW: u16 = 0x1302;
pub const RTAG_NODE: u16 = 0x1500;
pub const RTAG_TEXTTYPE: u16 = 0x1602;
pub const RTAG_PRESENTATION: u16 = 0x1701;
pub const RTAG_SPACING: u16 = 0x1802; // unused; not sure about 02
pub const RTAG_STRING: u16 = 0x1906;
pub const RTAG_STRANS: u16 = 0x1a01;
pub const RTAG_MAG: u16 = 0x1b05;
pub const RTAG_ANGLE: u16 = 0x1c05;
pub const RTAG_UINTEGER: u16 = 0x1d02; // unused; not sure about 02
pub const RTAG_USTRING: u16 = 0x1e06; // unused; not sure about 06
pub const RTAG_REFLIBS: u16 = 0x1f06;
pub const RTAG_FONTS: u16 = 0x2006;
pub const RTAG_PATHTYPE: u16 = 0x2102;
pub const RTAG_GENERATIONS: u16 = 0x2202;
pub const RTAG_ATTRTABLE: u16 = 0x2306;
pub const RTAG_STYPTABLE: u16 = 0x2406; // unused; not sure about 06
pub const RTAG_STRTYPE: u16 = 0x2502; // unused
pub const RTAG_ELFLAGS: u16 = 0x2601;
pub const RTAG_ELKEY: u16 = 0x2703; // unused
pub const RTAG_LINKTYPE: u16 = 0x2803; // unused
pub const RTAG_LINKKEYS: u16 = 0x2903; // unused
pub const RTAG_NODETYPE: u16 = 0x2a02;
pub const RTAG_PROPATTR: u16 = 0x2b02;
pub const RTAG_PROPVALUE: u16 = 0x2c06;
pub const RTAG_BOX: u16 = 0x2d00;
pub const RTAG_BOXTYPE: u16 = 0x2e02;
pub const RTAG_PLEX: u16 = 0x2f03;
pub const RTAG_BGNEXTN: u16 = 0x3003;
pub const RTAG_ENDEXTN: u16 = 0x3103;
pub const RTAG_TAPENUM: u16 = 0x3202;
pub const RTAG_TAPECODE: u16 = 0x3302;
pub const RTAG_STRCLASS: u16 = 0x3401;
pub const RTAG_RESERVED: u16 = 0x3503;
pub const RTAG_FORMAT: u16 = 0x3602;
pub const RTAG_MASK: u16 = 0x3706; // list of Layers and dtypes
pub const RTAG_ENDMASKS: u16 = 0x3800; // end of MASKS records
pub const RTAG_LIBDIRSIZE: u16 = 0x3902;
pub const RTAG_SRFNAME: u16 = 0x3a06;
pub const RTAG_LIBSECUR: u16 = 0x3b02;
pub const RTAG_BORDER: u16 = 0x3c00;
pub const RTAG_SOFTFENCE: u16 = 0x3d00;
pub const RTAG_HARDFENCE: u16 = 0x3f00;
pub const RTAG_SOFTWIRE: u16 = 0x3f00;
pub const RTAG_HARDWIRE: u16 = 0x4000;
pub const RTAG_PATHPORT: u16 = 0x4100;
pub const RTAG_NODEPORT: u16 = 0x4200;
pub const RTAG_USERCONSTRAINT: u16 = 0x4300;
pub const RTAG_SPACERERROR: u16 = 0x4400;
pub const RTAG_CONTACT: u16 = 0x4500;
/*
// data types
pub const DATA_TYPE_NONE: u16 = 0x00;
pub const DATA_TYPE_BIT: u16 = 0x01;
pub const DATA_TYPE_INT16: u16 = 0x02;
pub const DATA_TYPE_INT32: u16 = 0x03;
pub const DATA_TYPE_REAL32: u16 = 0x04;
pub const DATA_TYPE_REAL64: u16 = 0x05;
pub const DATA_TYPE_STR: u16 = 0x06;
pub const MAX_DATA_SIZE: usize = 8;
/// Returns the size of the given data type in bytes.
pub fn data_size(t: u16) -> Option<u16> {
match t {
x if x == DATA_TYPE_NONE => 0,
x if x == DATA_TYPE_BIT => 2,
x if x == DATA_TYPE_INT16 => 2,
x if x == DATA_TYPE_INT32 => 4,
x if x == DATA_TYPE_REAL32 => 4,
x if x == DATA_TYPE_REAL64 => 8,
_ => 0
}
*/
pub struct HEADER;
impl Record<Int2> for HEADER {
fn tag() -> u16 { RTAG_HEADER }
fn expected_size() -> Option<u16> { Some(2) }
}
//impl Record<Int2> for HEADER;
pub struct BGNLIB;
impl Record<DateTimePair> for BGNLIB {
fn tag() -> u16 { RTAG_BGNLIB }
fn expected_size() -> Option<u16> { Some(2 * 2 * 6) }
}
pub struct LIBNAME;
impl Record<ASCII> for LIBNAME {
fn tag() -> u16 { RTAG_LIBNAME }
fn expected_size() -> Option<u16> { None }
}
pub struct UNITS;
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) }
}
pub struct STRNAME;
impl Record<ASCII> for STRNAME {
fn tag() -> u16 { RTAG_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 }
}
pub struct ENDEL;
impl Record<Empty> for ENDEL {
fn tag() -> u16 { RTAG_ENDEL }
fn expected_size() -> Option<u16> { Some(0) }
}
pub struct SNAME;
impl Record<ASCII> for SNAME {
fn tag() -> u16 { RTAG_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 }
fn expected_size() -> Option<u16> { Some(2) }
}
pub struct STRING;
impl Record<ASCII> for STRING {
fn tag() -> u16 { RTAG_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 }
fn expected_size() -> Option<u16> { Some(2) }
}
pub struct USTRING;
impl Record<ASCII> for USTRING {
fn tag() -> u16 { RTAG_USTRING }
fn expected_size() -> Option<u16> { None }
}
pub struct REFLIBS;
impl Record<ASCII> for REFLIBS {
fn tag() -> u16 { RTAG_REFLIBS }
fn expected_size() -> Option<u16> { None }
}
impl REFLIBS {
pub fn check_size(actual_size: usize) -> Result<(), String> {
if actual_size.is_multiple_of(44) {
Ok(())
} else {
Err(format!("Expected record size divisible by 44, got {}", actual_size))
}
}
}
pub struct FONTS;
impl Record<ASCII> for FONTS {
fn tag() -> u16 { RTAG_FONTS }
fn expected_size() -> Option<u16> { None }
}
impl FONTS {
pub fn check_size(actual_size: usize) -> Result<(), String> {
if actual_size.is_multiple_of(44) {
Ok(())
} else {
Err(format!("Expected record size divisible by 44, got {}", actual_size))
}
}
}
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 }
fn expected_size() -> Option<u16> { None }
}
impl ATTRTABLE {
pub fn check_size(actual_size: usize) -> Result<(), String> {
if actual_size.is_multiple_of(44) {
Ok(())
} else {
Err(format!("Expected record size divisible by 44, got {}", actual_size))
}
}
}
pub struct STYPTABLE;
impl Record<ASCII> for STYPTABLE {
fn tag() -> u16 { RTAG_STYPTABLE }
fn expected_size() -> Option<u16> { None }
}
pub struct STRTYPE;
impl Record<Int2> for STRTYPE {
fn tag() -> u16 { RTAG_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 }
fn expected_size() -> Option<u16> { Some(2) }
}
pub struct LINKTYPE;
impl Record<Int2> for LINKTYPE {
fn tag() -> u16 { RTAG_LINKTYPE }
fn expected_size() -> Option<u16> { Some(2) }
}
pub struct LINKKEYS;
impl Record<Int2> for LINKKEYS {
fn tag() -> u16 { RTAG_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 }
fn expected_size() -> Option<u16> { Some(2) }
}
pub struct TAPECODE;
impl Record<Int2Array> for TAPECODE {
fn tag() -> u16 { RTAG_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 }
fn expected_size() -> Option<u16> { Some(2) }
}
pub struct FORMAT;
impl Record<Int2> for FORMAT {
fn tag() -> u16 { RTAG_FORMAT }
fn expected_size() -> Option<u16> { Some(2) }
}
pub struct MASK;
impl Record<ASCII> for MASK {
fn tag() -> u16 { RTAG_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 }
fn expected_size() -> Option<u16> { Some(2) }
}
pub struct SRFNAME;
impl Record<ASCII> for SRFNAME {
fn tag() -> u16 { RTAG_SRFNAME }
fn expected_size() -> Option<u16> { None }
}
pub struct LIBSECUR;
impl Record<Int2> for LIBSECUR {
fn tag() -> u16 { RTAG_LIBSECUR }
fn expected_size() -> Option<u16> { Some(2) }
}
pub struct BORDER;
impl Record<Empty> for BORDER {
fn tag() -> u16 { RTAG_BORDER }
fn expected_size() -> Option<u16> { Some(0) }
}
pub struct SOFTFENCE;
impl Record<Empty> for SOFTFENCE {
fn tag() -> u16 { RTAG_SOFTFENCE }
fn expected_size() -> Option<u16> { Some(0) }
}
pub struct HARDFENCE;
impl Record<Empty> for HARDFENCE {
fn tag() -> u16 { RTAG_HARDFENCE }
fn expected_size() -> Option<u16> { Some(0) }
}
pub struct SOFTWIRE;
impl Record<Empty> for SOFTWIRE {
fn tag() -> u16 { RTAG_SOFTWIRE }
fn expected_size() -> Option<u16> { Some(0) }
}
pub struct HARDWIRE;
impl Record<Empty> for HARDWIRE {
fn tag() -> u16 { RTAG_HARDWIRE }
fn expected_size() -> Option<u16> { Some(0) }
}
pub struct PATHPORT;
impl Record<Empty> for PATHPORT {
fn tag() -> u16 { RTAG_PATHPORT }
fn expected_size() -> Option<u16> { Some(0) }
}
pub struct NODEPORT;
impl Record<Empty> for NODEPORT {
fn tag() -> u16 { RTAG_NODEPORT }
fn expected_size() -> Option<u16> { Some(0) }
}
pub struct USERCONSTRAINT;
impl Record<Empty> for USERCONSTRAINT {
fn tag() -> u16 { RTAG_USERCONSTRAINT }
fn expected_size() -> Option<u16> { Some(0) }
}
pub struct SPACERERROR;
impl Record<Empty> for SPACERERROR {
fn tag() -> u16 { RTAG_SPACERERROR }
fn expected_size() -> Option<u16> { Some(0) }
}
pub struct CONTACT;
impl Record<Empty> for CONTACT {
fn tag() -> u16 { RTAG_CONTACT }
fn expected_size() -> Option<u16> { Some(0) }
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn bgnstr_reads_both_timestamps() {
let mut bytes = vec![0, 28, 0x05, 0x02];
for value in [126_i16, 11, 14, 12, 30, 0, 125, 10, 13, 9, 15, 1] {
bytes.extend_from_slice(&value.to_be_bytes());
}
bytes.extend_from_slice(&[0, 4, 0x07, 0]);
let (remaining, timestamps) = BGNSTR::read(&bytes).unwrap();
assert_eq!(timestamps, [[2026, 11, 14, 12, 30, 0], [2025, 10, 13, 9, 15, 1]]);
assert_eq!(remaining, &[0, 4, 0x07, 0]);
}
}

1492
src/review.rs Normal file

File diff suppressed because it is too large Load diff

127
src/review/changes.rs Normal file
View file

@ -0,0 +1,127 @@
use super::*;
#[derive(Debug)]
pub struct Replacement {
pub removed: usize,
pub added: usize,
pub explanation: String,
/// Explanation matching is never object identity or geometric equivalence.
pub match_rule: &'static str,
}
type Group = (u16, Option<Layer>, Option<String>, Option<Bounds>);
fn group(object: &ObjectChange, bounds: bool) -> Group {
(
object.kind,
object.layer,
object.target.clone(),
if bounds && object.target.is_none() {
object.bounds
} else {
None
},
)
}
/// Pair only unique candidates in the remaining multisets. Ambiguous groups
/// remain additions/removals; no identity or geometric equivalence is inferred.
pub(super) fn replacements(old: &[ObjectChange], new: &[ObjectChange]) -> Vec<Replacement> {
let (mut used_old, mut used_new) = (BTreeSet::new(), BTreeSet::new());
let mut result = Vec::new();
for bounds in [true, false] {
let (mut a, mut b) = (
BTreeMap::<Group, Vec<usize>>::new(),
BTreeMap::<Group, Vec<usize>>::new(),
);
for (i, object) in old
.iter()
.enumerate()
.filter(|(i, _)| !used_old.contains(i))
{
a.entry(group(object, bounds)).or_default().push(i);
}
for (i, object) in new
.iter()
.enumerate()
.filter(|(i, _)| !used_new.contains(i))
{
b.entry(group(object, bounds)).or_default().push(i);
}
for (key, indices) in a {
if let (1, Some(others)) = (indices.len(), b.get(&key))
&& others.len() == 1
{
let (i, j) = (indices[0], others[0]);
result.push(Replacement {
removed: i,
added: j,
explanation: explain(&old[i], &new[j]),
match_rule: if old[i].target.is_some() {
"unique_kind_target"
} else if bounds {
"unique_kind_layer_bounds"
} else {
"unique_kind_layer"
},
});
used_old.insert(i);
used_new.insert(j);
}
}
}
result.sort_by_key(|r| (r.removed, r.added));
result
}
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])
));
}
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)
));
}
if old.width != new.width {
let width = |n: Option<i32>| {
n.map(|n| n.to_string())
.unwrap_or_else(|| "absent".to_string())
};
parts.push(format!(
"width {} -> {} dbu",
width(old.width),
width(new.width)
));
}
if old.bounds == new.bounds {
parts.push("coordinate bounds unchanged".to_string());
} else {
parts.push(format!(
"coordinate bounds {:?} -> {:?}",
old.bounds, new.bounds
));
}
parts.join("; ")
}

120
src/review/names.rs Normal file
View file

@ -0,0 +1,120 @@
use super::*;
pub fn parse_layer(value: &str) -> Result<Layer, String> {
let (a, b) = value
.split_once('/')
.ok_or("expected layer/type, for example 900/7")?;
Ok((decimal(a)?, decimal(b)?))
}
fn decimal(value: &str) -> Result<u16, String> {
if value.is_empty() || !value.bytes().all(|b| b.is_ascii_digit()) {
return Err("layer numbers must be unsigned decimal integers".to_string());
}
value
.parse()
.map_err(|_| "layer number exceeds 65535".to_string())
}
pub fn parse_layer_name(value: &str) -> Result<(Layer, String), String> {
let mut parts = value.splitn(3, ':');
let a = parts.next().ok_or("expected layer:datatype:name")?;
let b = parts.next().ok_or("expected layer:datatype:name")?;
let name = parts
.next()
.filter(|s| !s.is_empty() && !s.contains('\0'))
.ok_or("layer name must be nonempty and contain no NUL")?;
Ok(((decimal(a)?, decimal(b)?), name.to_string()))
}
fn record(output: &mut Vec<u8>, tag: u16, payload: &[u8]) -> Result<(), String> {
let padding = usize::from(tag & 0xff == 6 && !payload.len().is_multiple_of(2));
let size = payload
.len()
.checked_add(padding + 4)
.and_then(|n| u16::try_from(n).ok())
.ok_or("metadata label exceeds GDS record size")?;
output.extend_from_slice(&size.to_be_bytes());
output.extend_from_slice(&tag.to_be_bytes());
output.extend_from_slice(payload);
if padding != 0 {
output.push(0);
}
Ok(())
}
fn label(output: &mut Vec<u8>, carrier: Layer, text: &str) -> Result<(), String> {
record(output, RTAG_TEXT, &[])?;
record(output, RTAG_LAYER, &carrier.0.to_be_bytes())?;
record(output, RTAG_TEXTTYPE, &carrier.1.to_be_bytes())?;
record(output, RTAG_XY, &[0; 8])?;
record(output, RTAG_STRING, text.as_bytes())?;
record(output, RTAG_ENDEL, &[])
}
/// Consolidate recognized metadata into an explicitly chosen existing cell and
/// carrier. Other records, including timestamps and unknown records, are copied
/// byte-for-byte. Updates override existing names; the input is never modified.
pub fn write_layer_names(
input: &[u8],
cell_name: &str,
carrier: Layer,
updates: &BTreeMap<Layer, String>,
) -> Result<Vec<u8>, String> {
let doc = Document::parse(input)?;
if doc
.diagnostics
.iter()
.any(|i| i.code == "unsupported_metadata_version")
{
return Err("cannot rewrite an unsupported layer-name metadata version".to_string());
}
let cell = doc
.cells
.get(cell_name)
.ok_or_else(|| format!("cell {cell_name:?} does not exist"))?;
if cell
.objects
.iter()
.any(|o| o.layer == Some(carrier) && !o.metadata)
{
return Err(
"carrier pair already contains ordinary design objects in the selected cell"
.to_string(),
);
}
let mut names = doc.layer_names.clone();
for (&pair, name) in updates {
if name.is_empty() || name.contains('\0') {
return Err("layer names must be nonempty and contain no NUL".to_string());
}
names.insert(pair, name.clone());
}
let mut legend = Vec::new();
label(&mut legend, carrier, LAYER_SENTINEL)?;
for ((layer, dtype), name) in names {
label(&mut legend, carrier, &format!("{layer}:{dtype}:{name}"))?;
}
let mut edits: Vec<_> = doc
.cells
.values()
.flat_map(|c| &c.objects)
.filter(|o| o.metadata)
.map(|o| (o.offset, o.end_offset, None))
.collect();
edits.push((cell.end_offset, cell.end_offset, Some(legend.as_slice())));
edits.sort_by_key(|(start, end, _)| (*start, *end));
let mut result = Vec::with_capacity(input.len() + legend.len());
let mut cursor = 0;
for (start, end, replacement) in edits {
result.extend_from_slice(&input[cursor..start]);
if let Some(bytes) = replacement {
result.extend_from_slice(bytes);
}
cursor = end;
}
result.extend_from_slice(&input[cursor..]);
// Validate the final declaration set before the CLI creates an output file.
Document::parse(&result)?;
Ok(result)
}

407
src/review/report.rs Normal file
View file

@ -0,0 +1,407 @@
//! Versioned machine-readable diff reports. No consumer needs to parse prose.
use super::*;
use serde_json::{Value, json};
use std::path::{Path, PathBuf};
pub const SCHEMA_VERSION: u32 = 1;
#[derive(Clone, Debug, Default)]
pub struct ReportInputs {
pub old: Option<PathBuf>,
pub new: Option<PathBuf>,
}
/// Limits affect evidence only. The default includes every changed object.
#[derive(Clone, Debug, Default)]
pub struct ReportOptions {
pub filter: DisplayFilter,
pub max_objects: Option<usize>,
pub hierarchy: bool,
}
fn layer(pair: Layer) -> Value {
json!({"number": pair.0, "purpose": pair.1})
}
fn scope(value: &Scope) -> Value {
json!({"top": value.top, "cell": value.cell, "layer": value.layer.map(layer)})
}
fn input(path: Option<&Path>, doc: Option<&Document<'_>>) -> Value {
let units = doc
.filter(|d| d.present)
.and_then(|d| d.header.iter().find(|(tag, _)| *tag == RTAG_UNITS))
.map(|(tag, data)| field(*tag, data));
json!({
"path": path.and_then(Path::to_str),
"path_encoded_hex": path.filter(|p| p.to_str().is_none())
.map(|p| hex(p.as_os_str().as_encoded_bytes())),
"present": doc.map(|d| d.present),
"units": units
})
}
fn envelope(inputs: &ReportInputs, selection: &Scope, options: &ReportOptions) -> Value {
json!({
"schema_version": SCHEMA_VERSION,
"kind": "diff",
"producer": {"name": "pheon", "version": env!("CARGO_PKG_VERSION")},
"comparison_profile": PROFILE,
"inputs": {"old": input(inputs.old.as_deref(), None), "new": input(inputs.new.as_deref(), None)},
"scope": scope(selection),
"coordinates": {"unit": "database_unit", "frame": "cell_local", "bounds": "coordinate_envelope"},
"status": "failed",
"result": "unknown",
"exit_code": 2,
"summary": null,
"report_selection": {
"cell": options.filter.cell,
"layer": options.filter.layer.map(layer),
"max_objects": options.max_objects.map(|n| n.to_string()),
"truncated": false,
"filtered_cells": "0", "filtered_objects": "0", "limited_objects": "0", "emitted_objects": "0"
},
"layer_legend": [],
"library_changes": [], "layer_name_changes": [], "cell_changes": [],
"hierarchy": null,
"diagnostics": []
})
}
/// Input/selection failures still produce a valid report. Unknown locations are
/// null rather than inferred by parsing an error's human-readable message.
pub fn failure_report(
inputs: &ReportInputs,
selection: &Scope,
options: &ReportOptions,
code: &str,
message: &str,
side: Option<&str>,
) -> Value {
let mut report = envelope(inputs, selection, options);
report["diagnostics"] = json!([{
"code": code, "severity": "error", "message": message,
"side": side, "byte_offset": null, "cell": null, "layer": null, "target": null
}]);
report
}
fn field(tag: u16, data: &[u8]) -> Value {
let (encoding, value) = if tag == RTAG_XY && data.len().is_multiple_of(8) {
("xy", json!(points(data)))
} else if matches!(
tag,
RTAG_LAYER | RTAG_DATATYPE | RTAG_TEXTTYPE | RTAG_NODETYPE | RTAG_BOXTYPE
) && data.len().is_multiple_of(2)
{
(
"uint16",
json!(
data.as_chunks::<2>()
.0
.iter()
.copied()
.map(u16::from_be_bytes)
.collect::<Vec<_>>()
),
)
} else {
match tag & 0xff {
0 if data.is_empty() => ("none", Value::Null),
1 if data.len().is_multiple_of(2) => (
"bits",
json!(
data.as_chunks::<2>()
.0
.iter()
.copied()
.map(u16::from_be_bytes)
.collect::<Vec<_>>()
),
),
2 if data.len().is_multiple_of(2) => (
"int16",
json!(
data.as_chunks::<2>()
.0
.iter()
.copied()
.map(i16::from_be_bytes)
.collect::<Vec<_>>()
),
),
3 if data.len().is_multiple_of(4) => (
"int32",
json!(
data.as_chunks::<4>()
.0
.iter()
.copied()
.map(i32::from_be_bytes)
.collect::<Vec<_>>()
),
),
5 if data.len().is_multiple_of(8) => (
"real8",
json!(
data.as_chunks::<8>()
.0
.iter()
.map(|&v| decode_real8(u64::from_be_bytes(v)))
.collect::<Vec<_>>()
),
),
6 => ("ascii", json!(std::str::from_utf8(data).ok())),
_ => ("bytes", Value::Null),
}
};
json!({"tag": tag, "name": tag_name(tag), "encoding": encoding, "value": value, "payload_hex": hex(data)})
}
fn fields(values: &[FieldValue]) -> Vec<Value> {
values.iter().map(|f| field(f.tag, &f.data)).collect()
}
fn object(value: &ObjectChange, id: &str, side: &str) -> Value {
json!({
"id": id, "kind": kind_name(value.kind), "tag": value.kind,
"source": {"side": side, "byte_offset": value.offset.to_string(), "end_offset": value.end_offset.to_string()},
"layer": value.layer.map(layer), "bounds": value.bounds, "target": value.target,
"origin": value.origin.filter(|_| matches!(value.kind, RTAG_SREF | RTAG_AREF | RTAG_TEXT)), "width": value.width, "counts": value.counts,
"fields": fields(&value.fields),
"properties": value.properties.iter().map(|(attribute, bytes)| json!({
"attribute": attribute, "value": std::str::from_utf8(bytes).ok(), "payload_hex": hex(bytes)
})).collect::<Vec<_>>()
})
}
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| {
json!({
"tag": tag,
"before": a.get(&tag).map(|data| field(tag, data)),
"after": b.get(&tag).map(|data| field(tag, data))
})
})
.collect()
}
impl Comparison {
/// Render evidence from the same documents used to construct this comparison.
/// IDs are local to this report, not persistent object identities.
pub fn json_report(
&self,
old: &Document<'_>,
new: &Document<'_>,
inputs: &ReportInputs,
options: &ReportOptions,
) -> Value {
let mut report = envelope(inputs, &self.scope, options);
report["inputs"] = json!({"old": input(inputs.old.as_deref(), Some(old)), "new": input(inputs.new.as_deref(), Some(new))});
let different = !self.cells.is_empty()
|| !self.library_fields.is_empty()
|| !self.layer_names.is_empty();
report["status"] = json!(if self.issues.is_empty() {
"complete"
} else {
"incomplete"
});
report["result"] = json!(if different {
"different"
} else if self.is_equal() {
"equal"
} else {
"unknown"
});
report["exit_code"] = json!(self.exit_code());
report["summary"] = json!({
"counts": if self.issues.is_empty() { "exact" } else { "observed" },
"changed_cells": self.cells.len().to_string(),
"removed_objects": self.cells.iter().map(|c| c.removed.len()).sum::<usize>().to_string(),
"added_objects": self.cells.iter().map(|c| c.added.len()).sum::<usize>().to_string(),
"layer_name_changes": self.layer_names.len().to_string(),
"library_field_changes": self.library_fields.len().to_string()
});
report["layer_legend"] = json!(
self.layer_legend
.iter()
.map(|(pair, name)| json!({"layer": layer(*pair), "name": name}))
.collect::<Vec<_>>()
);
report["layer_name_changes"] = json!(
self.layer_names
.iter()
.map(|c| json!({
"layer": layer(c.layer), "before": c.old, "after": c.new
}))
.collect::<Vec<_>>()
);
report["library_changes"] = json!(self.library_fields.iter().map(|c| {
if c.tag.is_none() {
json!({"field": "presence", "tag": null, "before": old.present, "after": new.present})
} else {
json!({"field": c.field, "tag": c.tag, "before": fields(&c.old_fields), "after": fields(&c.new_fields)})
}
}).collect::<Vec<_>>());
let (mut emitted, mut filtered_objects, mut limited, mut filtered_cells) =
(0_usize, 0_usize, 0_usize, 0_usize);
let mut cells = Vec::new();
for (cell_index, cell) in self.cells.iter().enumerate() {
if options
.filter
.cell
.as_ref()
.is_some_and(|name| name != &cell.name)
{
filtered_cells += 1;
filtered_objects += cell.removed.len() + cell.added.len();
continue;
}
let (mut removed, mut added) = (Vec::new(), Vec::new());
let mut shown = BTreeSet::new();
for (side, objects, output) in [
("old", &cell.removed, &mut removed),
("new", &cell.added, &mut added),
] {
for (index, change) in objects.iter().enumerate() {
if options
.filter
.layer
.is_some_and(|l| change.layer != Some(l))
{
filtered_objects += 1;
} else if options.max_objects.is_some_and(|max| emitted >= max) {
limited += 1;
} else {
let id = format!("c{cell_index}:{side}:{index}");
output.push(object(change, &id, side));
shown.insert(id);
emitted += 1;
}
}
}
let replacements: Vec<_> = cell.replacements.iter().filter_map(|r| {
let a = format!("c{cell_index}:old:{}", r.removed);
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": r.explanation,
"field_changes": field_changes(&cell.removed[r.removed], &cell.added[r.added]),
"properties_changed": cell.removed[r.removed].properties != cell.added[r.added].properties
}))
}).collect();
cells.push(json!({
"name": cell.name,
"before": cell.old_offset.map(|offset| json!({"byte_offset": offset.to_string(), "attributes": fields(&cell.old_attributes)})),
"after": cell.new_offset.map(|offset| json!({"byte_offset": offset.to_string(), "attributes": fields(&cell.new_attributes)})),
"attributes_changed": cell.attributes_changed,
"removed": removed, "added": added, "replacements": replacements
}));
}
report["cell_changes"] = json!(cells);
report["report_selection"]["truncated"] =
json!(filtered_cells > 0 || filtered_objects > 0 || limited > 0);
for (key, count) in [
("filtered_cells", filtered_cells),
("filtered_objects", filtered_objects),
("limited_objects", limited),
("emitted_objects", emitted),
] {
report["report_selection"][key] = json!(count.to_string());
}
let mut diagnostics: Vec<_> = [("old", old), ("new", new)]
.into_iter()
.flat_map(|(side, doc)| {
doc.diagnostics.iter().map(move |d| {
json!({
"code": d.code, "severity": "error", "message": d.message, "side": side,
"byte_offset": d.offset.map(|n| n.to_string()), "cell": d.cell,
"layer": d.layer.map(layer), "target": d.target
})
})
})
.collect();
if old.scope != new.scope {
diagnostics.push(json!({"code": "scope_mismatch", "severity": "error", "message": "comparison scopes differ", "side": null, "byte_offset": null, "cell": null, "layer": null, "target": null}));
}
report["diagnostics"] = json!(diagnostics);
if options.hierarchy {
report["hierarchy"] = graph(old, new, self);
}
report
}
}
fn graph(old: &Document<'_>, new: &Document<'_>, diff: &Comparison) -> Value {
let a = old.edges();
let b = new.edges();
let edges: BTreeSet<_> = a.keys().chain(b.keys()).cloned().collect();
let names: BTreeSet<_> = old
.cells
.keys()
.chain(new.cells.keys())
.cloned()
.chain(
edges
.iter()
.flat_map(|(parent, child)| [parent.clone(), child.clone()]),
)
.collect();
let direct: BTreeSet<_> = diff.cells.iter().map(|c| c.name.clone()).collect();
let mut affected = direct.clone();
let mut parents = BTreeMap::<&str, Vec<&str>>::new();
for (parent, child) in &edges {
parents.entry(child).or_default().push(parent);
}
let mut pending: Vec<_> = direct.iter().cloned().collect();
while let Some(child) = pending.pop() {
for parent in parents.get(child.as_str()).into_iter().flatten() {
if affected.insert((*parent).to_string()) {
pending.push((*parent).to_string());
}
}
}
let changed_refs: BTreeSet<_> = diff
.cells
.iter()
.flat_map(|c| {
c.removed
.iter()
.chain(&c.added)
.filter_map(|o| o.target.as_ref().map(|t| (c.name.clone(), t.clone())))
})
.collect();
let a_targets: BTreeSet<_> = a.keys().map(|(_, t)| t.as_str()).collect();
let b_targets: BTreeSet<_> = b.keys().map(|(_, t)| t.as_str()).collect();
let state = |doc: &Document<'_>, targets: &BTreeSet<&str>, name: &String| {
if doc.cells.contains_key(name) {
"included"
} else if doc.known_cells.contains(name) {
"outside_scope"
} else if targets.contains(name.as_str()) {
"unresolved"
} else {
"absent"
}
};
json!({
"nodes": names.iter().map(|name| json!({
"cell": name, "before": state(old, &a_targets, name), "after": state(new, &b_targets, name),
"change": if direct.contains(name) { "direct" } else if affected.contains(name) { "affected" } else { "unchanged" }
})).collect::<Vec<_>>(),
"edges": edges.iter().map(|edge| json!({
"parent": edge.0, "target": edge.1,
"before_count": a.get(edge).copied().unwrap_or(0).to_string(),
"after_count": b.get(edge).copied().unwrap_or(0).to_string(),
"reference_changed": changed_refs.contains(edge)
})).collect::<Vec<_>>()
})
}

233
src/review/selection.rs Normal file
View file

@ -0,0 +1,233 @@
use super::*;
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct Scope {
pub top: Option<String>,
pub cell: Option<String>,
pub layer: Option<Layer>,
}
/// Presentation filters never modify comparison status, totals, or hierarchy.
#[derive(Clone, Debug, Default)]
pub struct DisplayFilter {
pub cell: Option<String>,
pub layer: Option<Layer>,
}
impl Scope {
pub fn description(&self) -> String {
let top = self
.top
.as_ref()
.map(|s| format!("{s:?}"))
.unwrap_or_else(|| "all".to_string());
let cell = self
.cell
.as_ref()
.map(|s| format!("{s:?}"))
.unwrap_or_else(|| "all".to_string());
let layer = self
.layer
.map(|(a, b)| format!("{a}/{b}"))
.unwrap_or_else(|| "all".to_string());
format!("scope top={top} cell={cell} layer={layer}")
}
pub fn validate_pair(&self, old: &Document<'_>, new: &Document<'_>) -> Result<(), String> {
for (kind, name) in [("top", &self.top), ("cell", &self.cell)] {
if let Some(name) = name
&& !old.cells.contains_key(name)
&& !new.cells.contains_key(name)
{
return Err(format!(
"selected {kind} {name:?} exists in neither revision"
));
}
}
if let (Some(top), Some(cell)) = (&self.top, &self.cell)
&& !old.reachable(top).contains(cell)
&& !new.reachable(top).contains(cell)
{
return Err(format!(
"selected cell {cell:?} is not reachable from {top:?}"
));
}
Ok(())
}
}
impl<'a> Document<'a> {
/// A missing Git revision has no cells or declarations. Retain the other
/// revision's library header to avoid inventing a unit/name change.
pub fn empty_like(&self) -> Self {
Self {
records: Vec::new(),
cells: BTreeMap::new(),
layer_names: BTreeMap::new(),
issues: Vec::new(),
diagnostics: Vec::new(),
header: self.header.clone(),
global_issues: Vec::new(),
known_cells: BTreeSet::new(),
scope: self.scope.clone(),
present: false,
}
}
fn reachable(&self, root: &str) -> BTreeSet<String> {
let mut found = BTreeSet::new();
let mut pending = vec![root.to_string()];
while let Some(name) = pending.pop() {
if let Some(cell) = self.cells.get(&name)
&& found.insert(name)
{
pending.extend(cell.objects.iter().filter_map(|o| o.target.clone()));
}
}
found
}
pub(super) fn refresh_issues(&mut self) {
self.diagnostics = self.global_issues.clone();
for (name, cell) in &self.cells {
for issue in &cell.issues {
let mut issue = issue.clone();
issue.cell = Some(name.clone());
issue.message = format!("cell {name:?}: {}", issue.message);
self.diagnostics.push(issue);
}
for object in &cell.objects {
for issue in &object.issues {
let mut issue = issue.clone();
issue.cell = Some(name.clone());
issue.layer = object.layer;
issue.message = format!("cell {name:?}: {}", issue.message);
self.diagnostics.push(issue);
}
}
}
self.check_graph();
self.issues = self.diagnostics.iter().map(|d| d.message.clone()).collect();
}
/// Project a parsed document. Missing selectors are allowed only for one
/// side of a comparison, after Scope::validate_pair has checked both sides.
pub fn selected(mut self, scope: &Scope, allow_missing: bool) -> Result<Self, String> {
if self.scope != Scope::default() {
return Err("document is already scoped; combine selectors in one Scope".to_string());
}
if !allow_missing {
scope.validate_pair(&self, &self)?;
}
let mut wanted = scope
.top
.as_ref()
.map(|t| self.reachable(t))
.unwrap_or_else(|| self.cells.keys().cloned().collect());
if let Some(cell) = &scope.cell {
wanted.retain(|n| n == cell);
}
// Determine layer coverage before projecting cells: exact-cell scopes
// still need to know whether a referenced definition uses this layer.
let relevant = scope.layer.map(|layer| {
let mut relevant: BTreeSet<_> = self
.cells
.iter()
.filter(|(_, c)| {
!c.issues.is_empty()
|| c.objects.iter().any(|o| {
!o.metadata
&& (o.layer == Some(layer)
|| (!o.issues.is_empty() && o.layer.is_none()))
})
})
.map(|(n, _)| n.clone())
.collect();
let mut parents = BTreeMap::<String, Vec<String>>::new();
for (parent, cell) in &self.cells {
for o in &cell.objects {
if let Some(target) = &o.target {
parents
.entry(target.clone())
.or_default()
.push(parent.clone());
if !self.known_cells.contains(target) {
relevant.insert(parent.clone());
}
}
}
}
let mut pending: Vec<_> = relevant.iter().cloned().collect();
// A cycle cannot be proven to have no effect on a layer. Preserve
// cycles (and their descendants) so graph validation remains visible.
let mut indegree: BTreeMap<_, usize> =
self.cells.keys().map(|n| (n.clone(), 0)).collect();
for cell in self.cells.values() {
for target in cell.objects.iter().filter_map(|o| o.target.as_ref()) {
if let Some(n) = indegree.get_mut(target) {
*n += 1;
}
}
}
let mut ready: Vec<_> = indegree
.iter()
.filter(|(_, n)| **n == 0)
.map(|(n, _)| n.clone())
.collect();
while let Some(name) = ready.pop() {
for target in self.cells[&name]
.objects
.iter()
.filter_map(|o| o.target.as_ref())
{
if let Some(n) = indegree.get_mut(target) {
*n -= 1;
if *n == 0 {
ready.push(target.clone());
}
}
}
}
for (name, _) in indegree.iter().filter(|(_, n)| **n != 0) {
if relevant.insert(name.clone()) {
pending.push(name.clone());
}
}
while let Some(child) = pending.pop() {
for parent in parents.get(&child).into_iter().flatten() {
if relevant.insert(parent.clone()) {
pending.push(parent.clone());
}
}
}
relevant
});
self.cells.retain(|name, _| {
wanted.contains(name) && relevant.as_ref().is_none_or(|r| r.contains(name))
});
if let (Some(layer), Some(relevant)) = (scope.layer, relevant) {
for cell in self.cells.values_mut() {
cell.objects.retain(|o| {
o.metadata
|| o.layer == Some(layer)
|| o.target
.as_ref()
.is_some_and(|t| relevant.contains(t) || !self.known_cells.contains(t))
|| (!o.issues.is_empty() && o.layer.is_none())
});
}
self.layer_names.retain(|pair, _| *pair == layer);
} else if scope.top.is_some() || scope.cell.is_some() {
let layers: BTreeSet<_> = self
.cells
.values()
.flat_map(|c| &c.objects)
.filter(|o| !o.metadata)
.filter_map(|o| o.layer)
.collect();
self.layer_names.retain(|pair, _| layers.contains(pair));
}
self.scope = scope.clone();
self.refresh_issues();
Ok(self)
}
}