use std::path::PathBuf;
use clap::Subcommand;
use blackline_docx::{track_redline, Docx, Granularity, TrackOp};
use crate::util::{atomic_write, print_json, read_json_arg, resolve_author, resolve_output};
#[derive(Subcommand)]
pub enum TrackCmd {
Apply {
file: PathBuf,
#[arg(long)]
ops: String,
#[arg(short, long)]
output: Option<PathBuf>,
#[arg(long)]
in_place: bool,
#[arg(long)]
author: Option<String>,
#[arg(long)]
date: Option<String>,
#[arg(long, default_value = "word")]
granularity: String,
#[arg(long)]
lenient: bool,
#[arg(long)]
dry_run: bool,
#[arg(long)]
json: bool,
#[arg(long)]
part: Option<String>,
},
Redline {
original: PathBuf,
revised: PathBuf,
#[arg(short, long)]
output: PathBuf,
#[arg(long)]
author: Option<String>,
#[arg(long, default_value = "word")]
granularity: String,
},
Changes {
file: PathBuf,
#[arg(long)]
author: Option<String>,
},
Comments {
file: PathBuf,
#[arg(long)]
author: Option<String>,
},
Settle {
file: PathBuf,
#[arg(long, group = "settle")]
accept: bool,
#[arg(long, group = "settle")]
reject: bool,
#[arg(long)]
author: Option<String>,
#[arg(short, long)]
output: Option<PathBuf>,
#[arg(long)]
in_place: bool,
#[arg(long)]
json: bool,
},
}
pub fn run(cmd: TrackCmd) -> Result<i32, String> {
match cmd {
TrackCmd::Apply {
file,
ops,
output,
in_place,
author,
date,
granularity,
lenient,
dry_run,
json,
part,
} => {
let raw = read_json_arg(&ops)?;
let ops: Vec<TrackOp> = serde_json::from_str(&raw)
.map_err(|e| format!("usage: invalid --ops JSON: {e}"))?;
if ops.is_empty() {
return Err("usage: --ops is an empty array".into());
}
let gran = Granularity::parse(&granularity).map_err(|e| format!("usage: {e}"))?;
let default_author = if ops.iter().any(|o| o.needs_author() && !o.has_own_author()) {
Some(resolve_author(author.as_deref())?)
} else {
author.filter(|a| !a.trim().is_empty())
};
let doc = Docx::open(&file).map_err(|e| e.to_string())?;
let mut builder = doc.track(ops).granularity(gran);
if let Some(a) = default_author {
builder = builder.author(a);
}
if let Some(d) = date.filter(|s| !s.trim().is_empty()) {
builder = builder.date(d);
}
if lenient {
builder = builder.lenient();
}
if dry_run {
builder = builder.dry_run();
}
if let Some(p) = part {
builder = builder.part(p);
}
let outcome = builder.apply().map_err(|e| e.to_string())?;
if json {
print_json(&outcome.report)?;
}
if dry_run {
return Ok(0);
}
let edited = outcome
.document
.ok_or_else(|| "track apply produced no document".to_string())?;
let (path, _) = resolve_output(&file, output.as_deref(), in_place)?;
atomic_write(&path, |tmp| edited.save(tmp).map_err(|e| e.to_string()))?;
Ok(0)
}
TrackCmd::Redline {
original,
revised,
output,
author,
granularity,
} => {
let author = resolve_author(author.as_deref())?;
let gran = Granularity::parse(&granularity).map_err(|e| format!("usage: {e}"))?;
let a = Docx::open(&original).map_err(|e| e.to_string())?;
let b = Docx::open(&revised).map_err(|e| e.to_string())?;
let out = track_redline(&a, &b, &author, gran).map_err(|e| e.to_string())?;
atomic_write(&output, |tmp| out.save(tmp).map_err(|e| e.to_string()))?;
Ok(0)
}
TrackCmd::Changes { file, author } => {
let doc = Docx::open(&file).map_err(|e| e.to_string())?;
let mut changes = doc.changes().map_err(|e| e.to_string())?;
if let Some(want) = author {
changes.retain(|c| c.author == want);
}
print_json(&changes)?;
Ok(0)
}
TrackCmd::Comments { file, author } => {
let doc = Docx::open(&file).map_err(|e| e.to_string())?;
let mut comments = doc.comments().map_err(|e| e.to_string())?;
if let Some(want) = author {
comments.retain(|c| c.author == want);
}
print_json(&comments)?;
Ok(0)
}
TrackCmd::Settle {
file,
accept,
reject,
author,
output,
in_place,
json,
} => {
if accept == reject {
return Err("usage: pass exactly one of --accept or --reject".into());
}
let op = if accept {
TrackOp::AcceptAll { author }
} else {
TrackOp::RejectAll { author }
};
let doc = Docx::open(&file).map_err(|e| e.to_string())?;
let outcome = doc.track([op]).apply().map_err(|e| e.to_string())?;
if json {
print_json(&outcome.report)?;
}
let edited = outcome
.document
.ok_or_else(|| "settle produced no document".to_string())?;
let (path, _) = resolve_output(&file, output.as_deref(), in_place)?;
atomic_write(&path, |tmp| edited.save(tmp).map_err(|e| e.to_string()))?;
Ok(0)
}
}
}