perforce-cli 0.2.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
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
use std::path::{Path, PathBuf};
use std::process::{Child, Command, Stdio};

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

use crate::spawn::ParameterizedSpawn;

// ---------------------------------------------------------------------------
// Clone target: `-r remotespec` vs `-f filespec`
// ---------------------------------------------------------------------------

/// The `-r remotespec` target state: use the remote spec named `remotespec`,
/// installed on the remote server, as a template for the clone and stream
/// setup.
///
/// Entered with [`Clone::remote`]; mutually exclusive with
/// [`FileSpecMode`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RemoteSpecMode {
    source: String,
}

/// The `-f filespec` target state: use the *filespec* in the remote server as
/// the path to clone.
///
/// Entered with [`Clone::file`]; mutually exclusive with
/// [`RemoteSpecMode`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FileSpecMode {
    source: String,
}

impl ExclusiveOption for RemoteSpecMode {
    fn inject_args(&self, command: &mut Command) {
        command.arg("-r").arg(&self.source);
    }
}

impl ExclusiveOption for FileSpecMode {
    fn inject_args(&self, command: &mut Command) {
        command.arg("-f").arg(&self.source);
    }
}

/// Marker for a selected clone target: either [`RemoteSpecMode`] (`-r`) or
/// [`FileSpecMode`] (`-f`).
///
/// Exactly one of `-r` / `-f` is required, so the command can only be
/// assembled and spawned once [`struct@Clone`] has transitioned into one of these
/// states.
pub trait CloneTarget: ExclusiveOption {}

impl CloneTarget for RemoteSpecMode {}
impl CloneTarget for FileSpecMode {}

// ---------------------------------------------------------------------------
// Remote port: `-p port`
// ---------------------------------------------------------------------------

/// The `-p port` state: the address (`P4PORT`) of the remote server to clone
/// from, modelled as an ordinary option because `p4 clone` does not accept
/// the global options.
///
/// Entered with [`Clone::port`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Port {
    port: String,
}

// ---------------------------------------------------------------------------
// Command struct
// ---------------------------------------------------------------------------

/// Type-safe builder for `p4 clone`.
///
#[cfg_attr(
    feature = "lt2017_1",
    doc = "Clone a new local Perforce server from a remote server."
)]
#[cfg_attr(
    all(feature = "lt2017_2", not(feature = "lt2017_1")),
    doc = "Clone a new local Helix Core server from a remote server."
)]
#[cfg_attr(
    all(feature = "lt2019_1", not(feature = "lt2017_2")),
    doc = "Clone a new local Helix Server from a remote server."
)]
#[cfg_attr(
    all(feature = "lt2021_1", not(feature = "lt2019_1")),
    doc = "Clone a new local Helix server from a remote server."
)]
#[cfg_attr(
    all(feature = "lt2024_1", not(feature = "lt2021_1")),
    doc = "Clone a new local Helix Server from a remote server."
)]
#[cfg_attr(
    all(feature = "lt2024_2", not(feature = "lt2024_1")),
    doc = "Clone a new local Helix Core Server from a remote server."
)]
#[cfg_attr(
    not(feature = "lt2024_2"),
    doc = "Clone a new local P4 Server from a remote server."
)]
///
/// When you clone from a remote server, you copy the portion of its contents
/// that you want to work with into your local server.
///
/// Unlike most commands, `p4 clone` does not accept the full set of global
/// options: only `-u user`, `-d dir`, and `-c client` are recognized before
/// the subcommand, so they are modelled as ordinary options here rather than
/// through [`GlobalOpts`](crate::global::GlobalOpts).
///
/// # Syntax
///
/// ```text
/// p4 [-u user] [-d dir] [-c client] clone [-m depth] [-v] -p port -r remote
/// p4 [-u user] [-d dir] [-c client] clone [-m depth] [-v] -p port -f filespec
/// ```
///
/// The `P` type parameter tracks the remote port (`-p`) at compile time and
/// the `M` type parameter tracks the clone target (`-r` / `-f`); both start
/// as [`Unselected`], in which the command cannot be assembled. Select a port
/// with [`Self::port`] and exactly one target with [`Self::remote`] or
/// [`Self::file`] before assembling the command.
#[derive(Debug, Clone)]
pub struct Clone<P = Unselected, M = Unselected> {
    bin: PathBuf,

    /// `-u user`
    user: Option<String>,

    /// `-d dir`
    dir: Option<PathBuf>,

    /// `-c client`
    client: Option<String>,

    /// `-m depth`
    depth: Option<u64>,

    /// `-v`
    verbose: bool,

    port: P,

    mode: M,
}

// ---------------------------------------------------------------------------
// Construction
// ---------------------------------------------------------------------------

impl Clone<Unselected, Unselected> {
    /// Creates a new `p4 clone` command with no remote port and no clone
    /// target selected.
    ///
    /// `bin` is the path to the Perforce command-line executable. The command
    /// cannot be assembled in this state; select a remote port with
    /// [`Self::port`] (`-p`) and exactly one target with
    /// [`Self::remote`] (`-r`) or [`Self::file`] (`-f`) first.
    pub fn new(bin: impl Into<PathBuf>) -> Self {
        Self {
            bin: bin.into(),
            user: None,
            dir: None,
            client: None,
            depth: None,
            verbose: false,
            port: Unselected,
            mode: Unselected,
        }
    }
}

// ---------------------------------------------------------------------------
// Common options: available in every state
// ---------------------------------------------------------------------------

impl<P, M> Clone<P, M> {
    /// Returns the path to the Perforce command-line executable.
    pub fn get_bin(&self) -> &Path {
        self.bin.as_path()
    }

    /// Sets the path to the Perforce command-line executable.
    pub fn set_bin(&mut self, bin: impl Into<PathBuf>) -> &mut Self {
        self.bin = bin.into();
        self
    }

    /// Sets the path to the Perforce command-line executable.
    pub fn bin(mut self, bin: impl Into<PathBuf>) -> Self {
        self.bin = bin.into();
        self
    }

    /// Returns the value of `-u user`, if set.
    ///
    /// This is one of the few options that `p4 clone` recognizes *before* the
    /// subcommand, in place of the full set of global options.
    pub fn get_user(&self) -> Option<&str> {
        self.user.as_deref()
    }

    /// Sets the value of `-u user`.
    pub fn set_user(&mut self, v: impl Into<String>) -> &mut Self {
        self.user = Some(v.into());
        self
    }

    /// Sets `-u user`.
    ///
    /// # Description
    ///
    /// -u user
    ///
    #[cfg_attr(feature = "lt2017_2", doc = "Specifies the Perforce user.")]
    #[cfg_attr(
        all(feature = "lt2019_1", not(feature = "lt2017_2")),
        doc = "Specifies the Helix Server user."
    )]
    #[cfg_attr(
        all(feature = "lt2021_1", not(feature = "lt2019_1")),
        doc = "Specifies the Helix server user."
    )]
    #[cfg_attr(
        all(feature = "lt2024_1", not(feature = "lt2021_1")),
        doc = "Specifies the Helix Server user."
    )]
    #[cfg_attr(
        all(feature = "lt2024_2", not(feature = "lt2024_1")),
        doc = "Specifies the Helix Core Server user."
    )]
    #[cfg_attr(not(feature = "lt2024_2"), doc = "Specifies the P4 Server user.")]
    pub fn user(mut self, v: impl Into<String>) -> Self {
        self.user = Some(v.into());
        self
    }

    /// Returns the value of `-d dir`, if set.
    ///
    /// This is one of the few options that `p4 clone` recognizes *before* the
    /// subcommand, in place of the full set of global options.
    pub fn get_dir(&self) -> Option<&Path> {
        self.dir.as_deref()
    }

    /// Sets the value of `-d dir`.
    pub fn set_dir(&mut self, v: impl Into<PathBuf>) -> &mut Self {
        self.dir = Some(v.into());
        self
    }

    /// Sets `-d dir`.
    ///
    /// # Description
    ///
    /// -d dir
    ///
    #[cfg_attr(
        feature = "lt2017_1",
        doc = "Specifies the directory in which the new Perforce server is initialized. If not specified, defaults to the current directory."
    )]
    #[cfg_attr(
        all(feature = "lt2017_2", not(feature = "lt2017_1")),
        doc = "Specifies the directory in which the new Helix Core server is initialized. If not specified, defaults to the current directory."
    )]
    #[cfg_attr(
        all(feature = "lt2019_1", not(feature = "lt2017_2")),
        doc = "Specifies the directory in which the new Helix Server is initialized. If not specified, defaults to the current directory."
    )]
    #[cfg_attr(
        all(feature = "lt2021_1", not(feature = "lt2019_1")),
        doc = "Specifies the directory in which the new Helix server is initialized. If not specified, defaults to the current directory."
    )]
    #[cfg_attr(
        all(feature = "lt2024_1", not(feature = "lt2021_1")),
        doc = "Specifies the directory in which the new Helix Server is initialized. If not specified, defaults to the current directory."
    )]
    #[cfg_attr(
        all(feature = "lt2024_2", not(feature = "lt2024_1")),
        doc = "Specifies the directory in which the new Helix Core Server is initialized. If not specified, defaults to the current directory."
    )]
    #[cfg_attr(
        not(feature = "lt2024_2"),
        doc = "Specifies the directory in which the new P4 Server is initialized. If not specified, defaults to the current directory."
    )]
    pub fn dir(mut self, v: impl Into<PathBuf>) -> Self {
        self.dir = Some(v.into());
        self
    }

    /// Returns the value of `-c client`, if set.
    ///
    /// This is one of the few options that `p4 clone` recognizes *before* the
    /// subcommand, in place of the full set of global options.
    pub fn get_client(&self) -> Option<&str> {
        self.client.as_deref()
    }

    /// Sets the value of `-c client`.
    pub fn set_client(&mut self, v: impl Into<String>) -> &mut Self {
        self.client = Some(v.into());
        self
    }

    /// Sets `-c client`.
    ///
    /// # Description
    ///
    /// -c client
    ///
    /// Specifies the client name. If not specified, defaults to the name
    /// established with the `p4 init` command.
    pub fn client(mut self, v: impl Into<String>) -> Self {
        self.client = Some(v.into());
        self
    }

    /// Returns the value of `-m depth`, if set.
    pub fn get_depth(&self) -> Option<u64> {
        self.depth
    }

    /// Sets the value of `-m depth`.
    pub fn set_depth(&mut self, v: u64) -> &mut Self {
        self.depth = Some(v);
        self
    }

    /// Sets `-m depth`.
    ///
    /// # Description
    ///
    /// -m depth
    ///
    /// Specifies the maximum number of revisions of each file to clone; a
    /// *shallow* clone.
    pub fn depth(mut self, v: u64) -> Self {
        self.depth = Some(v);
        self
    }

    /// Returns whether `-v` verbose mode is enabled.
    pub fn get_verbose(&self) -> bool {
        self.verbose
    }

    /// Sets whether `-v` verbose mode is enabled.
    pub fn set_verbose(&mut self, v: bool) -> &mut Self {
        self.verbose = v;
        self
    }

    /// Enables `-v` verbose mode when `v` is `true`.
    ///
    /// # Description
    ///
    /// -v
    ///
    /// Enables verbose mode.
    pub fn verbose(mut self, v: bool) -> Self {
        self.verbose = v;
        self
    }
}

// ---------------------------------------------------------------------------
// Port selection: `-p port`
// ---------------------------------------------------------------------------

impl<M> Clone<Unselected, M> {
    /// Sets `-p port` and transitions this command to the [`Port`] state.
    ///
    /// # Description
    ///
    /// -p port
    ///
    #[cfg_attr(
        feature = "lt2016_1",
        doc = "Specifies the address (`P4PORT`) of the remote server you wish to clone from."
    )]
    #[cfg_attr(
        not(feature = "lt2016_1"),
        doc = "Specifies the address (`P4PORT`) of the remote server you wish to clone from.",
        doc = "",
        doc = "This flag is optional. If not specified, `p4 clone` uses the remote server specified by the `P4PORT` environment variable."
    )]
    ///
    /// A remote port is required before the command can be assembled.
    pub fn port(self, v: impl Into<String>) -> Clone<Port, M> {
        Clone {
            bin: self.bin,
            user: self.user,
            dir: self.dir,
            client: self.client,
            depth: self.depth,
            verbose: self.verbose,
            port: Port { port: v.into() },
            mode: self.mode,
        }
    }
}

impl<M> Clone<Port, M> {
    /// Returns the value of `-p port`.
    pub fn get_port(&self) -> &str {
        &self.port.port
    }

    /// Sets the value of `-p port`.
    pub fn set_port(&mut self, v: impl Into<String>) -> &mut Self {
        self.port.port = v.into();
        self
    }

    /// Sets `-p port`.
    ///
    /// # Description
    ///
    /// -p port
    ///
    #[cfg_attr(
        feature = "lt2016_1",
        doc = "Specifies the address (`P4PORT`) of the remote server you wish to clone from."
    )]
    #[cfg_attr(
        not(feature = "lt2016_1"),
        doc = "Specifies the address (`P4PORT`) of the remote server you wish to clone from.",
        doc = "",
        doc = "This flag is optional. If not specified, `p4 clone` uses the remote server specified by the `P4PORT` environment variable."
    )]
    pub fn port(mut self, v: impl Into<String>) -> Self {
        self.port.port = v.into();
        self
    }
}

// ---------------------------------------------------------------------------
// Clone target selection: `-r remotespec` vs `-f filespec`
// ---------------------------------------------------------------------------

impl<P> Clone<P, Unselected> {
    /// Sets `-r remotespec` and transitions this command to the
    /// [`RemoteSpecMode`] state.
    ///
    /// # Description
    ///
    /// -r remotespec
    ///
    /// Specifies the remote spec call `remotespec` installed on the remote
    /// server to use as a template for the clone and stream setup.
    ///
    /// This is mutually exclusive with `-f filespec`
    /// ([`Self::file`]).
    pub fn remote(self, v: impl Into<String>) -> Clone<P, RemoteSpecMode> {
        Clone {
            bin: self.bin,
            user: self.user,
            dir: self.dir,
            client: self.client,
            depth: self.depth,
            verbose: self.verbose,
            port: self.port,
            mode: RemoteSpecMode { source: v.into() },
        }
    }

    /// Sets `-f filespec` and transitions this command to the
    /// [`FileSpecMode`] state.
    ///
    /// # Description
    ///
    /// -f filespec
    ///
    #[cfg_attr(
        feature = "lt2016_1",
        doc = "Specifies a `filepath` in the remote server to use as the path to clone. Perforce will use this path to determine the stream setup in the local server. Specifying the filepath also creates a default remote spec called `origin`."
    )]
    #[cfg_attr(
        all(feature = "lt2017_2", not(feature = "lt2016_1")),
        doc = "Specifies a `filespec` in the remote server to use as the path to clone. Perforce uses this path to determine the stream setup in the local server. The Helix Versioning Engine also uses this file specification to determine the stream setup in the personal server. Specifying the filespec also creates a default remote spec called `origin`.",
        doc = "",
        doc = "The `-f` preceding the filespec is optional. You can simply issue the command `p4 clone //file/path`."
    )]
    #[cfg_attr(
        all(feature = "lt2018_1", not(feature = "lt2017_2")),
        doc = "Specifies a `filespec` in the remote server to use as the path to clone. The Helix Versioning Engine uses this path to determine the stream setup in the local server. Helix Server also uses this file specification to determine the stream setup in the personal server. Specifying the filespec also creates a default remote spec called `origin`.",
        doc = "",
        doc = "The `-f` preceding the filespec is optional. You can simply issue the command `p4 clone //file/path`."
    )]
    #[cfg_attr(
        all(feature = "lt2019_1", not(feature = "lt2018_1")),
        doc = "Specifies a `filespec` in the remote server to use as the path to clone. The Helix Core Server uses this path to determine the stream setup in the local server. Helix Server also uses this file specification to determine the stream setup in the personal server. Specifying the filespec also creates a default remote spec called `origin`.",
        doc = "",
        doc = "The `-f` preceding the filespec is optional. You can simply issue the command `p4 clone //file/path`."
    )]
    #[cfg_attr(
        all(feature = "lt2021_1", not(feature = "lt2019_1")),
        doc = "Specifies a `filespec` in the remote server to use as the path to clone. The Helix Core server uses this path to determine the stream setup in the local server. Helix server also uses this file specification to determine the stream setup in the personal server. Specifying the filespec also creates a default remote spec called `origin`.",
        doc = "",
        doc = "The `-f` preceding the filespec is optional. You can simply issue the command `p4 clone //file/path`."
    )]
    #[cfg_attr(
        all(feature = "lt2024_1", not(feature = "lt2021_1")),
        doc = "Specifies a `filespec` in the remote server to use as the path to clone. The Helix Core Server uses this path to determine the stream setup in the local server. Helix Server also uses this file specification to determine the stream setup in the personal server. Specifying the filespec also creates a default remote spec called `origin`.",
        doc = "",
        doc = "The `-f` preceding the filespec is optional. You can simply issue the command `p4 clone //file/path`."
    )]
    #[cfg_attr(
        all(feature = "lt2024_2", not(feature = "lt2024_1")),
        doc = "Specifies a `filespec` in the remote server to use as the path to clone. The Helix Core Server uses this path to determine the stream setup in the local server. Helix Core Server also uses this file specification to determine the stream setup in the personal server. Specifying the filespec also creates a default remote spec called `origin`.",
        doc = "",
        doc = "The `-f` preceding the filespec is optional. You can simply issue the command `p4 clone //file/path`."
    )]
    #[cfg_attr(
        not(feature = "lt2024_2"),
        doc = "Specifies a `filespec` in the remote server to use as the path to clone. The P4 Server uses this path to determine the stream setup in the local server. P4 Server also uses this file specification to determine the stream setup in the personal server. Specifying the filespec also creates a default remote spec called `origin`.",
        doc = "",
        doc = "The `-f` preceding the filespec is optional. You can simply issue the command `p4 clone //file/path`."
    )]
    ///
    /// This is mutually exclusive with `-r remotespec`
    /// ([`Self::remote`]).
    pub fn file(self, v: impl Into<String>) -> Clone<P, FileSpecMode> {
        Clone {
            bin: self.bin,
            user: self.user,
            dir: self.dir,
            client: self.client,
            depth: self.depth,
            verbose: self.verbose,
            port: self.port,
            mode: FileSpecMode { source: v.into() },
        }
    }
}

impl<P> Clone<P, RemoteSpecMode> {
    /// Returns the value of `-r remotespec`.
    pub fn get_source(&self) -> &str {
        &self.mode.source
    }

    /// Sets the value of `-r remotespec`.
    pub fn set_source(&mut self, v: impl Into<String>) -> &mut Self {
        self.mode.source = v.into();
        self
    }

    /// Sets `-r remotespec`.
    ///
    /// # Description
    ///
    /// -r remotespec
    ///
    /// Specifies the remote spec call `remotespec` installed on the remote
    /// server to use as a template for the clone and stream setup.
    pub fn source(mut self, v: impl Into<String>) -> Self {
        self.mode.source = v.into();
        self
    }
}

impl<P> Clone<P, FileSpecMode> {
    /// Returns the value of `-f filespec`.
    pub fn get_source(&self) -> &str {
        &self.mode.source
    }

    /// Sets the value of `-f filespec`.
    pub fn set_source(&mut self, v: impl Into<String>) -> &mut Self {
        self.mode.source = v.into();
        self
    }

    /// Sets `-f filespec`.
    ///
    /// # Description
    ///
    /// -f filespec
    ///
    #[cfg_attr(
        feature = "lt2016_1",
        doc = "Specifies a `filepath` in the remote server to use as the path to clone. Perforce will use this path to determine the stream setup in the local server. Specifying the filepath also creates a default remote spec called `origin`."
    )]
    #[cfg_attr(
        all(feature = "lt2017_2", not(feature = "lt2016_1")),
        doc = "Specifies a `filespec` in the remote server to use as the path to clone. Perforce uses this path to determine the stream setup in the local server. The Helix Versioning Engine also uses this file specification to determine the stream setup in the personal server. Specifying the filespec also creates a default remote spec called `origin`.",
        doc = "",
        doc = "The `-f` preceding the filespec is optional. You can simply issue the command `p4 clone //file/path`."
    )]
    #[cfg_attr(
        all(feature = "lt2018_1", not(feature = "lt2017_2")),
        doc = "Specifies a `filespec` in the remote server to use as the path to clone. The Helix Versioning Engine uses this path to determine the stream setup in the local server. Helix Server also uses this file specification to determine the stream setup in the personal server. Specifying the filespec also creates a default remote spec called `origin`.",
        doc = "",
        doc = "The `-f` preceding the filespec is optional. You can simply issue the command `p4 clone //file/path`."
    )]
    #[cfg_attr(
        all(feature = "lt2019_1", not(feature = "lt2018_1")),
        doc = "Specifies a `filespec` in the remote server to use as the path to clone. The Helix Core Server uses this path to determine the stream setup in the local server. Helix Server also uses this file specification to determine the stream setup in the personal server. Specifying the filespec also creates a default remote spec called `origin`.",
        doc = "",
        doc = "The `-f` preceding the filespec is optional. You can simply issue the command `p4 clone //file/path`."
    )]
    #[cfg_attr(
        all(feature = "lt2021_1", not(feature = "lt2019_1")),
        doc = "Specifies a `filespec` in the remote server to use as the path to clone. The Helix Core server uses this path to determine the stream setup in the local server. Helix server also uses this file specification to determine the stream setup in the personal server. Specifying the filespec also creates a default remote spec called `origin`.",
        doc = "",
        doc = "The `-f` preceding the filespec is optional. You can simply issue the command `p4 clone //file/path`."
    )]
    #[cfg_attr(
        all(feature = "lt2024_1", not(feature = "lt2021_1")),
        doc = "Specifies a `filespec` in the remote server to use as the path to clone. The Helix Core Server uses this path to determine the stream setup in the local server. Helix Server also uses this file specification to determine the stream setup in the personal server. Specifying the filespec also creates a default remote spec called `origin`.",
        doc = "",
        doc = "The `-f` preceding the filespec is optional. You can simply issue the command `p4 clone //file/path`."
    )]
    #[cfg_attr(
        all(feature = "lt2024_2", not(feature = "lt2024_1")),
        doc = "Specifies a `filespec` in the remote server to use as the path to clone. The Helix Core Server uses this path to determine the stream setup in the local server. Helix Core Server also uses this file specification to determine the stream setup in the personal server. Specifying the filespec also creates a default remote spec called `origin`.",
        doc = "",
        doc = "The `-f` preceding the filespec is optional. You can simply issue the command `p4 clone //file/path`."
    )]
    #[cfg_attr(
        not(feature = "lt2024_2"),
        doc = "Specifies a `filespec` in the remote server to use as the path to clone. The P4 Server uses this path to determine the stream setup in the local server. P4 Server also uses this file specification to determine the stream setup in the personal server. Specifying the filespec also creates a default remote spec called `origin`.",
        doc = "",
        doc = "The `-f` preceding the filespec is optional. You can simply issue the command `p4 clone //file/path`."
    )]
    pub fn source(mut self, v: impl Into<String>) -> Self {
        self.mode.source = v.into();
        self
    }
}

// ---------------------------------------------------------------------------
// SubCommand: only after a port (`-p`) and a target (`-r` / `-f`) are selected
// ---------------------------------------------------------------------------

impl<M: CloneTarget> SubCommand for Clone<Port, M> {
    fn name(&self) -> &str {
        "clone"
    }

    /// Injects the options that precede the subcommand name (`-u`, `-d`,
    /// `-c`), then the subcommand name, then the remaining options.
    ///
    /// `p4 clone` does not accept the global options, so no
    /// [`GlobalOpts`](crate::global::GlobalOpts) are injected.
    fn inject_args(&self, command: &mut Command) {
        if let Some(user) = &self.user {
            command.arg("-u").arg(user);
        }

        if let Some(dir) = &self.dir {
            command.arg("-d").arg(dir);
        }

        if let Some(client) = &self.client {
            command.arg("-c").arg(client);
        }

        self.inject_local_args(command.arg(self.name()));
    }

    fn inject_local_args(&self, command: &mut Command) {
        if let Some(depth) = self.depth {
            command.arg("-m").arg(depth.to_string());
        }

        if self.verbose {
            command.arg("-v");
        }

        command.arg("-p").arg(&self.port.port);

        self.mode.inject_args(command);
    }
}

// ---------------------------------------------------------------------------
// Spawning: only after a port (`-p`) and a target (`-r` / `-f`) are selected
// ---------------------------------------------------------------------------

impl<M: CloneTarget> ParameterizedSpawn<()> for Clone<Port, M> {
    type Output = Child;
    type Error = std::io::Error;

    /// Spawns `p4 clone` 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, (): ()) -> 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::args_of;

    #[test]
    fn bin_accessors_update_executable_path() {
        let mut command = Clone::new("p4").port("perforce:1666").remote("origin");
        assert_eq!(command.get_bin(), Path::new("p4"));

        command.set_bin("/usr/local/bin/p4");
        assert_eq!(command.get_bin(), Path::new("/usr/local/bin/p4"));

        let command = command.bin("p4.exe");
        assert_eq!(command.get_bin(), Path::new("p4.exe"));
    }

    #[test]
    fn getters_reflect_selected_values() {
        let mut command = Clone::new("p4").port("perforce:1666").remote("origin");

        assert_eq!(command.get_user(), None);
        assert_eq!(command.get_dir(), None);
        assert_eq!(command.get_client(), None);
        assert_eq!(command.get_depth(), None);
        assert!(!command.get_verbose());
        assert_eq!(command.get_port(), "perforce:1666");
        assert_eq!(command.get_source(), "origin");

        command.set_user("bruno");
        command.set_dir("Ace");
        command.set_client("bruno-ws");
        command.set_depth(5);
        command.set_verbose(true);
        command.set_port("perforce:1667");
        command.set_source("upstream");

        assert_eq!(command.get_user(), Some("bruno"));
        assert_eq!(command.get_dir(), Some(Path::new("Ace")));
        assert_eq!(command.get_client(), Some("bruno-ws"));
        assert_eq!(command.get_depth(), Some(5));
        assert!(command.get_verbose());
        assert_eq!(command.get_port(), "perforce:1667");
        assert_eq!(command.get_source(), "upstream");
    }

    /// Dry-run checks of the assembled `p4 clone` command line; no process is
    /// spawned.
    #[test]
    fn port_and_remote_only() {
        let clone = Clone::new("p4").port("perforce:1666").remote("origin");

        assert_eq!(
            args_of(&clone.setup_command("p4")),
            ["clone", "-p", "perforce:1666", "-r", "origin"]
        );
    }

    #[test]
    fn port_and_file_only() {
        let clone = Clone::new("p4")
            .port("perforce:1666")
            .file("//depot/main/...");

        assert_eq!(
            args_of(&clone.setup_command("p4")),
            ["clone", "-p", "perforce:1666", "-f", "//depot/main/..."]
        );
        assert_eq!(clone.get_source(), "//depot/main/...");
    }

    /// The two required selections are independent of each other: the target
    /// may be chosen before the port.
    #[test]
    fn selection_order_is_independent() {
        let check = |clone: Clone<Port, RemoteSpecMode>| {
            assert_eq!(
                args_of(&clone.setup_command("p4")),
                ["clone", "-p", "perforce:1666", "-r", "origin"]
            );
        };

        check(Clone::new("p4").port("perforce:1666").remote("origin"));
        check(Clone::new("p4").remote("origin").port("perforce:1666"));
    }

    #[test]
    fn all_options_in_syntax_order() {
        let clone = Clone::new("p4")
            .user("bruno")
            .dir("Ace")
            .client("bruno-ws")
            .depth(5)
            .verbose(true)
            .port("perforce:1666")
            .file("//depot/main/...");

        assert_eq!(
            args_of(&clone.setup_command("p4")),
            [
                "-u",
                "bruno",
                "-d",
                "Ace",
                "-c",
                "bruno-ws",
                "clone",
                "-m",
                "5",
                "-v",
                "-p",
                "perforce:1666",
                "-f",
                "//depot/main/..."
            ]
        );
    }

    #[test]
    fn verbose_flag_is_omitted_when_disabled() {
        let clone = Clone::new("p4")
            .port("perforce:1666")
            .remote("origin")
            .verbose(false);

        assert_eq!(
            args_of(&clone.setup_command("p4")),
            ["clone", "-p", "perforce:1666", "-r", "origin"]
        );
    }

    /// `-p`, `-m`, and `-v` are common options: they may be set before the
    /// port and target are selected.
    #[test]
    fn common_options_before_required_selections() {
        let clone = Clone::new("p4")
            .depth(1)
            .verbose(true)
            .port("perforce:1666")
            .remote("origin");

        assert_eq!(
            args_of(&clone.setup_command("p4")),
            [
                "clone",
                "-m",
                "1",
                "-v",
                "-p",
                "perforce:1666",
                "-r",
                "origin"
            ]
        );
    }
}