perforce-cli 0.1.0-alpha.2

A type-safe builder library for spawning Perforce (p4) commands, with compile-time option state isolation and multi-version support.
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
pub mod add;
pub mod admin;
#[cfg(not(feature = "lt2016_1"))]
pub mod aliases;
pub mod annotate;
pub mod archive;
pub mod attribute;
pub mod changes;
pub mod describe;
pub mod diff;
pub mod diff2;
pub mod edit;
pub mod filelog;
pub mod print;
pub mod sync;
pub mod r#where;

pub use add::Add;
pub use admin::AdminEntry;
#[cfg(not(feature = "lt2016_1"))]
pub use aliases::Aliases;
pub use annotate::Annotate;
pub use archive::Archive;
pub use attribute::Attribute;
pub use changes::Changes;
pub use describe::Describe;
pub use diff::Diff;
pub use diff::DisplayOptions;
pub use diff2::Diff2;
pub use diff2::Diff2Parameters;
pub use edit::Edit;
pub use filelog::FileLog;
pub use print::Print;
pub use sync::Sync;
pub use r#where::Where;

use std::{ffi::OsStr, process::Command};

use crate::global::GlobalOpts;

/// Long output mode for changelist descriptions shared by commands such as
/// `p4 changes` and `p4 filelog`.
///
/// By default, only the first 30 (or 31) characters of the description are
/// shown.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LongOutput {
    /// Show the full text of each changelist description (`-l`).
    Default,
    /// Show the full text truncated at 250 characters (`-L`).
    Truncated,
}

impl LongOutput {
    /// Returns the command-line flag for this long output mode.
    pub fn as_str(&self) -> &'static str {
        match self {
            LongOutput::Default => "-l",
            LongOutput::Truncated => "-L",
        }
    }
}

/// Output format of the diff routine passed via `-doptions`.
///
/// This is one of two orthogonal dimensions of [`DiffOptions`] (the other
/// being [`WhitespaceHandling`]). Exactly one format may be selected; the
/// [`Default`](DiffFormat::Default) variant corresponds to no format flag.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum DiffFormat {
    /// Default diff output format (no format flag).
    #[default]
    Default,
    /// Context output format (`-dc[num]`), showing `num` lines of context
    /// around the changes.
    Context(Option<u32>),
    /// RCS output format (`-dn`), showing additions and deletions with
    /// associated line ranges.
    Rcs,
    /// Summary output format (`-ds`), showing only the number of chunks and
    /// lines added, deleted, or changed.
    Summary,
    /// Unified output format (`-du[num]`), showing added and deleted lines
    /// with `num` lines of context, in a form compatible with `patch(1)`.
    Unified(Option<u32>),
}

/// Whitespace handling of the diff routine passed via `-doptions`.
///
/// This is one of two orthogonal dimensions of [`DiffOptions`] (the other
/// being [`DiffFormat`]). The
/// [`IgnoreChangesWithinWhitespace`](WhitespaceHandling::IgnoreChangesWithinWhitespace)
/// and [`IgnoreAllWhitespace`](WhitespaceHandling::IgnoreAllWhitespace)
/// variants each imply
/// [`IgnoreLineEndings`](WhitespaceHandling::IgnoreLineEndings).
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum WhitespaceHandling {
    /// No whitespace handling.
    #[default]
    None,
    /// Ignore line-ending (CR/LF) convention when finding diffs (`-dl`).
    IgnoreLineEndings,
    /// Ignore changes made within whitespace (`-db`); implies `-dl`.
    IgnoreChangesWithinWhitespace,
    /// Ignore whitespace altogether (`-dw`); implies `-dl`.
    IgnoreAllWhitespace,
}

/// Diff routine options passed via `-doptions`, shared by commands such as
/// `p4 diff`, `p4 diff2`, `p4 describe`, and `p4 annotate`.
///
/// The structured ([`Typed`](DiffOptions::Typed)) options split into two
/// orthogonal dimensions — [`DiffFormat`] (output format) and
/// [`WhitespaceHandling`] — each of which is internally mutually exclusive.
/// All options are assembled with a [`DiffOptionsBuilder`], whose mode type
/// parameter tracks at compile time whether structured or raw options are
/// being built; both builder states convert into this type via [`From`], and
/// command methods accept `impl Into<DiffOptions>` so a builder can be
/// passed directly.
///
/// # Examples
///
/// ```
/// use perforce_cli::cmd::{DiffOptions, DiffOptionsBuilder};
///
/// // `-dub`: unified format, ignore changes within whitespace
/// let opts = DiffOptions::from(
///     DiffOptionsBuilder::unified(None).ignore_changes_within_whitespace(),
/// );
///
/// // `-dc3`: context format with 3 lines of context
/// let opts = DiffOptions::from(DiffOptionsBuilder::context(Some(3)));
///
/// // `-d-C 25`: pass `-C 25` straight to an external diff program
/// let opts = DiffOptions::from(DiffOptionsBuilder::raw("-C 25"));
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DiffOptions {
    /// Structured diff options (the documented subset of the standard UNIX
    /// diff flags).
    Typed {
        format: DiffFormat,
        whitespace: WhitespaceHandling,
    },
    /// Raw option string passed directly to the underlying diff routine
    /// (useful with an external diff program configured via `P4DIFF`).
    Raw(String),
}

impl Default for DiffOptions {
    fn default() -> Self {
        DiffOptions::Typed {
            format: DiffFormat::Default,
            whitespace: WhitespaceHandling::None,
        }
    }
}

impl DiffOptions {
    /// Returns the selected output format, or `None` if this is a
    /// [`Raw`](DiffOptions::Raw) value.
    pub fn format(&self) -> Option<DiffFormat> {
        match self {
            DiffOptions::Typed { format, .. } => Some(*format),
            DiffOptions::Raw(_) => None,
        }
    }

    /// Returns the selected whitespace handling, or `None` if this is a
    /// [`Raw`](DiffOptions::Raw) value.
    pub fn whitespace_handling(&self) -> Option<WhitespaceHandling> {
        match self {
            DiffOptions::Typed { whitespace, .. } => Some(*whitespace),
            DiffOptions::Raw(_) => None,
        }
    }

    /// Returns the raw option string, or `None` if this is a
    /// [`Typed`](DiffOptions::Typed) value.
    pub fn raw_str(&self) -> Option<&str> {
        match self {
            DiffOptions::Typed { .. } => None,
            DiffOptions::Raw(s) => Some(s),
        }
    }

    /// Injects the `-doptions` argument into `command`.
    ///
    /// If this is a [`Typed`](DiffOptions::Typed) value with both the format
    /// and whitespace handling at their defaults, no `-d` argument is emitted.
    pub fn inject_arg(&self, command: &mut Command) {
        use DiffFormat::*;
        use WhitespaceHandling::*;

        let (format, whitespace) = match self {
            DiffOptions::Typed { format, whitespace } => (*format, *whitespace),
            DiffOptions::Raw(s) => {
                command.arg(format!("-d{s}"));
                return;
            }
        };

        if matches!(format, Default) && matches!(whitespace, None) {
            return;
        }

        let mut s = String::from("-d");

        match format {
            Default => {}
            Context(num) => {
                s.push('c');
                if let Some(n) = num {
                    s.push_str(&n.to_string());
                }
            }
            Rcs => s.push('n'),
            Summary => s.push('s'),
            Unified(num) => {
                s.push('u');
                if let Some(n) = num {
                    s.push_str(&n.to_string());
                }
            }
        }

        match whitespace {
            None => {}
            IgnoreLineEndings => s.push('l'),
            IgnoreChangesWithinWhitespace => s.push('b'),
            IgnoreAllWhitespace => s.push('w'),
        }

        command.arg(s);
    }
}

impl From<String> for DiffOptions {
    fn from(s: String) -> Self {
        DiffOptions::Raw(s)
    }
}

/// The structured ([`Typed`](DiffOptions::Typed)) state of a
/// [`DiffOptionsBuilder`], holding the selected [`DiffFormat`] and
/// [`WhitespaceHandling`].
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct DiffOptionsTypedMode {
    format: DiffFormat,
    whitespace: WhitespaceHandling,
}

/// The raw ([`Raw`](DiffOptions::Raw)) state of a [`DiffOptionsBuilder`],
/// holding the option string passed verbatim to the underlying diff routine.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DiffOptionsRawMode {
    raw: String,
}

/// Builder for [`DiffOptions`], shared by commands such as `p4 diff`,
/// `p4 diff2`, `p4 describe`, and `p4 annotate`.
///
/// The mode type parameter tracks at compile time which kind of options are
/// being assembled. The format constructors ([`DiffOptionsBuilder::context`],
/// [`DiffOptionsBuilder::rcs`], [`DiffOptionsBuilder::summary`],
/// [`DiffOptionsBuilder::unified`], and [`DiffOptionsBuilder::new`]) produce
/// the [`DiffOptionsTypedMode`] state, where the whitespace builders
/// ([`DiffOptionsBuilder::ignore_line_endings`],
/// [`DiffOptionsBuilder::ignore_changes_within_whitespace`],
/// [`DiffOptionsBuilder::ignore_all_whitespace`]) become available;
/// [`DiffOptionsBuilder::raw`] produces the [`DiffOptionsRawMode`] state,
/// which offers no further options — the two states never mix.
///
/// Both states convert into [`DiffOptions`] via [`From`], so command methods
/// that accept `impl Into<DiffOptions>` take a builder directly.
///
/// # Examples
///
/// ```
/// use perforce_cli::cmd::DiffOptionsBuilder;
///
/// // `-dub`: unified format, ignore changes within whitespace
/// let builder =
///     DiffOptionsBuilder::unified(None).ignore_changes_within_whitespace();
///
/// // `-dc3`: context format with 3 lines of context
/// let builder = DiffOptionsBuilder::context(Some(3));
///
/// // `-d-C 25`: pass `-C 25` straight to an external diff program
/// let builder = DiffOptionsBuilder::raw("-C 25");
/// ```
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct DiffOptionsBuilder<M> {
    mode: M,
}

impl DiffOptionsBuilder<DiffOptionsTypedMode> {
    /// Creates a new builder in the [`DiffOptionsTypedMode`] state with the
    /// default format and no whitespace handling; the resulting
    /// [`DiffOptions`] emits no `-d` flag.
    pub fn new() -> Self {
        Self {
            mode: DiffOptionsTypedMode::default(),
        }
    }

    /// `-dc[num]`: context output format with `num` lines of context.
    pub fn context(num: Option<u32>) -> Self {
        Self {
            mode: DiffOptionsTypedMode {
                format: DiffFormat::Context(num),
                whitespace: WhitespaceHandling::None,
            },
        }
    }

    /// `-dn`: RCS output format.
    pub fn rcs() -> Self {
        Self {
            mode: DiffOptionsTypedMode {
                format: DiffFormat::Rcs,
                whitespace: WhitespaceHandling::None,
            },
        }
    }

    /// `-ds`: summary output format.
    pub fn summary() -> Self {
        Self {
            mode: DiffOptionsTypedMode {
                format: DiffFormat::Summary,
                whitespace: WhitespaceHandling::None,
            },
        }
    }

    /// `-du[num]`: unified output format with `num` lines of context.
    pub fn unified(num: Option<u32>) -> Self {
        Self {
            mode: DiffOptionsTypedMode {
                format: DiffFormat::Unified(num),
                whitespace: WhitespaceHandling::None,
            },
        }
    }

    /// `-dl`: ignore line-ending (CR/LF) convention when finding diffs.
    pub fn ignore_line_endings(mut self) -> Self {
        self.mode.whitespace = WhitespaceHandling::IgnoreLineEndings;
        self
    }

    /// `-db`: ignore changes made within whitespace (implies `-dl`).
    pub fn ignore_changes_within_whitespace(mut self) -> Self {
        self.mode.whitespace = WhitespaceHandling::IgnoreChangesWithinWhitespace;
        self
    }

    /// `-dw`: ignore whitespace altogether (implies `-dl`).
    pub fn ignore_all_whitespace(mut self) -> Self {
        self.mode.whitespace = WhitespaceHandling::IgnoreAllWhitespace;
        self
    }
}

impl DiffOptionsBuilder<DiffOptionsRawMode> {
    /// Passes an arbitrary option string directly to the underlying diff
    /// routine (for use with an external diff program).
    ///
    /// The string is appended to `-d` verbatim. For example,
    /// `DiffOptionsBuilder::raw("-C 25")` produces `-d-C 25`.
    pub fn raw(s: impl Into<String>) -> Self {
        Self {
            mode: DiffOptionsRawMode { raw: s.into() },
        }
    }
}

impl From<DiffOptionsBuilder<DiffOptionsTypedMode>> for DiffOptions {
    fn from(builder: DiffOptionsBuilder<DiffOptionsTypedMode>) -> Self {
        DiffOptions::Typed {
            format: builder.mode.format,
            whitespace: builder.mode.whitespace,
        }
    }
}

impl From<DiffOptionsBuilder<DiffOptionsRawMode>> for DiffOptions {
    fn from(builder: DiffOptionsBuilder<DiffOptionsRawMode>) -> Self {
        DiffOptions::Raw(builder.mode.raw)
    }
}

/// The default "no option selected" variant, shared by every
/// [`ExclusiveOption`] group.
///
/// It is a zero-sized type whose [`ExclusiveOption::inject_args`] is a no-op,
/// so any mutually exclusive option group can use it as its default type
/// parameter instead of defining its own empty variant.
#[derive(Debug, Clone, Copy, Default)]
pub struct Unselected;

impl ExclusiveOption for Unselected {
    fn inject_args(&self, _: &mut Command) {}
}

/// Marker trait for a mutually exclusive option group.
///
/// Some Perforce commands accept a set of options where only one may be
/// selected at a time (for example `p4 admin checkpoint [-z | -Z]` or
/// `p4 admin updatespecdepot [-a | -s type]`). Each variant of such a group
/// implements this trait to inject its own CLI arguments; the selected
/// variant is encoded in a type parameter so that the alternatives are
/// unavailable at compile time.
///
/// Variants may carry their own data (for example the `type` argument of
/// `-s type`) and inject any number of arguments, keeping the trait open to
/// option groups more complex than a single flag.
pub trait ExclusiveOption {
    #[allow(unused_variables)]
    /// Inject the CLI arguments corresponding to this selection into
    /// `command`.
    fn inject_args(&self, command: &mut Command) {}
}

pub trait SubCommand {
    fn name(&self) -> &str;

    fn inject_local_args(&self, command: &mut Command);

    fn global_opts(&self) -> Option<&GlobalOpts> {
        None
    }

    fn inject_args(&self, command: &mut Command) {
        // inject global opts
        if let Some(global_opts) = self.global_opts() {
            global_opts.setup_args(command);
        };
        // inject local opts
        self.inject_local_args(command.arg(self.name()));
    }

    fn setup_command<S: AsRef<OsStr>>(&self, bin: S) -> Command {
        let mut cmd = Command::new(bin);

        self.inject_args(&mut cmd);
        cmd
    }
}

/// Test-only helper: collects the arguments assembled on a [`Command`] as
/// strings for easy comparison.
#[cfg(test)]
pub(crate) fn args_of(command: &Command) -> Vec<String> {
    command
        .get_args()
        .map(|arg| arg.to_string_lossy().into_owned())
        .collect()
}

#[cfg(test)]
mod diff_options_tests {
    use super::*;

    fn injected(opts: impl Into<DiffOptions>) -> Vec<String> {
        let mut cmd = Command::new("p4");
        opts.into().inject_arg(&mut cmd);
        args_of(&cmd)
    }

    #[test]
    fn default_emits_nothing() {
        assert!(injected(DiffOptionsBuilder::new()).is_empty());
        assert!(injected(DiffOptions::default()).is_empty());
    }

    #[test]
    fn unified_format() {
        assert_eq!(injected(DiffOptionsBuilder::unified(None)), ["-du"]);
    }

    #[test]
    fn unified_format_with_context() {
        assert_eq!(injected(DiffOptionsBuilder::unified(Some(3))), ["-du3"]);
    }

    #[test]
    fn context_format() {
        assert_eq!(injected(DiffOptionsBuilder::context(None)), ["-dc"]);
        assert_eq!(injected(DiffOptionsBuilder::context(Some(5))), ["-dc5"]);
    }

    #[test]
    fn rcs_format() {
        assert_eq!(injected(DiffOptionsBuilder::rcs()), ["-dn"]);
    }

    #[test]
    fn summary_format() {
        assert_eq!(injected(DiffOptionsBuilder::summary()), ["-ds"]);
    }

    #[test]
    fn whitespace_only() {
        assert_eq!(
            injected(DiffOptionsBuilder::new().ignore_line_endings()),
            ["-dl"]
        );
    }

    #[test]
    fn unified_with_ignore_changes_within_whitespace() {
        assert_eq!(
            injected(DiffOptionsBuilder::unified(None).ignore_changes_within_whitespace()),
            ["-dub"]
        );
    }

    #[test]
    fn context_with_ignore_all_whitespace() {
        assert_eq!(
            injected(DiffOptionsBuilder::context(Some(2)).ignore_all_whitespace()),
            ["-dc2w"]
        );
    }

    #[test]
    fn raw_passthrough() {
        assert_eq!(injected(DiffOptionsBuilder::raw("-C 25")), ["-d-C 25"]);
        assert_eq!(injected(DiffOptionsBuilder::raw("--brief")), ["-d--brief"]);
    }

    #[test]
    fn getters_reflect_variant() {
        let typed = DiffOptions::from(DiffOptionsBuilder::unified(Some(3)).ignore_line_endings());
        assert_eq!(typed.format(), Some(DiffFormat::Unified(Some(3))));
        assert_eq!(
            typed.whitespace_handling(),
            Some(WhitespaceHandling::IgnoreLineEndings)
        );
        assert_eq!(typed.raw_str(), None);

        let raw = DiffOptions::from(DiffOptionsBuilder::raw("abc"));
        assert_eq!(raw.format(), None);
        assert_eq!(raw.whitespace_handling(), None);
        assert_eq!(raw.raw_str(), Some("abc"));
    }
}