feat(review): add deterministic GDS dumps and structural comparisons
Read embedded layer-name labels, retain unsupported records, and report affected hierarchy as ASCII or Mermaid. Preserve duplicate objects, canonicalize polygon rings, and cover the first profile with ten fixture pairs and golden outputs.
This commit is contained in:
parent
4108baf0a1
commit
a04a955e90
1 changed files with 2160 additions and 0 deletions
139
src/bin/klamath.rs
Normal file
139
src/bin/klamath.rs
Normal file
|
|
@ -0,0 +1,139 @@
|
|||
use klamath_rs_ext::review::{Comparison, Document, dump_records, hierarchy};
|
||||
use std::{
|
||||
ffi::OsString,
|
||||
fs,
|
||||
io::{self, Write},
|
||||
process::ExitCode,
|
||||
};
|
||||
|
||||
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]
|
||||
|
||||
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 run() -> Result<u8, String> {
|
||||
let args: Vec<_> = std::env::args_os().skip(1).collect();
|
||||
if args.is_empty() || args[0] == "--help" || args[0] == "-h" {
|
||||
print!("{HELP}");
|
||||
return Ok(0);
|
||||
}
|
||||
let command = args[0].to_str().ok_or("invalid command")?;
|
||||
let paths = match command {
|
||||
"dump" | "hierarchy" => 1,
|
||||
"diff" => 2,
|
||||
_ => return Err(HELP.to_string()),
|
||||
};
|
||||
if args.len() <= paths {
|
||||
return Err(HELP.to_string());
|
||||
}
|
||||
let mut raw = false;
|
||||
let mut format = if command == "diff" {
|
||||
"none".to_string()
|
||||
} else {
|
||||
"ascii".to_string()
|
||||
};
|
||||
let mut max_changes = 100;
|
||||
let mut depth = 8;
|
||||
let mut i = paths + 1;
|
||||
while i < args.len() {
|
||||
match args[i].to_str() {
|
||||
Some("--records") if command == "dump" => raw = true,
|
||||
Some("--hierarchy") if command == "diff" => format = value(&args, &mut i)?,
|
||||
Some("--format") if command == "hierarchy" => format = value(&args, &mut i)?,
|
||||
Some("--max-changes") if command == "diff" => {
|
||||
max_changes = value(&args, &mut i)?
|
||||
.parse()
|
||||
.map_err(|_| "invalid change limit")?
|
||||
}
|
||||
Some("--depth") if command != "dump" => {
|
||||
depth = value(&args, &mut i)?.parse().map_err(|_| "invalid depth")?
|
||||
}
|
||||
_ => return Err(format!("unknown option {:?}\n{HELP}", args[i])),
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
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 old_bytes = fs::read(&args[1]).map_err(|e| format!("{:?}: {e}", args[1]))?;
|
||||
if raw {
|
||||
io::stdout()
|
||||
.lock()
|
||||
.write_all(dump_records(&old_bytes)?.as_bytes())
|
||||
.map_err(|e| e.to_string())?;
|
||||
return Ok(0);
|
||||
}
|
||||
let old = Document::parse(&old_bytes)?;
|
||||
let (output, status) = match command {
|
||||
"dump" => (
|
||||
old.canonical_dump(),
|
||||
if old.issues.is_empty() { 0 } else { 2 },
|
||||
),
|
||||
"hierarchy" => {
|
||||
let mut output = hierarchy(&old, &old, None, format == "mermaid", depth);
|
||||
if format != "mermaid" {
|
||||
for issue in &old.issues {
|
||||
output.push_str(&format!("INCOMPLETE: {issue}\n"));
|
||||
}
|
||||
} else {
|
||||
for issue in &old.issues {
|
||||
eprintln!("INCOMPLETE: {issue}");
|
||||
}
|
||||
}
|
||||
(output, if old.issues.is_empty() { 0 } else { 2 })
|
||||
}
|
||||
"diff" => {
|
||||
let new_bytes = fs::read(&args[2]).map_err(|e| format!("{:?}: {e}", args[2]))?;
|
||||
let new = Document::parse(&new_bytes)?;
|
||||
let diff = Comparison::between(&old, &new);
|
||||
let mut output = diff.text(max_changes);
|
||||
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");
|
||||
}
|
||||
}
|
||||
(output, diff.exit_code())
|
||||
}
|
||||
_ => unreachable!(),
|
||||
};
|
||||
io::stdout()
|
||||
.lock()
|
||||
.write_all(output.as_bytes())
|
||||
.map_err(|e| e.to_string())?;
|
||||
Ok(status)
|
||||
}
|
||||
|
||||
fn main() -> ExitCode {
|
||||
match run() {
|
||||
Ok(status) => ExitCode::from(status),
|
||||
Err(message) => {
|
||||
eprintln!("klamath: {message}");
|
||||
ExitCode::from(2)
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue