feat(metadata): write embedded layer names without rewriting design records

Consolidate sentinel declarations in an explicit existing cell and carrier pair. Preserve unrelated bytes and padding, reject ambiguous carriers and future versions, and create a separate output file without overwriting existing files.
This commit is contained in:
Jan Petykiewicz 2026-11-14 17:27:02 -08:00
commit 360e0f98f7

View file

@ -1,5 +1,8 @@
use klamath_rs_ext::review::{Comparison, Document, dump_records, hierarchy};
use klamath_rs_ext::review::{
Comparison, Document, dump_records, hierarchy, parse_layer, parse_layer_name, write_layer_names,
};
use std::{
collections::BTreeMap,
ffi::OsString,
fs,
io::{self, Write},
@ -10,6 +13,7 @@ const HELP: &str = "Usage:
klamath dump FILE [--records]
klamath diff OLD NEW [--hierarchy ascii|mermaid|none] [--max-changes N] [--depth N]
klamath hierarchy FILE [--format ascii|mermaid] [--depth N]
klamath 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.
@ -24,6 +28,55 @@ fn value(args: &[OsString], index: &mut usize) -> Result<String, String> {
.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 run() -> Result<u8, String> {
let args: Vec<_> = std::env::args_os().skip(1).collect();
if args.is_empty() || args[0] == "--help" || args[0] == "-h" {
@ -31,6 +84,9 @@ fn run() -> Result<u8, String> {
return Ok(0);
}
let command = args[0].to_str().ok_or("invalid command")?;
if command == "name-layers" {
return name_layers(&args[1..]);
}
let paths = match command {
"dump" | "hierarchy" => 1,
"diff" => 2,