perforce-cli 0.1.0-alpha.3

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
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
use std::{
    ffi::OsStr,
    path::PathBuf,
    process::{Child, Command, Stdio},
};

use super::{DiffOptions, ExclusiveOption, SubCommand, Unselected};

use crate::global::GlobalOpts;
use crate::spawn::ParameterizedSpawn;

/// Display options passed via the `-soptions` flag, producing a shorthand
/// list of files that match the filter instead of diffs.
///
/// Exactly one filter may be selected; the enum makes the alternatives
/// unavailable at compile time.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DisplayOptions {
    /// `-sa`
    ///
    /// Show only the names of opened files that are different from the
    /// revision in the depot, or are missing.
    OpenedChangedOrMissing,

    /// `-sb`
    ///
    /// Show only the names of files opened for integrate that have been
    /// resolved, but that have been modified after being resolved.
    ResolvedThenModified,

    /// `-sd`
    ///
    /// Show only the names of unopened files that are missing from the
    /// client workspace, but present in the depot.
    UnopenedMissing,

    /// `-se`
    ///
    /// Show only the names of unopened files in the client workspace that
    /// are different than the revision in the depot.
    UnopenedChanged,

    /// `-sl file ...`
    ///
    /// Every unopened `file` is compared with the depot, and listed with a
    /// status of `same`, `diff`, or `missing`.
    ///
    #[cfg_attr(
        feature = "lt2014_2",
        doc = "If you use the `-f` flag together with the `-sl` flag, files that are open for edit are also compared and their status is listed."
    )]
    #[cfg_attr(
        not(feature = "lt2014_2"),
        doc = "If you use the `-f` option together with the `-sl` option, files that are open for edit are also compared and their status is listed."
    )]
    ///
    /// The compared files are passed as the file arguments of the spawned
    /// command.
    ListWithStatus,

    /// `-sr`
    ///
    /// Show only the names of opened files in the client workspace that are
    /// identical to the revision in the depot.
    OpenedIdentical,
}

impl DisplayOptions {
    /// Returns the command-line flag for this display option.
    pub fn as_str(&self) -> &'static str {
        match self {
            DisplayOptions::OpenedChangedOrMissing => "-sa",
            DisplayOptions::ResolvedThenModified => "-sb",
            DisplayOptions::UnopenedMissing => "-sd",
            DisplayOptions::UnopenedChanged => "-se",
            DisplayOptions::ListWithStatus => "-sl",
            DisplayOptions::OpenedIdentical => "-sr",
        }
    }
}

/// The `-soptions` state of [`WorkspaceMode`]: a display filter is selected,
/// making `-m max` unavailable.
///
/// Entered with [`Diff::display_options`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct WorkspaceDisplayMode {
    display_opts: DisplayOptions,
}

impl ExclusiveOption for WorkspaceDisplayMode {
    fn inject_args(&self, command: &mut Command) {
        command.arg(self.display_opts.as_str());
    }
}

/// The `-m max` state of [`WorkspaceMode`]: a file limit is selected, making
/// `-soptions` unavailable.
///
/// Entered with [`Diff::limit`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct WorkspaceRegularMode {
    limit: u64,
}

impl ExclusiveOption for WorkspaceRegularMode {
    fn inject_args(&self, command: &mut Command) {
        command.arg("-m").arg(self.limit.to_string());
    }
}

/// Workspace content mode of `p4 diff`, comparing files in the client
/// workspace to revisions in the depot.
///
/// Entered with [`Diff::force`], [`Diff::differing_only`], or
/// [`Diff::diff_nontext`].
///
/// The `M` type parameter isolates `-m max` and `-soptions` at compile time:
/// [`Diff::limit`] transitions to the [`WorkspaceRegularMode`] state, while
/// [`Diff::display_options`] transitions to the [`WorkspaceDisplayMode`]
/// state.
#[derive(Debug, Clone, Default)]
pub struct WorkspaceMode<M = Unselected> {
    force: bool,

    differing_only: bool,

    diff_nontext: bool,

    mode: M,
}

impl<M: ExclusiveOption> ExclusiveOption for WorkspaceMode<M> {
    fn inject_args(&self, command: &mut Command) {
        if self.force {
            command.arg("-f");
        }

        if self.diff_nontext {
            command.arg("-t");
        }

        if self.differing_only {
            command.arg("-Od");
        }

        self.mode.inject_args(command);
    }
}

/// Stream spec mode of `p4 diff` (`-As`): diff a privately edited stream
/// spec against another version of the same stream spec, or diff two
/// arbitrary stream specs against each other.
///
/// Entered with [`Diff::stream_spec_mode`]; the stream spec to compare
/// against is passed as the argument of the spawned command.
#[cfg(not(feature = "lt2019_1"))]
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct StreamSpecMode;

#[cfg(not(feature = "lt2019_1"))]
impl ExclusiveOption for StreamSpecMode {
    fn inject_args(&self, command: &mut Command) {
        command.arg("-As");
    }
}

#[cfg_attr(
    feature = "lt2014_2",
    doc = "`p4 [g-opts] diff [-dflags -f -m max -Od -sa -sb -sd -se -sr -sl -t] [file[rev#]...]`"
)]
#[cfg_attr(
    all(feature = "lt2015_1", not(feature = "lt2014_2")),
    doc = "`p4 [g-opts] diff [-doptions -f -m max -Od -sa -sb -sd -se -sr -sl -t] [file[rev#]...]`"
)]
#[cfg_attr(
    all(feature = "lt2016_1", not(feature = "lt2015_1")),
    doc = "`p4 [g-opts] diff [-doptions] [-f -t -Od] [-m max] [-soptions] [file[rev] …]`"
)]
#[cfg_attr(
    all(feature = "lt2019_1", not(feature = "lt2016_1")),
    doc = "`p4 [g-opts] diff [-doptions] [-f -t -Od] [-m max] [-soptions] [file[rev] ...]`"
)]
#[cfg_attr(
    not(feature = "lt2019_1"),
    doc = "`p4 [g-opts] diff [-doptions] [-f -t -Od] [-m max] [-soptions] [file[rev] ...]`",
    doc = "",
    doc = "`p4 [g-opts] diff [-doptions] -As [streamname[@change]]`"
)]
///
/// Diff utility for comparing workspace content to depot content. (For
/// comparing two depot paths, see `p4 diff2`.)
///
#[cfg_attr(not(feature = "lt2019_1"), doc = "Also for stream spec comparison.")]
///
#[cfg_attr(
    feature = "lt2019_1",
    doc = "The `M` type parameter tracks the command mode at compile time. The default [`Unselected`] state offers the workspace content options; [`Self::force`], [`Self::differing_only`], and [`Self::diff_nontext`] transition to the [`WorkspaceMode`] state."
)]
#[cfg_attr(
    not(feature = "lt2019_1"),
    doc = "The `M` type parameter tracks the command mode at compile time. The default [`Unselected`] state offers neither the workspace content options nor `-As`; [`Self::force`], [`Self::differing_only`], and [`Self::diff_nontext`] transition to the [`WorkspaceMode`] state, while [`Self::stream_spec_mode`] transitions to the [`StreamSpecMode`] state."
)]
#[derive(Debug, Clone, Default)]
pub struct Diff<M = Unselected> {
    bin: PathBuf,

    global_opts: GlobalOpts,

    diff_opts: Option<DiffOptions>,

    mode: M,
}

impl Diff<Unselected> {
    /// Creates a new `p4 diff` command.
    ///
    /// `bin` is the path to the Perforce command-line executable.
    pub fn new(bin: impl Into<PathBuf>, global_opts: GlobalOpts) -> Self {
        Self {
            bin: bin.into(),
            global_opts,
            diff_opts: None,
            mode: Unselected,
        }
    }

    /// # Description
    ///
    /// -f
    ///
    /// Force the diff (if no revision is specified, against the head
    /// revision), even when the client file is not open for `edit`.
    ///
    /// Transitions this command to the [`WorkspaceMode`] state with `-f` set
    /// according to `v`.
    pub fn force(self, v: bool) -> Diff<WorkspaceMode<Unselected>> {
        Diff {
            bin: self.bin,
            global_opts: self.global_opts,
            diff_opts: self.diff_opts,
            mode: WorkspaceMode {
                force: v,
                differing_only: false,
                diff_nontext: false,
                mode: Unselected,
            },
        }
    }

    /// # Description
    ///
    /// -Od
    ///
    /// Limit output to only those files that differ.
    ///
    /// Transitions this command to the [`WorkspaceMode`] state with `-Od`
    /// set according to `v`.
    pub fn differing_only(self, v: bool) -> Diff<WorkspaceMode<Unselected>> {
        Diff {
            bin: self.bin,
            global_opts: self.global_opts,
            diff_opts: self.diff_opts,
            mode: WorkspaceMode {
                force: false,
                differing_only: v,
                diff_nontext: false,
                mode: Unselected,
            },
        }
    }

    /// # Description
    ///
    /// -t
    ///
    /// Diff the revisions even if the files are not of type `text`.
    ///
    /// Transitions this command to the [`WorkspaceMode`] state with `-t` set
    /// according to `v`.
    pub fn diff_nontext(self, v: bool) -> Diff<WorkspaceMode<Unselected>> {
        Diff {
            bin: self.bin,
            global_opts: self.global_opts,
            diff_opts: self.diff_opts,
            mode: WorkspaceMode {
                force: false,
                differing_only: false,
                diff_nontext: v,
                mode: Unselected,
            },
        }
    }

    /// # Description
    ///
    /// -As
    ///
    /// Allows two arbitrary stream specs to be diffed against each other.
    /// Can be used with a streamname, or with a streamname at a specific
    /// changelist number.
    ///
    #[cfg_attr(
        feature = "lt2024_2",
        doc = "Limitation: Although this option requires the user have at least the `list` access to the stream path, it ignores any other entry in the protections table, including any minus sign (`-`) that would otherwise block the operation."
    )]
    #[cfg_attr(
        not(feature = "lt2024_2"),
        doc = "Although this option requires the user have at least the `list` access to the stream path, it ignores any other entry in the protections table, including any minus sign (`-`) that would otherwise block the operation."
    )]
    ///
    /// Transitions this command to the [`StreamSpecMode`] state. The stream
    /// spec to compare against is passed as the argument of the spawned
    /// command ([`ParameterizedSpawn::spawn_with`]); without one, the opened
    /// stream spec is diffed against its have version.
    #[cfg(not(feature = "lt2019_1"))]
    pub fn stream_spec_mode(self) -> Diff<StreamSpecMode> {
        Diff {
            bin: self.bin,
            global_opts: self.global_opts,
            diff_opts: self.diff_opts,
            mode: StreamSpecMode,
        }
    }
}

impl<M> Diff<M> {
    /// # Description
    ///
    /// -doptions
    ///
    #[cfg_attr(
        feature = "lt2014_2",
        doc = "Pass flags to the underlying diff routine (see the Usage Notes below for details)"
    )]
    #[cfg_attr(
        all(feature = "lt2015_1", not(feature = "lt2014_2")),
        doc = "Pass options to the underlying diff routine (see the Usage Notes below for details)."
    )]
    #[cfg_attr(
        all(feature = "lt2024_1", not(feature = "lt2015_1")),
        doc = "Pass options to the underlying diff routine (see Usage Notes for details)."
    )]
    #[cfg_attr(
        not(feature = "lt2024_1"),
        doc = "Pass options to the underlying diff routine (see Usage notes for details)."
    )]
    pub fn get_diff_options(&self) -> Option<&DiffOptions> {
        self.diff_opts.as_ref()
    }

    /// # Description
    ///
    /// -doptions
    ///
    #[cfg_attr(
        feature = "lt2014_2",
        doc = "Pass flags to the underlying diff routine (see the Usage Notes below for details)"
    )]
    #[cfg_attr(
        all(feature = "lt2015_1", not(feature = "lt2014_2")),
        doc = "Pass options to the underlying diff routine (see the Usage Notes below for details)."
    )]
    #[cfg_attr(
        all(feature = "lt2024_1", not(feature = "lt2015_1")),
        doc = "Pass options to the underlying diff routine (see Usage Notes for details)."
    )]
    #[cfg_attr(
        not(feature = "lt2024_1"),
        doc = "Pass options to the underlying diff routine (see Usage notes for details)."
    )]
    pub fn set_diff_options(&mut self, v: impl Into<DiffOptions>) -> &mut Self {
        self.diff_opts = Some(v.into());
        self
    }

    /// # Description
    ///
    /// -doptions
    ///
    #[cfg_attr(
        feature = "lt2014_2",
        doc = "Pass flags to the underlying diff routine (see the Usage Notes below for details)"
    )]
    #[cfg_attr(
        all(feature = "lt2015_1", not(feature = "lt2014_2")),
        doc = "Pass options to the underlying diff routine (see the Usage Notes below for details)."
    )]
    #[cfg_attr(
        all(feature = "lt2024_1", not(feature = "lt2015_1")),
        doc = "Pass options to the underlying diff routine (see Usage Notes for details)."
    )]
    #[cfg_attr(
        not(feature = "lt2024_1"),
        doc = "Pass options to the underlying diff routine (see Usage notes for details)."
    )]
    pub fn diff_options(mut self, v: impl Into<DiffOptions>) -> Self {
        self.diff_opts = Some(v.into());
        self
    }
}

impl<M: ExclusiveOption> Diff<M> {
    /// # Description
    ///
    /// g-opts
    ///
    #[cfg_attr(
        feature = "lt2014_2",
        doc = "See the [Global Options](GlobalOpts) section."
    )]
    #[cfg_attr(
        all(feature = "lt2015_1", not(feature = "lt2014_2")),
        doc = "See the [“Global Options”](GlobalOpts) section."
    )]
    #[cfg_attr(
        all(feature = "lt2017_1", not(feature = "lt2015_1")),
        doc = "See [“Global Options”](GlobalOpts)."
    )]
    #[cfg_attr(
        all(feature = "lt2018_2", not(feature = "lt2017_1")),
        doc = "See [Global Options](GlobalOpts)."
    )]
    #[cfg_attr(not(feature = "lt2018_2"), doc = "See [Global options](GlobalOpts).")]
    pub fn get_global_opts(&self) -> &GlobalOpts {
        &self.global_opts
    }

    /// # Description
    ///
    /// g-opts
    ///
    #[cfg_attr(
        feature = "lt2014_2",
        doc = "See the [Global Options](GlobalOpts) section."
    )]
    #[cfg_attr(
        all(feature = "lt2015_1", not(feature = "lt2014_2")),
        doc = "See the [“Global Options”](GlobalOpts) section."
    )]
    #[cfg_attr(
        all(feature = "lt2017_1", not(feature = "lt2015_1")),
        doc = "See [“Global Options”](GlobalOpts)."
    )]
    #[cfg_attr(
        all(feature = "lt2018_2", not(feature = "lt2017_1")),
        doc = "See [Global Options](GlobalOpts)."
    )]
    #[cfg_attr(not(feature = "lt2018_2"), doc = "See [Global options](GlobalOpts).")]
    pub fn set_global_opts(&mut self, v: GlobalOpts) -> &mut Self {
        self.global_opts = v;
        self
    }

    /// # Description
    ///
    /// g-opts
    ///
    #[cfg_attr(
        feature = "lt2014_2",
        doc = "See the [Global Options](GlobalOpts) section."
    )]
    #[cfg_attr(
        all(feature = "lt2015_1", not(feature = "lt2014_2")),
        doc = "See the [“Global Options”](GlobalOpts) section."
    )]
    #[cfg_attr(
        all(feature = "lt2017_1", not(feature = "lt2015_1")),
        doc = "See [“Global Options”](GlobalOpts)."
    )]
    #[cfg_attr(
        all(feature = "lt2018_2", not(feature = "lt2017_1")),
        doc = "See [Global Options](GlobalOpts)."
    )]
    #[cfg_attr(not(feature = "lt2018_2"), doc = "See [Global options](GlobalOpts).")]
    pub fn global_opts(mut self, v: GlobalOpts) -> Self {
        self.global_opts = v;
        self
    }
}

impl<M> Diff<WorkspaceMode<M>> {
    /// # Description
    ///
    /// -f
    ///
    /// Force the diff (if no revision is specified, against the head
    /// revision), even when the client file is not open for `edit`.
    pub fn get_force(&self) -> bool {
        self.mode.force
    }

    /// # Description
    ///
    /// -f
    ///
    /// Force the diff (if no revision is specified, against the head
    /// revision), even when the client file is not open for `edit`.
    pub fn set_force(&mut self, v: bool) -> &mut Self {
        self.mode.force = v;
        self
    }

    /// # Description
    ///
    /// -f
    ///
    /// Force the diff (if no revision is specified, against the head
    /// revision), even when the client file is not open for `edit`.
    pub fn force(mut self, v: bool) -> Self {
        self.mode.force = v;
        self
    }

    /// # Description
    ///
    /// -Od
    ///
    /// Limit output to only those files that differ.
    pub fn get_differing_only(&self) -> bool {
        self.mode.differing_only
    }

    /// # Description
    ///
    /// -Od
    ///
    /// Limit output to only those files that differ.
    pub fn set_differing_only(&mut self, v: bool) -> &mut Self {
        self.mode.differing_only = v;
        self
    }

    /// # Description
    ///
    /// -Od
    ///
    /// Limit output to only those files that differ.
    pub fn differing_only(mut self, v: bool) -> Self {
        self.mode.differing_only = v;
        self
    }

    /// # Description
    ///
    /// -t
    ///
    /// Diff the revisions even if the files are not of type `text`.
    pub fn get_diff_nontext(&self) -> bool {
        self.mode.diff_nontext
    }

    /// # Description
    ///
    /// -t
    ///
    /// Diff the revisions even if the files are not of type `text`.
    pub fn set_diff_nontext(&mut self, v: bool) -> &mut Self {
        self.mode.diff_nontext = v;
        self
    }

    /// # Description
    ///
    /// -t
    ///
    /// Diff the revisions even if the files are not of type `text`.
    pub fn diff_nontext(mut self, v: bool) -> Self {
        self.mode.diff_nontext = v;
        self
    }
}

impl Diff<WorkspaceMode<Unselected>> {
    /// # Description
    ///
    #[cfg_attr(feature = "lt2015_1", doc = "-sa, -sb, -sd, -se, -sr, -sl file ...")]
    #[cfg_attr(not(feature = "lt2015_1"), doc = "-soptions")]
    ///
    #[cfg_attr(
        feature = "lt2015_1",
        doc = "Pass one of the display options; see the individual option descriptions for details."
    )]
    #[cfg_attr(
        all(feature = "lt2024_1", not(feature = "lt2015_1")),
        doc = "Pass display options to the underlying diff routine (see Usage Notes for details)."
    )]
    #[cfg_attr(
        not(feature = "lt2024_1"),
        doc = "Pass display options to the underlying diff routine (see Usage notes for details)."
    )]
    ///
    /// Transitions this command to the [`WorkspaceDisplayMode`] state with
    /// the display `options` set; `-m max` is unavailable in this state.
    pub fn display_options(self, v: DisplayOptions) -> Diff<WorkspaceMode<WorkspaceDisplayMode>> {
        Diff {
            bin: self.bin,
            global_opts: self.global_opts,
            diff_opts: self.diff_opts,
            mode: WorkspaceMode {
                force: self.mode.force,
                differing_only: self.mode.differing_only,
                diff_nontext: self.mode.diff_nontext,
                mode: WorkspaceDisplayMode { display_opts: v },
            },
        }
    }

    /// # Description
    ///
    /// -m max
    ///
    #[cfg_attr(
        feature = "lt2014_2",
        doc = "Limit output to diffs (or status) of only the first `max` files, unless the `-s` flag is used, in which case the `-m` flag is ignored."
    )]
    #[cfg_attr(
        not(feature = "lt2014_2"),
        doc = "Limit output to diffs (or status) of only the first `max` files, unless the `-s` option is used, in which case the `-m` option is ignored."
    )]
    ///
    /// Transitions this command to the [`WorkspaceRegularMode`] state with
    /// the limit set to `max`; `-soptions` is unavailable in this state.
    pub fn limit(self, max: u64) -> Diff<WorkspaceMode<WorkspaceRegularMode>> {
        Diff {
            bin: self.bin,
            global_opts: self.global_opts,
            diff_opts: self.diff_opts,
            mode: WorkspaceMode {
                force: self.mode.force,
                differing_only: self.mode.differing_only,
                diff_nontext: self.mode.diff_nontext,
                mode: WorkspaceRegularMode { limit: max },
            },
        }
    }
}

impl Diff<WorkspaceMode<WorkspaceDisplayMode>> {
    /// # Description
    ///
    #[cfg_attr(feature = "lt2015_1", doc = "-sa, -sb, -sd, -se, -sr, -sl file ...")]
    #[cfg_attr(not(feature = "lt2015_1"), doc = "-soptions")]
    ///
    #[cfg_attr(
        feature = "lt2015_1",
        doc = "Pass one of the display options; see the individual option descriptions for details."
    )]
    #[cfg_attr(
        all(feature = "lt2024_1", not(feature = "lt2015_1")),
        doc = "Pass display options to the underlying diff routine (see Usage Notes for details)."
    )]
    #[cfg_attr(
        not(feature = "lt2024_1"),
        doc = "Pass display options to the underlying diff routine (see Usage notes for details)."
    )]
    pub fn get_display_options(&self) -> &DisplayOptions {
        &self.mode.mode.display_opts
    }

    /// # Description
    ///
    #[cfg_attr(feature = "lt2015_1", doc = "-sa, -sb, -sd, -se, -sr, -sl file ...")]
    #[cfg_attr(not(feature = "lt2015_1"), doc = "-soptions")]
    ///
    #[cfg_attr(
        feature = "lt2015_1",
        doc = "Pass one of the display options; see the individual option descriptions for details."
    )]
    #[cfg_attr(
        all(feature = "lt2024_1", not(feature = "lt2015_1")),
        doc = "Pass display options to the underlying diff routine (see Usage Notes for details)."
    )]
    #[cfg_attr(
        not(feature = "lt2024_1"),
        doc = "Pass display options to the underlying diff routine (see Usage notes for details)."
    )]
    pub fn set_display_options(&mut self, v: DisplayOptions) -> &mut Self {
        self.mode.mode.display_opts = v;
        self
    }
}

impl Diff<WorkspaceMode<WorkspaceRegularMode>> {
    /// # Description
    ///
    /// -m max
    ///
    #[cfg_attr(
        feature = "lt2014_2",
        doc = "Limit output to diffs (or status) of only the first `max` files, unless the `-s` flag is used, in which case the `-m` flag is ignored."
    )]
    #[cfg_attr(
        not(feature = "lt2014_2"),
        doc = "Limit output to diffs (or status) of only the first `max` files, unless the `-s` option is used, in which case the `-m` option is ignored."
    )]
    pub fn get_limit(&self) -> u64 {
        self.mode.mode.limit
    }

    /// # Description
    ///
    /// -m max
    ///
    #[cfg_attr(
        feature = "lt2014_2",
        doc = "Limit output to diffs (or status) of only the first `max` files, unless the `-s` flag is used, in which case the `-m` flag is ignored."
    )]
    #[cfg_attr(
        not(feature = "lt2014_2"),
        doc = "Limit output to diffs (or status) of only the first `max` files, unless the `-s` option is used, in which case the `-m` option is ignored."
    )]
    pub fn set_limit(&mut self, v: u64) -> &mut Self {
        self.mode.mode.limit = v;
        self
    }
}

impl<M: ExclusiveOption> SubCommand for Diff<M> {
    fn name(&self) -> &str {
        "diff"
    }

    fn inject_local_args(&self, command: &mut Command) {
        if let Some(diff_opts) = &self.diff_opts {
            diff_opts.inject_arg(command);
        }

        self.mode.inject_args(command);
    }

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

impl<S, I> ParameterizedSpawn<(S,)> for Diff<Unselected>
where
    S: IntoIterator<Item = I>,
    I: AsRef<OsStr>,
{
    type Output = Child;
    type Error = std::io::Error;

    /// Spawns `p4 diff` for the given file arguments as a child process with
    /// piped standard output and error streams; use the returned [`Child`]
    /// handle to wait for it or interact with it.
    fn spawn_with(&mut self, (files,): (S,)) -> Result<Self::Output, Self::Error> {
        self.setup_command(&self.bin)
            .args(files)
            .stdout(Stdio::piped())
            .stderr(Stdio::piped())
            .spawn()
    }
}

impl<M: ExclusiveOption, S, I> ParameterizedSpawn<(S,)> for Diff<WorkspaceMode<M>>
where
    S: IntoIterator<Item = I>,
    I: AsRef<OsStr>,
{
    type Output = Child;
    type Error = std::io::Error;

    /// Spawns `p4 diff` for the given file arguments as a child process with
    /// piped standard output and error streams; use the returned [`Child`]
    /// handle to wait for it or interact with it.
    fn spawn_with(&mut self, (files,): (S,)) -> Result<Self::Output, Self::Error> {
        self.setup_command(&self.bin)
            .args(files)
            .stdout(Stdio::piped())
            .stderr(Stdio::piped())
            .spawn()
    }
}

#[cfg(not(feature = "lt2019_1"))]
impl<I> ParameterizedSpawn<(I,)> for Diff<StreamSpecMode>
where
    I: AsRef<OsStr>,
{
    type Output = Child;
    type Error = std::io::Error;

    /// Spawns `p4 diff -As` as a child process with piped standard output
    /// and error streams; use the returned [`Child`] handle to wait for it
    /// or interact with it.
    ///
    /// The stream spec is a streamname, optionally at a specific changelist
    /// number: `@head` selects the head version, `@change` the version at a
    /// specific change, and `@=change` the shelved version at a specific
    /// change.
    ///
    /// Use [`spawn()`](SpawnExt::spawn) (no arguments) to diff the opened
    /// stream spec against its have version.
    fn spawn_with(&mut self, (stream_spec,): (I,)) -> Result<Self::Output, Self::Error> {
        self.setup_command(&self.bin)
            .arg(stream_spec)
            .stdout(Stdio::piped())
            .stderr(Stdio::piped())
            .spawn()
    }
}

#[cfg(not(feature = "lt2019_1"))]
impl ParameterizedSpawn<()> for Diff<StreamSpecMode> {
    type Output = Child;
    type Error = std::io::Error;

    /// Spawns `p4 diff -As` without a stream spec as a child process with
    /// piped standard output and error streams; the opened stream spec is
    /// diffed against its have version. Use the returned [`Child`] handle
    /// to wait for it or interact with it.
    fn spawn_with(&mut self, _: ()) -> Result<Self::Output, Self::Error> {
        self.setup_command(&self.bin)
            .stdout(Stdio::piped())
            .stderr(Stdio::piped())
            .spawn()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::cmd::DiffOptionsBuilder;
    use crate::cmd::args_of;

    /// Dry-run checks of the assembled `p4 diff` command line; no process is
    /// spawned.
    #[test]
    fn without_options() {
        let diff = Diff::new("p4", GlobalOpts::new());

        assert_eq!(args_of(&diff.setup_command("p4")), ["diff"]);
    }

    #[test]
    fn workspace_mode_via_force() {
        let diff = Diff::new("p4", GlobalOpts::new()).force(true);

        assert!(diff.get_force());
        assert_eq!(args_of(&diff.setup_command("p4")), ["diff", "-f"]);
    }

    #[test]
    fn workspace_mode_via_differing_only() {
        let diff = Diff::new("p4", GlobalOpts::new()).differing_only(true);

        assert_eq!(args_of(&diff.setup_command("p4")), ["diff", "-Od"]);
    }

    #[test]
    fn workspace_mode_via_diff_nontext() {
        let diff = Diff::new("p4", GlobalOpts::new()).diff_nontext(true);

        assert_eq!(args_of(&diff.setup_command("p4")), ["diff", "-t"]);
    }

    #[test]
    fn workspace_mode_combines_options() {
        let mut diff = Diff::new("p4", GlobalOpts::new()).diff_nontext(true);
        diff.set_force(true).set_differing_only(true);

        assert_eq!(
            args_of(&diff.setup_command("p4")),
            ["diff", "-f", "-t", "-Od"]
        );
    }

    #[test]
    fn workspace_mode_preserves_diff_options() {
        let mut diff = Diff::new("p4", GlobalOpts::new())
            .diff_options(DiffOptionsBuilder::unified(None))
            .force(true);
        diff.set_diff_options(DiffOptionsBuilder::summary());

        assert_eq!(args_of(&diff.setup_command("p4")), ["diff", "-ds", "-f"]);
    }

    #[test]
    fn display_mode_injects_filter() {
        let diff = Diff::new("p4", GlobalOpts::new())
            .force(true)
            .display_options(DisplayOptions::UnopenedChanged);

        assert_eq!(diff.get_display_options(), &DisplayOptions::UnopenedChanged);
        assert_eq!(args_of(&diff.setup_command("p4")), ["diff", "-f", "-se"]);
    }

    #[test]
    fn display_mode_flag_mapping() {
        assert_eq!(DisplayOptions::OpenedChangedOrMissing.as_str(), "-sa");
        assert_eq!(DisplayOptions::ResolvedThenModified.as_str(), "-sb");
        assert_eq!(DisplayOptions::UnopenedMissing.as_str(), "-sd");
        assert_eq!(DisplayOptions::UnopenedChanged.as_str(), "-se");
        assert_eq!(DisplayOptions::ListWithStatus.as_str(), "-sl");
        assert_eq!(DisplayOptions::OpenedIdentical.as_str(), "-sr");
    }

    #[test]
    fn regular_mode_injects_limit() {
        let diff = Diff::new("p4", GlobalOpts::new()).force(false).limit(10);

        assert_eq!(diff.get_limit(), 10);
        assert_eq!(args_of(&diff.setup_command("p4")), ["diff", "-m", "10"]);
    }

    #[cfg(not(feature = "lt2019_1"))]
    #[test]
    fn stream_spec_mode_bare() {
        let diff = Diff::new("p4", GlobalOpts::new()).stream_spec_mode();

        assert_eq!(args_of(&diff.setup_command("p4")), ["diff", "-As"]);
    }

    #[cfg(not(feature = "lt2019_1"))]
    #[test]
    fn stream_spec_mode_with_spec() {
        let diff = Diff::new("p4", GlobalOpts::new()).stream_spec_mode();

        // Mirrors `spawn_with`, which appends the stream spec after the
        // assembled command.
        let mut command = diff.setup_command("p4");
        command.arg("myStream@head");

        assert_eq!(args_of(&command), ["diff", "-As", "myStream@head"]);
    }

    #[cfg(not(feature = "lt2019_1"))]
    #[test]
    fn stream_spec_mode_preserves_diff_options() {
        let diff = Diff::new("p4", GlobalOpts::new())
            .diff_options(DiffOptionsBuilder::unified(None))
            .stream_spec_mode();

        // Mirrors `spawn_with`, which appends the stream spec after the
        // assembled command.
        let mut command = diff.setup_command("p4");
        command.arg("myStream");

        assert_eq!(args_of(&command), ["diff", "-du", "-As", "myStream"]);
    }
}