jubarte-cli 0.20.0

The jubarte engine's command line: every operation of the library behind one clap surface, plus self-update
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
// SPDX-FileCopyrightText: 2026 Jandira Technologies, LLC
//
// SPDX-License-Identifier: Apache-2.0

//! Typed edit grammar. The legacy `edit FILE` grammar remains versioned.

use std::{ffi::OsString, path::PathBuf};

use clap::{Args, Subcommand};
use serde::Serialize;

/// The typed edit route, selected before legacy flag grouping.
#[derive(Args, Debug, Serialize)]
pub struct TypedEdit {
    /// Exactly one operation grammar, or application of a saved plan.
    #[command(subcommand)]
    #[serde(flatten)]
    pub action: EditAction,
}

/// The initial PlanV2 text action profile.
#[derive(Subcommand, Debug, Serialize)]
#[serde(tag = "action", rename_all = "snake_case")]
pub enum EditAction {
    /// Replace exact literal text; without --at, search one declared story.
    #[command(
        override_usage = "jubarte edit replace [OPTIONS] <FILE> --find TEXT (--text TEXT|--text-file FILE)"
    )]
    Replace {
        /// Frozen policy and new-edit attribution.
        #[command(flatten)]
        inline: InlineOptions,
        /// Exact nonempty literal text.
        #[arg(long)]
        find: String,
        /// Original paragraph ID from read; never silently retargeted.
        #[arg(long)]
        at: Option<String>,
        /// Search story when --at is omitted (default body).
        #[arg(long, conflicts_with = "at")]
        story: Option<String>,
        /// 1-based exact match, including overlapping candidate starts.
        #[arg(long, value_parser = positive, conflicts_with = "all_matches")]
        occurrence: Option<usize>,
        /// Replace every candidate; refuses when any two candidates overlap. Requires --expect-count.
        #[arg(long, requires = "expect_count")]
        all_matches: bool,
        /// Required complete candidate count for --all-matches.
        #[arg(long, requires = "all_matches", value_parser = positive)]
        expect_count: Option<usize>,
        /// Exact text and optional attached comment.
        #[command(flatten)]
        payload: TextPayload,
    },
    /// Set exact desired paragraph text; an empty value retains the paragraph.
    #[command(
        override_usage = "jubarte edit rewrite [OPTIONS] <FILE> --at ID (--text TEXT|--text-file FILE)"
    )]
    Rewrite {
        /// Frozen policy and new-edit attribution.
        #[command(flatten)]
        inline: InlineOptions,
        /// Original paragraph ID.
        #[arg(long)]
        at: String,
        /// Exact text and optional attached comment.
        #[command(flatten)]
        payload: TextPayload,
    },
    /// Insert an explicitly typed text or paragraph payload.
    #[command(
        override_usage = "jubarte edit insert [OPTIONS] <FILE> --kind text|paragraph --at ID (--text TEXT|--text-file FILE)"
    )]
    Insert {
        /// Frozen policy and new-edit attribution.
        #[command(flatten)]
        inline: InlineOptions,
        /// Object type; position never infers the kind.
        #[arg(long, value_parser = ["text", "paragraph"])]
        kind: String,
        /// Original paragraph ID.
        #[arg(long)]
        at: String,
        /// Insert text before this exact nonempty literal.
        #[arg(long, conflicts_with_all = ["after_text", "edge", "position"])]
        before_text: Option<String>,
        /// Insert text after this exact nonempty literal.
        #[arg(long, conflicts_with_all = ["edge", "position"])]
        after_text: Option<String>,
        /// Insert text at a paragraph edge.
        #[arg(long, value_parser = ["start", "end"], conflicts_with_all = ["position", "occurrence"])]
        edge: Option<String>,
        /// Insert a paragraph beside the target paragraph.
        #[arg(long, value_parser = ["before", "after"], conflicts_with = "occurrence")]
        position: Option<String>,
        /// 1-based literal candidate.
        #[arg(long, value_parser = positive)]
        occurrence: Option<usize>,
        /// Exact text and optional attached comment.
        #[command(flatten)]
        payload: TextPayload,
    },
    /// Delete one exact range, or explicitly delete a whole paragraph.
    #[command(
        override_usage = "jubarte edit delete [OPTIONS] <FILE> --at ID (--find TEXT|--whole-block)"
    )]
    Delete {
        /// Frozen policy and new-edit attribution.
        #[command(flatten)]
        inline: InlineOptions,
        /// Original paragraph ID; mandatory even for a range deletion.
        #[arg(long)]
        at: String,
        /// Exact nonempty literal range to remove.
        #[arg(
            long,
            required_unless_present = "whole_block",
            conflicts_with = "whole_block"
        )]
        find: Option<String>,
        /// Explicit structural deletion (PlanV2 delete_paragraph).
        #[arg(long, required_unless_present = "find", conflicts_with = "occurrence")]
        whole_block: bool,
        /// 1-based literal candidate.
        #[arg(long, value_parser = positive, requires = "find")]
        occurrence: Option<usize>,
    },
    /// Apply only: never record into, consume, or rewrite the plan.
    // apply has no --save-plan, so its output is required unless it bundles
    // or dry-runs.
    #[command(
        override_usage = "jubarte edit apply [OPTIONS] <FILE> --plan FILE",
        mut_arg("output", |a| a.help("Primary edited DOCX; required unless bundling, replacing FILE or dry-running"))
    )]
    Apply {
        /// Immutable source document.
        file: PathBuf,
        /// Input plan; v2 policy and attribution belong exclusively to it.
        #[arg(long, value_name = "FILE", value_parser = super::plan_path())]
        plan: PathBuf,
        /// Advanced v1-only waiver when neither plan nor CLI supplies a hash.
        #[arg(long)]
        allow_unbound_plan: bool,
        /// Publication and source preconditions.
        #[command(flatten)]
        delivery: DeliveryOptions,
    },
}

/// Exact text input; file bytes must be UTF-8 and are never trimmed.
#[derive(Args, Debug, Serialize)]
#[group(skip)]
pub struct TextPayload {
    /// Exact plain text (not Markdown); empty text is meaningful.
    #[arg(
        long,
        required_unless_present = "text_file",
        conflicts_with = "text_file"
    )]
    pub text: Option<String>,
    /// Exact UTF-8 file; all trailing newlines are preserved.
    #[arg(long, value_name = "FILE", required_unless_present = "text")]
    pub text_file: Option<PathBuf>,
    /// Attach a comment to the nonempty resulting text.
    #[arg(long)]
    pub comment: Option<String>,
}

/// Policy and attribution for a newly compiled inline plan.
#[derive(Args, Debug, Serialize)]
pub struct InlineOptions {
    /// Immutable source document.
    pub file: PathBuf,
    /// New revision and comment author. A write that adds markup needs it
    /// spelled here or in JUBARTE_AUTHOR.
    #[arg(short = 'a', long, default_value = "Jubarte", value_parser = super::parse_author)]
    pub author: String,
    /// Whether `-a`/`--author` was spelled on the command line (set after
    /// parsing). The typed receipt uses this to say when the default signed
    /// the new markup (#1891).
    #[arg(skip)]
    #[serde(skip)]
    pub author_given: bool,
    /// Frozen timestamp; timezone required. Default current UTC.
    #[arg(short = 'd', long = "datetime", value_parser = super::parse_datetime)]
    pub datetime: Option<String>,
    /// Track new edits, preserving earlier history according to policy.
    #[arg(long, default_value = "on", value_parser = ["on", "off"])]
    pub track: String,
    /// Policy for history remaining after explicit selected decisions.
    #[arg(long, default_value = "keep", value_parser = ["keep", "accept", "reject", "refuse"])]
    pub existing_revisions: String,
    /// Original-source revision ID to accept; repeat to select a set.
    #[arg(long)]
    pub accept_change: Vec<String>,
    /// Original-source revision ID to reject; repeat to select a set.
    #[arg(long)]
    pub reject_change: Vec<String>,
    /// Save a source-bound PlanV2 without applying. Freezes targets, policy and time.
    #[arg(long, value_name = "FILE", value_parser = super::plan_path(), conflicts_with_all = ["output", "out_dir", "report", "emit", "expect", "dry_run", "in_place"])]
    pub save_plan: Option<PathBuf>,
    /// Publication and source preconditions.
    #[command(flatten)]
    pub delivery: DeliveryOptions,
}

/// Publication and preparation options; never serialized into a saved plan.
#[derive(Args, Debug, Serialize)]
pub struct DeliveryOptions {
    /// Replace FILE through the same atomic write as -o. A refused edit leaves FILE unchanged. Not with -o or --out-dir.
    #[arg(long, conflicts_with_all = ["output", "out_dir", "dry_run"])]
    pub in_place: bool,
    /// Primary edited DOCX; required unless saving, bundling, replacing FILE or dry-running.
    #[arg(short = 'o', long, value_name = "FILE", conflicts_with = "out_dir")]
    pub output: Option<PathBuf>,
    /// Publish a new bundle directory (document.docx, changes.diff, report.json).
    #[arg(long, value_name = "NEW_DIR")]
    pub out_dir: Option<PathBuf>,
    /// JSON receipt, irrespective of filename suffix.
    #[arg(long, value_name = "FILE")]
    pub report: Option<PathBuf>,
    /// Bundle roles: document,patch,report (primary document always included).
    #[arg(long, value_delimiter = ',', value_parser = ["document", "patch", "report"])]
    pub emit: Vec<String>,
    /// Optional expectation contract, evaluated on the prepared primary
    /// before publication (PlanV2 requests only; use `jubarte check` for a
    /// standalone pass over a published output).
    #[arg(long, value_name = "FILE")]
    pub expect: Option<PathBuf>,
    /// Redundant source digest must agree with a plan's embedded digest.
    #[arg(long, value_name = "HASH", value_parser = digest)]
    pub expect_sha256: Option<String>,
    /// Prepare and validate the actual candidate; write no files.
    #[arg(long, conflicts_with_all = ["output", "out_dir"])]
    pub dry_run: bool,
    /// Require a primary document change, independently of derived artifacts.
    #[arg(long)]
    pub require_change: bool,
    /// Replace an explicitly named existing output; never overwrite an input.
    #[arg(long)]
    pub force: bool,
    /// Refuse to accept or reject another author's tracked change, and
    /// to delete, resolve, reopen or reword their comment. Restore,
    /// strike, a comment and a reply stay allowed. JUBARTE_KEEP_OTHERS=1
    /// sets the same guard. Another author is anyone other than
    /// --author (Jubarte when --author is left out).
    #[arg(long)]
    pub keep_others: bool,
    /// One result envelope, including empty results and refusals.
    #[arg(long, conflicts_with = "jsonl")]
    pub json: bool,
    /// Header and exactly one terminal result.
    #[arg(long)]
    pub jsonl: bool,
}

pub(super) fn positive(s: &str) -> Result<usize, String> {
    s.parse()
        .ok()
        .filter(|n| *n > 0)
        .ok_or_else(|| "must be a positive, 1-based integer".into())
}
pub(super) fn digest(s: &str) -> Result<String, String> {
    if s.len() == 64
        && s.bytes()
            .all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b))
    {
        Ok(s.into())
    } else {
        Err("must be 64 lowercase hexadecimal characters".into())
    }
}

/// Route typed actions only when the tokens are not a valid legacy invocation.
/// No filesystem probing: a filename cannot change parsing by appearing/disappearing.
/// `./replace` always denotes a legacy filename; bare `replace -p ...` remains v1.
pub fn route(arguments: &[OsString], model: &clap::Command) -> Vec<OsString> {
    let mut routed = arguments.to_vec();
    if arguments.get(1).is_none_or(|a| a != "edit") {
        return routed;
    }
    let action = arguments.iter().skip(2).find(|a| {
        !a.to_str().is_some_and(|s| {
            s == "--quiet" || s.starts_with('-') && s.len() > 1 && s[1..].bytes().all(|b| b == b'q')
        })
    });
    if !action.is_some_and(|a| {
        matches!(
            a.to_str(),
            Some("replace" | "rewrite" | "insert" | "delete" | "apply" | "template")
        )
    }) {
        return routed;
    }
    if model.clone().try_get_matches_from(arguments).is_ok() {
        return routed;
    }
    if action.is_some_and(|a| a == "template") {
        routed[1] = "__edit-template".into();
        return routed;
    }
    routed[1] = "__edit-v2".into();
    routed
}

pub(super) fn validate(
    args: &clap::ArgMatches,
    model: &mut clap::Command,
) -> Result<(), clap::Error> {
    let (action, args) = args.subcommand().expect("typed action required");
    let present = |id| {
        args.try_contains_id(id).unwrap_or(false)
            && args.value_source(id) == Some(clap::parser::ValueSource::CommandLine)
    };
    let error = |message: &str| {
        model
            .clone()
            .error(clap::error::ErrorKind::ArgumentConflict, message)
    };
    if !["output", "out_dir", "dry_run", "save_plan", "in_place"]
        .into_iter()
        .any(present)
    {
        return Err(error(
            "writes require -o FILE or --out-dir NEW_DIR or --in-place; use --dry-run or inline --save-plan FILE",
        ));
    }
    if action == "insert" {
        let paragraph = args
            .get_one::<String>("kind")
            .is_some_and(|s| s == "paragraph");
        if paragraph != present("position")
            || !paragraph
                && !["before_text", "after_text", "edge"]
                    .into_iter()
                    .any(present)
        {
            return Err(error(
                "--kind paragraph requires --position before|after; --kind text requires --before-text, --after-text or --edge",
            ));
        }
    }
    Ok(())
}

impl EditAction {
    /// Canonical action name for receipts.
    pub fn name(&self) -> &'static str {
        match self {
            Self::Replace { .. } => "replace",
            Self::Rewrite { .. } => "rewrite",
            Self::Insert { .. } => "insert",
            Self::Delete { .. } => "delete",
            Self::Apply { .. } => "apply",
        }
    }
    /// Inline policy, when this action compiles a new plan.
    pub fn inline(&self) -> Option<&InlineOptions> {
        match self {
            Self::Replace { inline, .. }
            | Self::Rewrite { inline, .. }
            | Self::Insert { inline, .. }
            | Self::Delete { inline, .. } => Some(inline),
            Self::Apply { .. } => None,
        }
    }
    /// Inline policy, mutable, when this action compiles a new plan.
    pub fn inline_mut(&mut self) -> Option<&mut InlineOptions> {
        match self {
            Self::Replace { inline, .. }
            | Self::Rewrite { inline, .. }
            | Self::Insert { inline, .. }
            | Self::Delete { inline, .. } => Some(inline),
            Self::Apply { .. } => None,
        }
    }
    /// Whether `-a` was spelled for this action. `edit apply` has none:
    /// the plan's author is the signature.
    pub fn author_was_given(&self) -> bool {
        self.inline().is_none_or(|inline| inline.author_given)
    }
    /// Exact payload or resource input for actions with text.
    pub fn payload(&self) -> Option<&TextPayload> {
        match self {
            Self::Replace { payload, .. }
            | Self::Rewrite { payload, .. }
            | Self::Insert { payload, .. } => Some(payload),
            _ => None,
        }
    }
    /// Source path.
    pub fn file(&self) -> &std::path::Path {
        match self {
            Self::Apply { file, .. } => file,
            _ => &self.inline().expect("inline").file,
        }
    }
    /// Delivery options, independent of plan-owned policy.
    pub fn delivery(&self) -> &DeliveryOptions {
        match self {
            Self::Apply { delivery, .. } => delivery,
            _ => &self.inline().expect("inline").delivery,
        }
    }
}