jubarte_cli/cli/
edit_template.rs1use clap::{Args, Subcommand};
7use serde::Serialize;
8use std::path::PathBuf;
9
10#[derive(Args, Debug, Serialize)]
12pub struct TemplateDispatch {
13 #[command(subcommand)]
15 #[serde(flatten)]
16 pub action: TemplateAction,
17}
18
19#[derive(Subcommand, Debug, Serialize)]
21#[serde(tag = "action", rename_all = "snake_case")]
22pub enum TemplateAction {
23 #[command(override_usage = "jubarte edit template [OPTIONS] <FILE> (-o PLAN.json|--dry-run)")]
25 Template(TemplateOptions),
26}
27
28#[derive(Args, Debug, Serialize)]
30#[command(
31 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"
32)]
33pub struct TemplateOptions {
34 pub file: PathBuf,
36 #[arg(short, long, value_name = "PLAN.json")]
38 pub output: Option<PathBuf>,
39 #[arg(long, value_name = "IDS")]
41 pub select: Option<String>,
42 #[arg(long, default_value = "all")]
44 pub story: String,
45 #[arg(long, conflicts_with = "regex")]
47 pub find: Option<String>,
48 #[arg(long)]
50 pub regex: Option<String>,
51 #[arg(long)]
53 pub ignore_case: bool,
54 #[arg(long)]
56 pub changed: bool,
57 #[arg(long, requires = "changed", value_parser = author_filter)]
59 pub by: Vec<String>,
60 #[arg(long, default_value_t = 0)]
62 pub context: usize,
63 #[arg(long)]
65 pub accept_change: Vec<String>,
66 #[arg(long)]
68 pub reject_change: Vec<String>,
69 #[arg(short = 'a', long, default_value = "Jubarte", value_parser = super::parse_author)]
71 pub author: String,
72 #[arg(short = 'd', long, value_parser = super::parse_datetime)]
74 pub datetime: Option<String>,
75 #[arg(long, default_value = "on", value_parser = ["on", "off"])]
77 pub track: String,
78 #[arg(long, default_value = "keep", value_parser = ["keep", "accept", "reject", "refuse"])]
80 pub existing_revisions: String,
81 #[arg(long)]
88 pub keep_others: bool,
89 #[arg(long, value_name = "HASH", value_parser = super::edit_actions::digest)]
91 pub expect_sha256: Option<String>,
92 #[arg(long)]
94 pub dry_run: bool,
95 #[arg(long, value_name = "FILE")]
97 pub report: Option<PathBuf>,
98 #[arg(long)]
100 pub force: bool,
101 #[arg(long, conflicts_with = "jsonl")]
103 pub json: bool,
104 #[arg(long)]
106 pub jsonl: bool,
107 #[arg(long, value_name = "PLAN.json")]
112 pub refresh_plan: Option<PathBuf>,
113}
114
115pub(super) fn validate(
116 args: &clap::ArgMatches,
117 model: &mut clap::Command,
118) -> Result<(), clap::Error> {
119 let (_, args) = args.subcommand().expect("template action required");
120 let error = |message| {
121 model
122 .clone()
123 .error(clap::error::ErrorKind::ArgumentConflict, message)
124 };
125 let output = args.get_one::<PathBuf>("output");
126 let raw = output.is_some_and(|p| p == std::path::Path::new("-"));
127 if output.is_none() && !args.get_flag("dry_run") {
128 return Err(error(
129 "template generation requires -o PLAN.json, explicit -o -, or --dry-run",
130 ));
131 }
132 if raw
133 && ["json", "jsonl", "dry_run"]
134 .iter()
135 .any(|flag| args.get_flag(flag))
136 {
137 return Err(error(
138 "-o - writes only raw PlanV2 JSON; it conflicts with --json, --jsonl and --dry-run",
139 ));
140 }
141 Ok(())
142}
143
144fn author_filter(value: &str) -> Result<String, String> {
145 crate::markdown::author(value).map(str::to_string)
146}