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
4 changed files with 12956 additions and 1 deletions

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)
}
}