1use std::ffi::OsStr;
2use std::path::{Path, PathBuf};
3use std::process::{Child, Command, Stdio};
4
5use super::{ExclusiveOption, SubCommand, Unselected};
6
7use crate::global::GlobalOpts;
8use crate::spawn::ParameterizedSpawn;
9
10#[derive(Debug, Clone, Default)]
21pub struct WorkspaceView {
22 client_mapped_only: bool,
23
24 client_have_list_only: bool,
25
26 #[cfg(not(feature = "lt2016_1"))]
27 case_insensitive: bool,
28
29 #[cfg(not(feature = "lt2025_2"))]
30 ignore_changeview: bool,
31
32 stream: Option<String>,
33}
34
35#[cfg(not(feature = "lt2022_2"))]
45#[derive(Debug, Clone, Default)]
46pub struct StreamView {}
47
48impl ExclusiveOption for WorkspaceView {
53 fn inject_args(&self, command: &mut Command) {
54 if self.client_mapped_only {
55 command.arg("-C");
56 }
57
58 if self.client_have_list_only {
59 command.arg("-H");
60 }
61
62 if let Some(stream) = &self.stream {
63 command.arg("-S").arg(stream);
64 }
65
66 #[cfg(not(feature = "lt2016_1"))]
67 if self.case_insensitive {
68 command.arg("-i");
69 }
70
71 #[cfg(not(feature = "lt2025_2"))]
72 if self.ignore_changeview {
73 command.arg("--ignore-changeview");
74 }
75 }
76}
77
78#[cfg(not(feature = "lt2022_2"))]
79impl ExclusiveOption for StreamView {
80 fn inject_args(&self, command: &mut Command) {
81 command.arg("--streamviews");
82 }
83}
84
85#[cfg_attr(
90 feature = "lt2016_1",
91 doc = "`p4 [g-opts] dirs [-C -D -H] [-S stream] depot_directory[revRange]...`"
92)]
93#[cfg_attr(
94 all(not(feature = "lt2016_1"), feature = "lt2017_2"),
95 doc = "`p4 [g-opts] dirs [-C -D -H] [-S stream] [-i] depot_directory[revRange]...`"
96)]
97#[cfg_attr(
98 all(not(feature = "lt2017_2"), feature = "lt2018_1"),
99 doc = "`p4 [g-opts] dirs [-C -D -H] [-S stream] [-i] depot_directory[revRange]...`",
100 doc = "",
101 doc = "`p4 [g-opts] dirs [-S stream] [-i] depot_directory[revRange]...`"
102)]
103#[cfg_attr(
104 all(not(feature = "lt2018_1"), feature = "lt2022_2"),
105 doc = "`p4 [g-opts] dirs [-C -D -H] [-S stream] [-i] depot_directories*[revSpec]`"
106)]
107#[cfg_attr(
108 all(not(feature = "lt2022_2"), feature = "lt2026_1"),
109 doc = "`p4 [g-opts] dirs [-C -D -H] [-S stream] [-i] depot_directories*[revSpec]`",
110 doc = "",
111 doc = "`p4 [g-opts] dirs [-D] [--streamviews] depot_directories*[revSpec]`"
112)]
113#[cfg_attr(
114 not(feature = "lt2026_1"),
115 doc = "`p4 [g-opts] dirs [-C -D -H] [-S stream] [-i] depot_directories*[revSpec]`",
116 doc = "",
117 doc = "`p4 [g-opts] dirs [-D] [--streamviews] depot_directories*[revSpec]`",
118 doc = "",
119 doc = "`p4 [g-opts] dirs [-D] [[-C] [-H] [-S stream] [-i] [--ignore-changeview] | [--streamviews]] depot_directories*[revSpec]`"
120)]
121#[cfg_attr(
126 feature = "lt2018_1",
127 doc = "",
128 doc = "The `depot_directory` argument must be provided in depot or local syntax and must end with the `*` wildcard."
129)]
130#[cfg_attr(
131 all(not(feature = "lt2018_1"), feature = "lt2025_2"),
132 doc = "",
133 doc = "The `depot_directories` argument must be provided in depot or local syntax and must end with the `*` wildcard."
134)]
135#[cfg_attr(
136 not(feature = "lt2025_2"),
137 doc = "",
138 doc = "If you use streams, you must provide the `depot_directories` argument in depot syntax. Otherwise, you can use depot or local syntax. The argument must end with the `*` wildcard."
139)]
140#[cfg_attr(
149 feature = "lt2018_2",
150 doc = "If you include a revision specifier or revision range as part of a directory argument, then the only subdirectories returned are those that contain at least one file revision that matches the given specifier."
151)]
152#[cfg_attr(
153 not(feature = "lt2018_2"),
154 doc = "If you include a revision specifier or revision range as part of a directory argument, the only subdirectories returned are those that contain at least one file revision that matches the given specifier."
155)]
156#[cfg_attr(
160 feature = "lt2024_1",
161 doc = "Perforce does not track directories in its database. Therefore, the `...` wildcard is not supported."
162)]
163#[cfg_attr(
164 all(not(feature = "lt2024_1"), feature = "lt2024_2"),
165 doc = "Helix Core Server does not track directories in its database. Therefore, the `...` wildcard is not supported."
166)]
167#[cfg_attr(
168 not(feature = "lt2024_2"),
169 doc = "P4 Server does not track directories in its database. Therefore, the `...` wildcard is not supported."
170)]
171#[derive(Debug, Clone, Default)]
172pub struct Dirs<M = Unselected> {
173 bin: PathBuf,
174
175 global_opts: GlobalOpts,
176
177 include_deleted: bool,
178
179 mode: M,
180}
181
182impl Dirs<Unselected> {
183 pub fn new(bin: impl Into<PathBuf>, global_opts: GlobalOpts) -> Self {
187 Self {
188 bin: bin.into(),
189 global_opts,
190 include_deleted: false,
191 mode: Unselected,
192 }
193 }
194
195 pub fn client_mapped_only(self) -> Dirs<WorkspaceView> {
205 Dirs {
206 bin: self.bin,
207 global_opts: self.global_opts,
208 include_deleted: self.include_deleted,
209 mode: WorkspaceView {
210 client_mapped_only: true,
211 ..Default::default()
212 },
213 }
214 }
215
216 pub fn client_have_list_only(self) -> Dirs<WorkspaceView> {
226 Dirs {
227 bin: self.bin,
228 global_opts: self.global_opts,
229 include_deleted: self.include_deleted,
230 mode: WorkspaceView {
231 client_have_list_only: true,
232 ..Default::default()
233 },
234 }
235 }
236
237 pub fn stream(self, v: impl Into<String>) -> Dirs<WorkspaceView> {
245 Dirs {
246 bin: self.bin,
247 global_opts: self.global_opts,
248 include_deleted: self.include_deleted,
249 mode: WorkspaceView {
250 stream: Some(v.into()),
251 ..Default::default()
252 },
253 }
254 }
255
256 #[cfg(not(feature = "lt2016_1"))]
266 pub fn case_insensitive(self) -> Dirs<WorkspaceView> {
267 Dirs {
268 bin: self.bin,
269 global_opts: self.global_opts,
270 include_deleted: self.include_deleted,
271 mode: WorkspaceView {
272 case_insensitive: true,
273 ..Default::default()
274 },
275 }
276 }
277
278 #[cfg(not(feature = "lt2025_2"))]
288 pub fn ignore_changeview(self) -> Dirs<WorkspaceView> {
289 Dirs {
290 bin: self.bin,
291 global_opts: self.global_opts,
292 include_deleted: self.include_deleted,
293 mode: WorkspaceView {
294 ignore_changeview: true,
295 ..Default::default()
296 },
297 }
298 }
299
300 #[cfg(not(feature = "lt2022_2"))]
310 pub fn stream_view(self) -> Dirs<StreamView> {
311 Dirs {
312 bin: self.bin,
313 global_opts: self.global_opts,
314 include_deleted: self.include_deleted,
315 mode: StreamView::default(),
316 }
317 }
318}
319
320impl Dirs<WorkspaceView> {
321 pub fn get_client_mapped_only(&self) -> bool {
328 self.mode.client_mapped_only
329 }
330
331 pub fn set_client_mapped_only(&mut self, v: bool) -> &mut Self {
338 self.mode.client_mapped_only = v;
339 self
340 }
341
342 pub fn client_mapped_only(mut self, v: bool) -> Self {
349 self.set_client_mapped_only(v);
350 self
351 }
352
353 pub fn get_client_have_list_only(&self) -> bool {
360 self.mode.client_have_list_only
361 }
362
363 pub fn set_client_have_list_only(&mut self, v: bool) -> &mut Self {
370 self.mode.client_have_list_only = v;
371 self
372 }
373
374 pub fn client_have_list_only(mut self, v: bool) -> Self {
381 self.set_client_have_list_only(v);
382 self
383 }
384
385 pub fn get_stream(&self) -> Option<&str> {
391 self.mode.stream.as_deref()
392 }
393
394 pub fn set_stream(&mut self, v: impl Into<String>) -> &mut Self {
400 self.mode.stream = Some(v.into());
401 self
402 }
403
404 pub fn stream(mut self, v: impl Into<String>) -> Self {
410 self.set_stream(v);
411 self
412 }
413
414 #[cfg(not(feature = "lt2016_1"))]
421 pub fn get_case_insensitive(&self) -> bool {
422 self.mode.case_insensitive
423 }
424
425 #[cfg(not(feature = "lt2016_1"))]
432 pub fn set_case_insensitive(&mut self, v: bool) -> &mut Self {
433 self.mode.case_insensitive = v;
434 self
435 }
436
437 #[cfg(not(feature = "lt2016_1"))]
444 pub fn case_insensitive(mut self, v: bool) -> Self {
445 self.set_case_insensitive(v);
446 self
447 }
448
449 #[cfg(not(feature = "lt2025_2"))]
456 pub fn get_ignore_changeview(&self) -> bool {
457 self.mode.ignore_changeview
458 }
459
460 #[cfg(not(feature = "lt2025_2"))]
467 pub fn set_ignore_changeview(&mut self, v: bool) -> &mut Self {
468 self.mode.ignore_changeview = v;
469 self
470 }
471
472 #[cfg(not(feature = "lt2025_2"))]
479 pub fn ignore_changeview(mut self, v: bool) -> Self {
480 self.set_ignore_changeview(v);
481 self
482 }
483}
484
485impl<M: ExclusiveOption> Dirs<M> {
486 pub fn get_bin(&self) -> &Path {
488 &self.bin
489 }
490
491 pub fn set_bin(&mut self, bin: impl Into<PathBuf>) -> &mut Self {
493 self.bin = bin.into();
494 self
495 }
496
497 pub fn bin(mut self, bin: impl Into<PathBuf>) -> Self {
499 self.set_bin(bin);
500 self
501 }
502
503 #[cfg_attr(
508 feature = "lt2014_2",
509 doc = "See the [Global Options](GlobalOpts) section."
510 )]
511 #[cfg_attr(
512 all(feature = "lt2015_1", not(feature = "lt2014_2")),
513 doc = "See the [“Global Options”](GlobalOpts) section."
514 )]
515 #[cfg_attr(
516 all(feature = "lt2017_1", not(feature = "lt2015_1")),
517 doc = "See [“Global Options”](GlobalOpts)."
518 )]
519 #[cfg_attr(
520 all(feature = "lt2018_2", not(feature = "lt2017_1")),
521 doc = "See [Global Options](GlobalOpts)."
522 )]
523 #[cfg_attr(not(feature = "lt2018_2"), doc = "See [Global options](GlobalOpts).")]
524 pub fn get_global_opts(&self) -> &GlobalOpts {
525 &self.global_opts
526 }
527
528 #[cfg_attr(
533 feature = "lt2014_2",
534 doc = "See the [Global Options](GlobalOpts) section."
535 )]
536 #[cfg_attr(
537 all(feature = "lt2015_1", not(feature = "lt2014_2")),
538 doc = "See the [“Global Options”](GlobalOpts) section."
539 )]
540 #[cfg_attr(
541 all(feature = "lt2017_1", not(feature = "lt2015_1")),
542 doc = "See [“Global Options”](GlobalOpts)."
543 )]
544 #[cfg_attr(
545 all(feature = "lt2018_2", not(feature = "lt2017_1")),
546 doc = "See [Global Options](GlobalOpts)."
547 )]
548 #[cfg_attr(not(feature = "lt2018_2"), doc = "See [Global options](GlobalOpts).")]
549 pub fn set_global_opts(&mut self, v: GlobalOpts) -> &mut Self {
550 self.global_opts = v;
551 self
552 }
553
554 #[cfg_attr(
559 feature = "lt2014_2",
560 doc = "See the [Global Options](GlobalOpts) section."
561 )]
562 #[cfg_attr(
563 all(feature = "lt2015_1", not(feature = "lt2014_2")),
564 doc = "See the [“Global Options”](GlobalOpts) section."
565 )]
566 #[cfg_attr(
567 all(feature = "lt2017_1", not(feature = "lt2015_1")),
568 doc = "See [“Global Options”](GlobalOpts)."
569 )]
570 #[cfg_attr(
571 all(feature = "lt2018_2", not(feature = "lt2017_1")),
572 doc = "See [Global Options](GlobalOpts)."
573 )]
574 #[cfg_attr(not(feature = "lt2018_2"), doc = "See [Global options](GlobalOpts).")]
575 pub fn global_opts(mut self, v: GlobalOpts) -> Self {
576 self.set_global_opts(v);
577 self
578 }
579
580 pub fn get_include_deleted(&self) -> bool {
587 self.include_deleted
588 }
589
590 pub fn set_include_deleted(&mut self, v: bool) -> &mut Self {
597 self.include_deleted = v;
598 self
599 }
600
601 pub fn include_deleted(mut self, v: bool) -> Self {
608 self.set_include_deleted(v);
609 self
610 }
611}
612
613impl<M: ExclusiveOption> SubCommand for Dirs<M> {
614 fn name(&self) -> &str {
615 "dirs"
616 }
617
618 fn inject_local_args(&self, command: &mut Command) {
619 if self.include_deleted {
620 command.arg("-D");
621 }
622
623 self.mode.inject_args(command);
624 }
625
626 fn global_opts(&self) -> Option<&GlobalOpts> {
627 Some(&self.global_opts)
628 }
629}
630
631impl<M: ExclusiveOption> ParameterizedSpawn<()> for Dirs<M> {
632 type Output = Child;
633 type Error = std::io::Error;
634
635 fn spawn_with(&mut self, (): ()) -> Result<Self::Output, Self::Error> {
638 self.setup_command(&self.bin)
639 .stdout(Stdio::piped())
640 .stderr(Stdio::piped())
641 .spawn()
642 }
643}
644
645impl<M, S, I> ParameterizedSpawn<(S,)> for Dirs<M>
646where
647 M: ExclusiveOption,
648 S: IntoIterator<Item = I>,
649 I: AsRef<OsStr>,
650{
651 type Output = Child;
652 type Error = std::io::Error;
653
654 fn spawn_with(&mut self, (directories,): (S,)) -> Result<Self::Output, Self::Error> {
657 self.setup_command(&self.bin)
658 .args(directories)
659 .stdout(Stdio::piped())
660 .stderr(Stdio::piped())
661 .spawn()
662 }
663}
664
665#[cfg(test)]
666mod tests {
667 use super::*;
668 use crate::cmd::args_of;
669
670 fn args(cmd: &impl SubCommand) -> Vec<String> {
671 args_of(&cmd.setup_command("p4"))
672 }
673
674 #[test]
675 fn plain_form() {
676 let cmd = Dirs::new("p4", GlobalOpts::new());
677 assert_eq!(args(&cmd), ["dirs"]);
678 }
679
680 #[test]
681 fn include_deleted_is_shared_by_every_form() {
682 let cmd = Dirs::new("p4", GlobalOpts::new()).include_deleted(true);
683 assert_eq!(args(&cmd), ["dirs", "-D"]);
684
685 let cmd = Dirs::new("p4", GlobalOpts::new())
686 .client_mapped_only()
687 .include_deleted(true);
688 assert_eq!(args(&cmd), ["dirs", "-D", "-C"]);
689
690 #[cfg(not(feature = "lt2022_2"))]
691 {
692 let cmd = Dirs::new("p4", GlobalOpts::new())
693 .stream_view()
694 .include_deleted(true);
695 assert_eq!(args(&cmd), ["dirs", "-D", "--streamviews"]);
696 }
697 }
698
699 #[test]
700 fn workspace_view_arguments() {
701 let cmd = Dirs::new("p4", GlobalOpts::new())
702 .client_mapped_only()
703 .client_have_list_only(true)
704 .stream("main")
705 .include_deleted(true);
706
707 assert_eq!(args(&cmd), ["dirs", "-D", "-C", "-H", "-S", "main"]);
708 }
709
710 #[test]
711 fn stream_accessors() {
712 let mut cmd = Dirs::new("p4", GlobalOpts::new()).stream("release");
713 assert_eq!(cmd.get_stream(), Some("release"));
714
715 cmd.set_stream("main");
716 assert_eq!(cmd.get_stream(), Some("main"));
717 assert_eq!(args(&cmd), ["dirs", "-S", "main"]);
718 }
719
720 #[cfg(not(feature = "lt2016_1"))]
721 #[test]
722 fn case_insensitive_flag() {
723 let cmd = Dirs::new("p4", GlobalOpts::new()).case_insensitive();
724 assert_eq!(args(&cmd), ["dirs", "-i"]);
725
726 let mut cmd = Dirs::new("p4", GlobalOpts::new()).client_mapped_only();
727 assert!(!cmd.get_case_insensitive());
728
729 cmd.set_case_insensitive(true);
730 assert!(cmd.get_case_insensitive());
731 assert_eq!(args(&cmd), ["dirs", "-C", "-i"]);
732 }
733
734 #[cfg(not(feature = "lt2022_2"))]
735 #[test]
736 fn streamviews_flag() {
737 let cmd = Dirs::new("p4", GlobalOpts::new()).stream_view();
738 assert_eq!(args(&cmd), ["dirs", "--streamviews"]);
739 }
740
741 #[cfg(not(feature = "lt2025_2"))]
742 #[test]
743 fn ignore_changeview_flag() {
744 let cmd = Dirs::new("p4", GlobalOpts::new()).ignore_changeview();
745 assert_eq!(args(&cmd), ["dirs", "--ignore-changeview"]);
746
747 let mut cmd = Dirs::new("p4", GlobalOpts::new()).client_mapped_only();
748 assert!(!cmd.get_ignore_changeview());
749
750 cmd.set_ignore_changeview(true);
751 assert!(cmd.get_ignore_changeview());
752 assert_eq!(args(&cmd), ["dirs", "-C", "--ignore-changeview"]);
753 }
754
755 #[test]
756 fn global_opts_are_injected_once() {
757 let cmd = Dirs::new(
758 "p4",
759 GlobalOpts::new().port("localhost:1666").quiet_mode(true),
760 )
761 .stream("main");
762
763 assert_eq!(
764 args(&cmd),
765 ["-p", "localhost:1666", "-q", "dirs", "-S", "main"]
766 );
767 }
768}