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
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
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
use std::ffi::OsStr;
use std::path::{Path, PathBuf};
use std::process::{Child, Command, Stdio};

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

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

/// Selection marker for the `-a` option: report statistics for all tables.
///
/// Because `-a` takes no positional arguments, this state carries no table
/// list; spawn it with `()`.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct DisplayAllTables;

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

/// The `-s` form: `p4 [g-opts] dbstat -s`.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct Size;

/// The scan form.
///
#[cfg_attr(
    feature = "lt2016_1",
    doc = "`p4 [g-opts] dbstat [-h] {-a | dbtable ...}`."
)]
#[cfg_attr(
    not(feature = "lt2016_1"),
    doc = "`p4 [g-opts] dbstat [-h][-f] {-a | dbtable ...}`."
)]
///
/// The `M` type parameter tracks the table selection: [`Unselected`] spawns
/// with a list of `dbtable` arguments; [`DisplayAllTables`] supplies `-a` and
/// spawns without arguments.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Scan<M = Unselected> {
    show_histogram: bool,

    #[cfg(not(feature = "lt2016_1"))]
    show_free_pages: bool,

    mode: M,
}

impl ExclusiveOption for Scan<Unselected> {
    fn inject_args(&self, command: &mut Command) {
        if self.show_histogram {
            command.arg("-h");
        }

        #[cfg(not(feature = "lt2016_1"))]
        if self.show_free_pages {
            command.arg("-f");
        }
    }
}

impl ExclusiveOption for Scan<DisplayAllTables> {
    fn inject_args(&self, command: &mut Command) {
        if self.show_histogram {
            command.arg("-h");
        }

        #[cfg(not(feature = "lt2016_1"))]
        if self.show_free_pages {
            command.arg("-f");
        }

        command.arg("-a");
    }
}

/// The checkpoint form:
/// `p4 [g-opts] dbstat -p [-w worklevel] [-n numberOfFiles] {-a | dbtable ...}`.
///
/// Mutually exclusive with the scan form: `-p` cannot be combined with `-h`
/// or `-f`.
#[cfg(not(feature = "lt2023_1"))]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CheckPoint<M = Unselected> {
    worklevel: Option<u64>,

    number_files: Option<u64>,

    mode: M,
}

#[cfg(not(feature = "lt2023_1"))]
impl ExclusiveOption for CheckPoint<Unselected> {
    fn inject_args(&self, command: &mut Command) {
        command.arg("-p");

        if let Some(worklevel) = self.worklevel {
            command.arg("-w").arg(worklevel.to_string());
        }

        if let Some(number_files) = self.number_files {
            command.arg("-n").arg(number_files.to_string());
        }
    }
}

#[cfg(not(feature = "lt2023_1"))]
impl ExclusiveOption for CheckPoint<DisplayAllTables> {
    fn inject_args(&self, command: &mut Command) {
        command.arg("-p");

        if let Some(worklevel) = self.worklevel {
            command.arg("-w").arg(worklevel.to_string());
        }

        if let Some(number_files) = self.number_files {
            command.arg("-n").arg(number_files.to_string());
        }

        command.arg("-a");
    }
}

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

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

#[cfg_attr(
    feature = "lt2016_1",
    doc = "`p4 [g-opts] dbstat [-h] {-a | dbtable ...}`"
)]
#[cfg_attr(
    all(feature = "lt2023_1", not(feature = "lt2016_1")),
    doc = "`p4 [g-opts] dbstat [-h][-f] {-a | dbtable ...}`"
)]
#[cfg_attr(
    not(feature = "lt2023_1"),
    doc = "`p4 [g-opts] dbstat [-h][-f] | [-p [-w worklevel] [-n numberOfFiles]] {-a | dbtable ...}`"
)]
///
/// `p4 [g-opts] dbstat -s`
///
/// Displays size or simple statistics for one or more database tables. The
/// `dbtable` corresponds to the `db.*` files in your server's root directory.
///
/// The `M` type parameter tracks the command form at compile time, starting
/// as [`Unselected`] from [`DBStat::new`]. The available states and
/// transitions are:
/// - [`Unselected`] / [`DisplayAllTables`]: the bare `{-a | dbtable ...}`
///   selection tail; spawn the former with a list of table names, or use
///   [`Self::all`] to supply `-a` and spawn the latter with `()`. Both states
///   can enter the scan and checkpoint forms; the transitions defined on
///   `DisplayAllTables` keep `-a`.
#[cfg_attr(
    feature = "lt2016_1",
    doc = "- [`Scan`] (entered via [`Self::show_histogram`]): the histogram option followed by a table selection; [`Self::all`] supplies `-a` and transitions to the [`DisplayAllTables`] state."
)]
#[cfg_attr(
    not(feature = "lt2016_1"),
    doc = "- [`Scan`] (entered via [`Self::show_histogram`] or [`Self::show_free_pages`]): histogram and free-page options followed by a table selection; [`Self::all`] supplies `-a` and transitions to the [`DisplayAllTables`] state."
)]
#[cfg_attr(
    not(feature = "lt2023_1"),
    doc = "- [`CheckPoint`] (entered via [`Self::checkpoint`]): `-p` with optional `-w`/`-n`; mutually exclusive with the scan form."
)]
/// - [`Size`] (entered via [`Self::size`]): the `-s` form.
#[derive(Debug, Clone, Default)]
pub struct DBStat<M = Unselected> {
    bin: PathBuf,

    global_opts: GlobalOpts,

    mode: M,
}

// ---------------------------------------------------------------------------
// Constructor and form transitions
// ---------------------------------------------------------------------------

impl DBStat<Unselected> {
    /// Creates a new `p4 dbstat` command.
    ///
    /// `bin` is the path to the Perforce command-line executable. Call one of
    /// the form transitions to select a command form.
    pub fn new(bin: impl Into<PathBuf>, global_opts: GlobalOpts) -> Self {
        Self {
            bin: bin.into(),
            global_opts,
            mode: Unselected,
        }
    }

    /// Enters the scan form with `-h` enabled:
    #[cfg_attr(feature = "lt2016_1", doc = "`p4 dbstat [-h] {-a | dbtable ...}`.")]
    #[cfg_attr(
        not(feature = "lt2016_1"),
        doc = "`p4 dbstat [-h][-f] {-a | dbtable ...}`."
    )]
    ///
    #[cfg_attr(
        feature = "lt2024_1",
        doc = "Display a histogram showing distances between leaf pages."
    )]
    #[cfg_attr(
        not(feature = "lt2024_1"),
        doc = "Display a histogram showing distances between leaf pages. This enables you to anticipate disk seeks required for sequential database scans. This scans only the internal node pages of the file, not the leaf or overflow pages, and thus reads only 1-5% of the file.",
        doc = "",
        doc = "> **Note**",
        doc = ">",
        doc = "> This option blocks write access to the database while it scans the tables. It is best to get guidance from [Perforce Support](https://www.perforce.com/support?product=Helix%20Core) before using this option."
    )]
    ///
    /// Use the scan state's `all` transition to supply `-a`; otherwise
    /// spawn with a list of table names.
    pub fn show_histogram(self) -> DBStat<Scan<Unselected>> {
        DBStat {
            bin: self.bin,
            global_opts: self.global_opts,
            mode: Scan {
                show_histogram: true,
                #[cfg(not(feature = "lt2016_1"))]
                show_free_pages: false,
                mode: Unselected,
            },
        }
    }

    /// Enters the scan form with `-f` enabled:
    /// `p4 dbstat [-h][-f] {-a | dbtable ...}`.
    ///
    #[cfg_attr(
        feature = "lt2024_1",
        doc = "Displays a page count, free pages, and percent free data for the specified tables."
    )]
    #[cfg_attr(
        not(feature = "lt2024_1"),
        doc = "Displays a page count, free pages, and percent free data for the specified tables. Using the `-f` with the `-h` option, this command shows a histogram of free page distribution."
    )]
    ///
    /// Use the scan state's `all` transition to supply `-a`; otherwise
    /// spawn with a list of table names.
    #[cfg(not(feature = "lt2016_1"))]
    pub fn show_free_pages(self) -> DBStat<Scan<Unselected>> {
        DBStat {
            bin: self.bin,
            global_opts: self.global_opts,
            mode: Scan {
                show_histogram: false,
                show_free_pages: true,
                mode: Unselected,
            },
        }
    }

    /// # Description
    ///
    /// -a
    ///
    /// Display statistics for all tables.
    ///
    /// Transitions to the [`DisplayAllTables`] state, which spawns without
    /// table arguments.
    pub fn all(self) -> DBStat<DisplayAllTables> {
        DBStat {
            bin: self.bin,
            global_opts: self.global_opts,
            mode: DisplayAllTables,
        }
    }

    /// Enters the checkpoint form with `-w worklevel` set:
    /// `p4 dbstat -p -w worklevel {-a | dbtable ...}`.
    ///
    /// The checkpoint form is mutually exclusive with the scan form.
    #[cfg(not(feature = "lt2023_1"))]
    pub fn worklevel(self, v: u64) -> DBStat<CheckPoint<Unselected>> {
        DBStat {
            bin: self.bin,
            global_opts: self.global_opts,
            mode: CheckPoint {
                worklevel: Some(v),
                number_files: None,
                mode: Unselected,
            },
        }
    }

    /// Enters the checkpoint form with `-n numberOfFiles` set:
    /// `p4 dbstat -p -n numberOfFiles {-a | dbtable ...}`.
    ///
    /// The checkpoint form is mutually exclusive with the scan form.
    #[cfg(not(feature = "lt2023_1"))]
    pub fn number_files(self, v: u64) -> DBStat<CheckPoint<Unselected>> {
        DBStat {
            bin: self.bin,
            global_opts: self.global_opts,
            mode: CheckPoint {
                worklevel: None,
                number_files: Some(v),
                mode: Unselected,
            },
        }
    }

    /// Enters the checkpoint form:
    /// `p4 dbstat -p [-w worklevel] [-n numberOfFiles] {-a | dbtable ...}`.
    ///
    /// Use [`Self::worklevel`] or [`Self::number_files`] to enter the form
    /// with `-w` or `-n` already set; use [`Self::all`] to supply `-a`,
    /// otherwise spawn with a list of table names.
    #[cfg(not(feature = "lt2023_1"))]
    pub fn checkpoint(self) -> DBStat<CheckPoint<Unselected>> {
        DBStat {
            bin: self.bin,
            global_opts: self.global_opts,
            mode: CheckPoint {
                worklevel: None,
                number_files: None,
                mode: Unselected,
            },
        }
    }

    /// Enters the size form: `p4 dbstat -s`.
    pub fn size(self) -> DBStat<Size> {
        DBStat {
            bin: self.bin,
            global_opts: self.global_opts,
            mode: Size,
        }
    }
}

// ---------------------------------------------------------------------------
// Transitions from the `-a` state
// ---------------------------------------------------------------------------

impl DBStat<DisplayAllTables> {
    /// Enters the scan form with `-h` set and `-a` retained:
    /// `p4 dbstat [-h][-f] -a`.
    pub fn show_histogram(self) -> DBStat<Scan<DisplayAllTables>> {
        DBStat {
            bin: self.bin,
            global_opts: self.global_opts,
            mode: Scan {
                show_histogram: true,
                #[cfg(not(feature = "lt2016_1"))]
                show_free_pages: false,
                mode: DisplayAllTables,
            },
        }
    }

    /// Enters the scan form with `-f` set and `-a` retained:
    /// `p4 dbstat [-h][-f] -a`.
    #[cfg(not(feature = "lt2016_1"))]
    pub fn show_free_pages(self) -> DBStat<Scan<DisplayAllTables>> {
        DBStat {
            bin: self.bin,
            global_opts: self.global_opts,
            mode: Scan {
                show_histogram: false,
                show_free_pages: true,
                mode: DisplayAllTables,
            },
        }
    }

    /// Enters the checkpoint form with `-a` retained: `p4 dbstat -p -a`.
    #[cfg(not(feature = "lt2023_1"))]
    pub fn checkpoint(self) -> DBStat<CheckPoint<DisplayAllTables>> {
        DBStat {
            bin: self.bin,
            global_opts: self.global_opts,
            mode: CheckPoint {
                worklevel: None,
                number_files: None,
                mode: DisplayAllTables,
            },
        }
    }

    /// Enters the checkpoint form with `-w worklevel` set and `-a` retained:
    /// `p4 dbstat -p -w worklevel -a`.
    #[cfg(not(feature = "lt2023_1"))]
    pub fn worklevel(self, v: u64) -> DBStat<CheckPoint<DisplayAllTables>> {
        DBStat {
            bin: self.bin,
            global_opts: self.global_opts,
            mode: CheckPoint {
                worklevel: Some(v),
                number_files: None,
                mode: DisplayAllTables,
            },
        }
    }

    /// Enters the checkpoint form with `-n numberOfFiles` set and `-a`
    /// retained: `p4 dbstat -p -n numberOfFiles -a`.
    #[cfg(not(feature = "lt2023_1"))]
    pub fn number_files(self, v: u64) -> DBStat<CheckPoint<DisplayAllTables>> {
        DBStat {
            bin: self.bin,
            global_opts: self.global_opts,
            mode: CheckPoint {
                worklevel: None,
                number_files: Some(v),
                mode: DisplayAllTables,
            },
        }
    }
}

impl<M> DBStat<Scan<M>> {
    /// # Description
    ///
    /// -h
    ///
    #[cfg_attr(
        feature = "lt2024_1",
        doc = "Display a histogram showing distances between leaf pages."
    )]
    #[cfg_attr(
        not(feature = "lt2024_1"),
        doc = "Display a histogram showing distances between leaf pages. This enables you to anticipate disk seeks required for sequential database scans. This scans only the internal node pages of the file, not the leaf or overflow pages, and thus reads only 1-5% of the file.",
        doc = "",
        doc = "> **Note**",
        doc = ">",
        doc = "> This option blocks write access to the database while it scans the tables. It is best to get guidance from [Perforce Support](https://www.perforce.com/support?product=Helix%20Core) before using this option."
    )]
    pub fn get_show_histogram(&self) -> bool {
        self.mode.show_histogram
    }

    /// # Description
    ///
    /// -h
    ///
    #[cfg_attr(
        feature = "lt2024_1",
        doc = "Display a histogram showing distances between leaf pages."
    )]
    #[cfg_attr(
        not(feature = "lt2024_1"),
        doc = "Display a histogram showing distances between leaf pages. This enables you to anticipate disk seeks required for sequential database scans. This scans only the internal node pages of the file, not the leaf or overflow pages, and thus reads only 1-5% of the file.",
        doc = "",
        doc = "> **Note**",
        doc = ">",
        doc = "> This option blocks write access to the database while it scans the tables. It is best to get guidance from [Perforce Support](https://www.perforce.com/support?product=Helix%20Core) before using this option."
    )]
    pub fn set_show_histogram(&mut self, v: bool) -> &mut Self {
        self.mode.show_histogram = v;
        self
    }

    /// # Description
    ///
    /// -h
    ///
    #[cfg_attr(
        feature = "lt2024_1",
        doc = "Display a histogram showing distances between leaf pages."
    )]
    #[cfg_attr(
        not(feature = "lt2024_1"),
        doc = "Display a histogram showing distances between leaf pages. This enables you to anticipate disk seeks required for sequential database scans. This scans only the internal node pages of the file, not the leaf or overflow pages, and thus reads only 1-5% of the file.",
        doc = "",
        doc = "> **Note**",
        doc = ">",
        doc = "> This option blocks write access to the database while it scans the tables. It is best to get guidance from [Perforce Support](https://www.perforce.com/support?product=Helix%20Core) before using this option."
    )]
    pub fn show_histogram(mut self, v: bool) -> Self {
        self.set_show_histogram(v);
        self
    }

    /// # Description
    ///
    /// -f
    ///
    #[cfg_attr(
        feature = "lt2024_1",
        doc = "Displays a page count, free pages, and percent free data for the specified tables."
    )]
    #[cfg_attr(
        not(feature = "lt2024_1"),
        doc = "Displays a page count, free pages, and percent free data for the specified tables. Using the `-f` with the `-h` option, this command shows a histogram of free page distribution."
    )]
    #[cfg(not(feature = "lt2016_1"))]
    pub fn get_show_free_pages(&self) -> bool {
        self.mode.show_free_pages
    }

    /// # Description
    ///
    /// -f
    ///
    #[cfg_attr(
        feature = "lt2024_1",
        doc = "Displays a page count, free pages, and percent free data for the specified tables."
    )]
    #[cfg_attr(
        not(feature = "lt2024_1"),
        doc = "Displays a page count, free pages, and percent free data for the specified tables. Using the `-f` with the `-h` option, this command shows a histogram of free page distribution."
    )]
    #[cfg(not(feature = "lt2016_1"))]
    pub fn set_show_free_pages(&mut self, v: bool) -> &mut Self {
        self.mode.show_free_pages = v;
        self
    }

    /// # Description
    ///
    /// -f
    ///
    #[cfg_attr(
        feature = "lt2024_1",
        doc = "Displays a page count, free pages, and percent free data for the specified tables."
    )]
    #[cfg_attr(
        not(feature = "lt2024_1"),
        doc = "Displays a page count, free pages, and percent free data for the specified tables. Using the `-f` with the `-h` option, this command shows a histogram of free page distribution."
    )]
    #[cfg(not(feature = "lt2016_1"))]
    pub fn show_free_pages(mut self, v: bool) -> Self {
        self.set_show_free_pages(v);
        self
    }
}

impl DBStat<Scan<Unselected>> {
    /// # Description
    ///
    /// -a
    ///
    /// Display statistics for all tables.
    ///
    /// Transitions to the [`DisplayAllTables`] state, which spawns without
    /// table arguments.
    pub fn all(self) -> DBStat<Scan<DisplayAllTables>> {
        DBStat {
            bin: self.bin,
            global_opts: self.global_opts,
            mode: Scan {
                show_histogram: self.mode.show_histogram,
                #[cfg(not(feature = "lt2016_1"))]
                show_free_pages: self.mode.show_free_pages,
                mode: DisplayAllTables,
            },
        }
    }
}

#[cfg(not(feature = "lt2023_1"))]
impl<M> DBStat<CheckPoint<M>> {
    /// # Description
    ///
    /// -w worklevel
    ///
    #[cfg_attr(
        feature = "lt2024_1",
        doc = "Overrides the [db.checkpoint.worklevel](https://help.perforce.com/helix-core/server-apps/cmdref/current/Content/CmdRef/configurables.alphabetical.html#db.checkpoint.worklevel) configurable so you can specify the `worklevel` at the command line."
    )]
    #[cfg_attr(
        not(feature = "lt2024_1"),
        doc = "The `worklevel` determines which level of the table should be used to split the table. This option overrides the [db.checkpoint.worklevel](https://help.perforce.com/helix-core/server-apps/cmdref/current/Content/CmdRef/configurables.alphabetical.html#db.checkpoint.worklevel) configurable so you can specify the `worklevel` at the command line."
    )]
    pub fn get_worklevel(&self) -> Option<u64> {
        self.mode.worklevel
    }

    /// # Description
    ///
    /// -w worklevel
    ///
    #[cfg_attr(
        feature = "lt2024_1",
        doc = "Overrides the [db.checkpoint.worklevel](https://help.perforce.com/helix-core/server-apps/cmdref/current/Content/CmdRef/configurables.alphabetical.html#db.checkpoint.worklevel) configurable so you can specify the `worklevel` at the command line."
    )]
    #[cfg_attr(
        not(feature = "lt2024_1"),
        doc = "The `worklevel` determines which level of the table should be used to split the table. This option overrides the [db.checkpoint.worklevel](https://help.perforce.com/helix-core/server-apps/cmdref/current/Content/CmdRef/configurables.alphabetical.html#db.checkpoint.worklevel) configurable so you can specify the `worklevel` at the command line."
    )]
    pub fn set_worklevel(&mut self, v: u64) -> &mut Self {
        self.mode.worklevel = Some(v);
        self
    }

    /// # Description
    ///
    /// -w worklevel
    ///
    #[cfg_attr(
        feature = "lt2024_1",
        doc = "Overrides the [db.checkpoint.worklevel](https://help.perforce.com/helix-core/server-apps/cmdref/current/Content/CmdRef/configurables.alphabetical.html#db.checkpoint.worklevel) configurable so you can specify the `worklevel` at the command line."
    )]
    #[cfg_attr(
        not(feature = "lt2024_1"),
        doc = "The `worklevel` determines which level of the table should be used to split the table. This option overrides the [db.checkpoint.worklevel](https://help.perforce.com/helix-core/server-apps/cmdref/current/Content/CmdRef/configurables.alphabetical.html#db.checkpoint.worklevel) configurable so you can specify the `worklevel` at the command line."
    )]
    pub fn worklevel(mut self, v: u64) -> Self {
        self.set_worklevel(v);
        self
    }

    /// # Description
    ///
    /// -n numberOfFiles
    ///
    #[cfg_attr(
        feature = "lt2024_1",
        doc = "Overrides the [db.checkpoint.numfiles](https://help.perforce.com/helix-core/server-apps/cmdref/current/Content/CmdRef/configurables.alphabetical.html#db.checkpoint.numfiles) configurable so you can specify `numberOfFiles` at the command line."
    )]
    #[cfg_attr(
        not(feature = "lt2024_1"),
        doc = "This option determines, along with the depth of the database table, the number of checkpoint files that would be generated. A large table might contain multiple pages. The `-n numberOfFiles` requested makes it possible divide the page count to split each chunk of work into similar sizes. This option overrides the [db.checkpoint.numfiles](https://help.perforce.com/helix-core/server-apps/cmdref/current/Content/CmdRef/configurables.alphabetical.html#db.checkpoint.numfiles) configurable so you can specify `numberOfFiles` at the command line."
    )]
    pub fn get_number_files(&self) -> Option<u64> {
        self.mode.number_files
    }

    /// # Description
    ///
    /// -n numberOfFiles
    ///
    #[cfg_attr(
        feature = "lt2024_1",
        doc = "Overrides the [db.checkpoint.numfiles](https://help.perforce.com/helix-core/server-apps/cmdref/current/Content/CmdRef/configurables.alphabetical.html#db.checkpoint.numfiles) configurable so you can specify `numberOfFiles` at the command line."
    )]
    #[cfg_attr(
        not(feature = "lt2024_1"),
        doc = "This option determines, along with the depth of the database table, the number of checkpoint files that would be generated. A large table might contain multiple pages. The `-n numberOfFiles` requested makes it possible divide the page count to split each chunk of work into similar sizes. This option overrides the [db.checkpoint.numfiles](https://help.perforce.com/helix-core/server-apps/cmdref/current/Content/CmdRef/configurables.alphabetical.html#db.checkpoint.numfiles) configurable so you can specify `numberOfFiles` at the command line."
    )]
    pub fn set_number_files(&mut self, v: u64) -> &mut Self {
        self.mode.number_files = Some(v);
        self
    }

    /// # Description
    ///
    /// -n numberOfFiles
    ///
    #[cfg_attr(
        feature = "lt2024_1",
        doc = "Overrides the [db.checkpoint.numfiles](https://help.perforce.com/helix-core/server-apps/cmdref/current/Content/CmdRef/configurables.alphabetical.html#db.checkpoint.numfiles) configurable so you can specify `numberOfFiles` at the command line."
    )]
    #[cfg_attr(
        not(feature = "lt2024_1"),
        doc = "This option determines, along with the depth of the database table, the number of checkpoint files that would be generated. A large table might contain multiple pages. The `-n numberOfFiles` requested makes it possible divide the page count to split each chunk of work into similar sizes. This option overrides the [db.checkpoint.numfiles](https://help.perforce.com/helix-core/server-apps/cmdref/current/Content/CmdRef/configurables.alphabetical.html#db.checkpoint.numfiles) configurable so you can specify `numberOfFiles` at the command line."
    )]
    pub fn number_files(mut self, v: u64) -> Self {
        self.set_number_files(v);
        self
    }
}

#[cfg(not(feature = "lt2023_1"))]
impl DBStat<CheckPoint<Unselected>> {
    /// # Description
    ///
    /// -a
    ///
    /// Display statistics for all tables.
    ///
    /// Transitions to the [`DisplayAllTables`] state, which spawns without
    /// table arguments.
    pub fn all(self) -> DBStat<CheckPoint<DisplayAllTables>> {
        DBStat {
            bin: self.bin,
            global_opts: self.global_opts,
            mode: CheckPoint {
                worklevel: self.mode.worklevel,
                number_files: self.mode.number_files,
                mode: DisplayAllTables,
            },
        }
    }
}

// ---------------------------------------------------------------------------
// Accessors and command assembly
// ---------------------------------------------------------------------------

impl<M> DBStat<M> {
    /// Get the Perforce command path.
    pub fn get_bin(&self) -> &Path {
        &self.bin
    }

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

    /// Set the Perforce command path.
    pub fn bin(mut self, bin: impl Into<PathBuf>) -> Self {
        self.set_bin(bin);
        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 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> SubCommand for DBStat<M>
where
    M: ExclusiveOption,
{
    fn name(&self) -> &str {
        "dbstat"
    }

    fn inject_local_args(&self, command: &mut Command) {
        self.mode.inject_args(command);
    }

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

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

    /// Spawns `p4 dbstat` for the given tables 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, (tables,): (S,)) -> Result<Self::Output, Self::Error> {
        self.setup_command(&self.bin)
            .args(tables)
            .stdout(Stdio::piped())
            .stderr(Stdio::piped())
            .spawn()
    }
}

impl ParameterizedSpawn<()> for DBStat<DisplayAllTables> {
    type Output = Child;
    type Error = std::io::Error;

    /// Spawns `p4 dbstat -a` 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()
    }
}

impl ParameterizedSpawn<()> for DBStat<Scan<DisplayAllTables>> {
    type Output = Child;
    type Error = std::io::Error;

    /// Spawns `p4 dbstat -a` 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()
    }
}

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

    /// Spawns `p4 dbstat` for the given tables 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, (tables,): (S,)) -> Result<Self::Output, Self::Error> {
        self.setup_command(&self.bin)
            .args(tables)
            .stdout(Stdio::piped())
            .stderr(Stdio::piped())
            .spawn()
    }
}

#[cfg(not(feature = "lt2023_1"))]
impl ParameterizedSpawn<()> for DBStat<CheckPoint<DisplayAllTables>> {
    type Output = Child;
    type Error = std::io::Error;

    /// Spawns `p4 dbstat -p -a` 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(not(feature = "lt2023_1"))]
impl<S, I> ParameterizedSpawn<(S,)> for DBStat<CheckPoint<Unselected>>
where
    S: IntoIterator<Item = I>,
    I: AsRef<OsStr>,
{
    type Output = Child;
    type Error = std::io::Error;

    /// Spawns `p4 dbstat -p` for the given tables 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, (tables,): (S,)) -> Result<Self::Output, Self::Error> {
        self.setup_command(&self.bin)
            .args(tables)
            .stdout(Stdio::piped())
            .stderr(Stdio::piped())
            .spawn()
    }
}

impl ParameterizedSpawn<()> for DBStat<Size> {
    type Output = Child;
    type Error = std::io::Error;

    /// Spawns `p4 dbstat -s` 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 = DBStat::new("p4", GlobalOpts::new()).show_histogram();
        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"));
    }

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

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

    #[test]
    fn plain_form_all() {
        let dbstat = DBStat::new("p4", GlobalOpts::new()).all();

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

    #[test]
    fn show_histogram_enters_scan_form() {
        let dbstat = DBStat::new("p4", GlobalOpts::new()).show_histogram();

        assert!(dbstat.get_show_histogram());
        assert_eq!(args_of(&dbstat.setup_command("p4")), ["dbstat", "-h"]);
    }

    #[cfg(not(feature = "lt2016_1"))]
    #[test]
    fn show_free_pages_enters_scan_form() {
        let dbstat = DBStat::new("p4", GlobalOpts::new()).show_free_pages();

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

    #[cfg(not(feature = "lt2016_1"))]
    #[test]
    fn scan_form_with_histogram_and_free_pages() {
        let dbstat = DBStat::new("p4", GlobalOpts::new())
            .show_histogram()
            .show_free_pages(true);

        assert!(dbstat.get_show_histogram());
        assert!(dbstat.get_show_free_pages());
        assert_eq!(args_of(&dbstat.setup_command("p4")), ["dbstat", "-h", "-f"]);
    }

    #[test]
    fn scan_form_all() {
        let dbstat = DBStat::new("p4", GlobalOpts::new()).show_histogram().all();

        assert_eq!(args_of(&dbstat.setup_command("p4")), ["dbstat", "-h", "-a"]);
    }

    #[test]
    fn size_form() {
        let dbstat = DBStat::new("p4", GlobalOpts::new()).size();

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

    #[cfg(not(feature = "lt2023_1"))]
    #[test]
    fn checkpoint_form_with_worklevel_and_number_files() {
        let mut dbstat = DBStat::new("p4", GlobalOpts::new()).checkpoint();
        dbstat.set_worklevel(2);
        let dbstat = dbstat.number_files(3);

        assert_eq!(dbstat.get_worklevel(), Some(2));
        assert_eq!(dbstat.get_number_files(), Some(3));
        assert_eq!(
            args_of(&dbstat.setup_command("p4")),
            ["dbstat", "-p", "-w", "2", "-n", "3"]
        );
    }

    #[cfg(not(feature = "lt2023_1"))]
    #[test]
    fn checkpoint_form_without_options() {
        let dbstat = DBStat::new("p4", GlobalOpts::new()).checkpoint();

        assert_eq!(dbstat.get_worklevel(), None);
        assert_eq!(dbstat.get_number_files(), None);
        assert_eq!(args_of(&dbstat.setup_command("p4")), ["dbstat", "-p"]);
    }

    #[cfg(not(feature = "lt2023_1"))]
    #[test]
    fn checkpoint_form_all_keeps_options() {
        let dbstat = DBStat::new("p4", GlobalOpts::new())
            .checkpoint()
            .worklevel(2)
            .all();

        assert_eq!(
            args_of(&dbstat.setup_command("p4")),
            ["dbstat", "-p", "-w", "2", "-a"]
        );
    }

    #[test]
    fn global_opts_are_injected_once() {
        let dbstat = DBStat::new(
            "p4",
            GlobalOpts::new().port("localhost:1666").quiet_mode(true),
        )
        .show_histogram()
        .all();

        assert_eq!(
            args_of(&dbstat.setup_command("p4")),
            ["-p", "localhost:1666", "-q", "dbstat", "-h", "-a"]
        );
    }
}