feat(review): add scoped reports and native Git integration
Separate top, exact-cell and layer comparison scopes from display filters. Explain unique replacements and preserve full evidence, duplicate counts and incomplete-result handling. Version the dump profile and freeze canonical, hierarchy and diff goldens. Package native Git and textconv drivers with integration coverage for edits, additions, deletions, renames, quoted paths and errors. Document embedded metadata writing and validate deep hierarchies, large polygons and symbolic arrays.
This commit is contained in:
parent
e56026c3a6
commit
d8f764c7fe
1 changed files with 1191 additions and 97 deletions
|
|
@ -1,5 +1,6 @@
|
|||
use klamath_rs_ext::review::{
|
||||
Comparison, Document, dump_records, hierarchy, parse_layer, parse_layer_name, write_layer_names,
|
||||
Comparison, DisplayFilter, Document, Scope, dump_records, hierarchy, parse_layer,
|
||||
parse_layer_name, write_layer_names,
|
||||
};
|
||||
use std::{
|
||||
collections::BTreeMap,
|
||||
|
|
@ -11,8 +12,8 @@ use std::{
|
|||
|
||||
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 diff OLD NEW [--top NAME] [--cell NAME] [--layer L/T]\n [--show-cell NAME] [--show-layer L/T] [--hierarchy ascii|mermaid|none]\n [--max-changes N] [--depth N]
|
||||
klamath hierarchy FILE [--format ascii|mermaid] [--depth N] [--top NAME]\n klamath git-diff [OPTIONS] -- PATH OLD OLD_OID OLD_MODE NEW NEW_OID NEW_MODE
|
||||
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,
|
||||
|
|
@ -77,39 +78,66 @@ fn name_layers(args: &[OsString]) -> Result<u8, String> {
|
|||
Ok(0)
|
||||
}
|
||||
|
||||
fn stdout(output: &str) -> Result<(), String> {
|
||||
io::stdout()
|
||||
.lock()
|
||||
.write_all(output.as_bytes())
|
||||
.map_err(|e| e.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}");
|
||||
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 paths = match command {
|
||||
let expected = match command {
|
||||
"dump" | "hierarchy" => 1,
|
||||
"diff" => 2,
|
||||
"git-diff" => 7,
|
||||
_ => return Err(HELP.to_string()),
|
||||
};
|
||||
if args.len() <= paths {
|
||||
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 command == "diff" {
|
||||
let mut format = if compare {
|
||||
"none".to_string()
|
||||
} else {
|
||||
"ascii".to_string()
|
||||
};
|
||||
let mut max_changes = 100;
|
||||
let mut depth = 8;
|
||||
let mut i = paths + 1;
|
||||
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("--hierarchy") if command == "diff" => format = value(&args, &mut i)?,
|
||||
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("--max-changes") if command == "diff" => {
|
||||
Some("--max-changes") if compare => {
|
||||
max_changes = value(&args, &mut i)?
|
||||
.parse()
|
||||
.map_err(|_| "invalid change limit")?
|
||||
|
|
@ -117,71 +145,107 @@ fn run() -> Result<u8, String> {
|
|||
Some("--depth") if command != "dump" => {
|
||||
depth = value(&args, &mut i)?.parse().map_err(|_| "invalid depth")?
|
||||
}
|
||||
_ => return Err(format!("unknown option {:?}\n{HELP}", args[i])),
|
||||
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!(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]))?;
|
||||
let read = |path: &OsString| fs::read(path).map_err(|e| format!("{path:?}: {e}"));
|
||||
if raw {
|
||||
io::stdout()
|
||||
.lock()
|
||||
.write_all(dump_records(&old_bytes)?.as_bytes())
|
||||
.map_err(|e| e.to_string())?;
|
||||
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);
|
||||
}
|
||||
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}");
|
||||
}
|
||||
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}");
|
||||
}
|
||||
(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!(),
|
||||
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 }])?)
|
||||
};
|
||||
io::stdout()
|
||||
.lock()
|
||||
.write_all(output.as_bytes())
|
||||
.map_err(|e| e.to_string())?;
|
||||
Ok(status)
|
||||
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 main() -> ExitCode {
|
||||
|
|
|
|||
Loading…
Reference in a new issue