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

@ -666,3 +666,119 @@ fn cli_uses_documented_exit_codes_and_accepts_paths_with_spaces() {
Some(2)
);
}
#[test]
fn metadata_writer_preserves_unrelated_bytes_and_consolidates_updates() {
use klamath_rs_ext::review::write_layer_names;
use std::collections::BTreeMap;
let mut bytes = shared(with_property(square(), "keep this"));
bytes.extend_from_slice(&[0; 32]);
let updates = BTreeMap::from([
((1, 0), "WG".to_string()),
((2, 3), "HEATER:draw".to_string()),
]);
let output = write_layer_names(&bytes, "CHIP", (900, 7), &updates).unwrap();
let doc = Document::parse(&output).unwrap();
assert_eq!(doc.layer_names, updates);
let meta: Vec<_> = doc.cells["CHIP"]
.objects
.iter()
.filter(|o| o.layer == Some((900, 7)))
.collect();
let start = meta.iter().map(|o| o.offset).min().unwrap();
let end = meta.iter().map(|o| o.end_offset).max().unwrap();
assert_eq!([&output[..start], &output[end..]].concat(), bytes);
let updated = write_layer_names(
&output,
"CHIP",
(900, 7),
&BTreeMap::from([((1, 0), "CORE".to_string())]),
)
.unwrap();
let parsed = Document::parse(&updated).unwrap();
assert_eq!(parsed.layer_names[&(1, 0)], "CORE");
assert_eq!(parsed.layer_names[&(2, 3)], "HEATER:draw");
assert_eq!(
parsed.cells["CHIP"]
.objects
.iter()
.filter(|o| o.layer == Some((900, 7)))
.count(),
3
);
let diff = Comparison::between(&Document::parse(&output).unwrap(), &parsed);
assert!(diff.cells.is_empty());
assert_eq!(diff.layer_names.len(), 1);
assert_eq!(
write_layer_names(&updated, "CHIP", (900, 7), &BTreeMap::new()).unwrap(),
updated
);
}
#[test]
fn metadata_writer_rejects_ambiguous_carriers_versions_and_bad_names() {
use klamath_rs_ext::review::{parse_layer_name, write_layer_names};
use std::collections::BTreeMap;
let bytes = layout(&[("CHIP", vec![label((900, 7), "ordinary label")])]);
assert!(
write_layer_names(&bytes, "CHIP", (900, 7), &BTreeMap::new())
.unwrap_err()
.contains("ordinary design")
);
assert!(
write_layer_names(&bytes, "missing", (901, 7), &BTreeMap::new())
.unwrap_err()
.contains("does not exist")
);
let bytes = layout(&[("CHIP", vec![label((900, 7), "KLAMATH_LAYER_NAMES_V2")])]);
assert!(
write_layer_names(&bytes, "CHIP", (901, 7), &BTreeMap::new())
.unwrap_err()
.contains("unsupported")
);
let bytes = layout(&[("CHIP", vec![square()])]);
for value in ["".to_string(), "bad\0name".to_string(), "x".repeat(65536)] {
assert!(
write_layer_names(&bytes, "CHIP", (901, 7), &BTreeMap::from([((1, 0), value)]))
.is_err()
);
}
for text in ["+1:0:name", "1:-1:name", "1:0:", "65536:0:name"] {
assert!(parse_layer_name(text).is_err());
}
}
#[test]
fn metadata_cli_creates_a_separate_file_and_refuses_existing_output() {
let directory = std::env::temp_dir().join(format!("klamath-metadata-{}", std::process::id()));
fs::create_dir_all(&directory).unwrap();
let source = directory.join("source.gds");
let output = directory.join("output.gds");
let bytes = shared(square());
fs::write(&source, &bytes).unwrap();
let _ = fs::remove_file(&output);
let mut command = Command::new(env!("CARGO_BIN_EXE_klamath"));
command.arg("name-layers").arg(&source).arg(&output).args([
"--cell",
"CHIP",
"--carrier",
"900/7",
"--name",
"1:0:WG",
]);
let result = command.output().unwrap();
assert!(
result.status.success(),
"{}",
String::from_utf8_lossy(&result.stderr)
);
assert_eq!(fs::read(&source).unwrap(), bytes);
let generated = fs::read(&output).unwrap();
assert_eq!(
Document::parse(&generated).unwrap().layer_names[&(1, 0)],
"WG"
);
assert_eq!(command.output().unwrap().status.code(), Some(2));
assert_eq!(fs::read(&output).unwrap(), generated);
fs::remove_dir_all(directory).unwrap();
}