use clap::{Args, Subcommand};
use serde::Serialize;
use std::path::PathBuf;
#[derive(Args, Debug, Serialize)]
pub struct TemplateDispatch {
#[command(subcommand)]
#[serde(flatten)]
pub action: TemplateAction,
}
#[derive(Subcommand, Debug, Serialize)]
#[serde(tag = "action", rename_all = "snake_case")]
pub enum TemplateAction {
#[command(override_usage = "jubarte edit template [OPTIONS] <FILE> (-o PLAN.json|--dry-run)")]
Template(TemplateOptions),
}
#[derive(Args, Debug, Serialize)]
#[command(
after_help = "Change a row's text to request a rewrite. Keeping or removing an unchanged row requests no rewrite.\nUse explicit typed operations for insertions, deletions and comments.\n\nExamples:\n jubarte edit template contract.docx --select p12,p20-p25 -o edits.json\n jubarte edit template contract.docx --story all -o edits.json\n jubarte edit apply contract.docx --plan edits.json -o revised.docx --json"
)]
pub struct TemplateOptions {
pub file: PathBuf,
#[arg(short, long, value_name = "PLAN.json")]
pub output: Option<PathBuf>,
#[arg(long, value_name = "IDS")]
pub select: Option<String>,
#[arg(long, default_value = "all")]
pub story: String,
#[arg(long, conflicts_with = "regex")]
pub find: Option<String>,
#[arg(long)]
pub regex: Option<String>,
#[arg(long)]
pub ignore_case: bool,
#[arg(long)]
pub changed: bool,
#[arg(long, requires = "changed", value_parser = author_filter)]
pub by: Vec<String>,
#[arg(long, default_value_t = 0)]
pub context: usize,
#[arg(long)]
pub accept_change: Vec<String>,
#[arg(long)]
pub reject_change: Vec<String>,
#[arg(short = 'a', long, default_value = "Jubarte", value_parser = super::parse_author)]
pub author: String,
#[arg(short = 'd', long, value_parser = super::parse_datetime)]
pub datetime: Option<String>,
#[arg(long, default_value = "on", value_parser = ["on", "off"])]
pub track: String,
#[arg(long, default_value = "keep", value_parser = ["keep", "accept", "reject", "refuse"])]
pub existing_revisions: String,
#[arg(long)]
pub keep_others: bool,
#[arg(long, value_name = "HASH", value_parser = super::edit_actions::digest)]
pub expect_sha256: Option<String>,
#[arg(long)]
pub dry_run: bool,
#[arg(long, value_name = "FILE")]
pub report: Option<PathBuf>,
#[arg(long)]
pub force: bool,
#[arg(long, conflicts_with = "jsonl")]
pub json: bool,
#[arg(long)]
pub jsonl: bool,
#[arg(long, value_name = "PLAN.json")]
pub refresh_plan: Option<PathBuf>,
}
pub(super) fn validate(
args: &clap::ArgMatches,
model: &mut clap::Command,
) -> Result<(), clap::Error> {
let (_, args) = args.subcommand().expect("template action required");
let error = |message| {
model
.clone()
.error(clap::error::ErrorKind::ArgumentConflict, message)
};
let output = args.get_one::<PathBuf>("output");
let raw = output.is_some_and(|p| p == std::path::Path::new("-"));
if output.is_none() && !args.get_flag("dry_run") {
return Err(error(
"template generation requires -o PLAN.json, explicit -o -, or --dry-run",
));
}
if raw
&& ["json", "jsonl", "dry_run"]
.iter()
.any(|flag| args.get_flag(flag))
{
return Err(error(
"-o - writes only raw PlanV2 JSON; it conflicts with --json, --jsonl and --dry-run",
));
}
Ok(())
}
fn author_filter(value: &str) -> Result<String, String> {
crate::markdown::author(value).map(str::to_string)
}