Skip to main content

perforce_cli/cmd/
cstat.rs

1use std::ffi::OsStr;
2use std::path::{Path, PathBuf};
3use std::process::{Child, Command, Stdio};
4
5use super::SubCommand;
6
7use crate::global::GlobalOpts;
8use crate::spawn::ParameterizedSpawn;
9
10/// `p4 [g-opts] cstat [files ...]`
11///
12#[cfg_attr(
13    feature = "lt2023_1",
14    doc = "Dump change/sync status for current client workspace."
15)]
16#[cfg_attr(
17    not(feature = "lt2023_1"),
18    doc = "Dump change or sync status for the current client workspace."
19)]
20///
21/// The `p4 cstat` command lists changes that are required, already synced, or
22/// partially synced to the current client workspace. Spawn it with a list of
23/// files to restrict the report; with no files, all changes are reported.
24#[derive(Debug, Clone, Default)]
25pub struct CStat {
26    bin: PathBuf,
27
28    global_opts: GlobalOpts,
29}
30
31impl CStat {
32    /// Creates a new `p4 cstat` command.
33    ///
34    /// `bin` is the path to the Perforce command-line executable.
35    pub fn new(bin: impl Into<PathBuf>, global_opts: GlobalOpts) -> Self {
36        Self {
37            bin: bin.into(),
38            global_opts,
39        }
40    }
41
42    /// Get the Perforce command path.
43    pub fn get_bin(&self) -> &Path {
44        &self.bin
45    }
46
47    /// Set the Perforce command path.
48    pub fn set_bin(&mut self, bin: impl Into<PathBuf>) -> &mut Self {
49        self.bin = bin.into();
50        self
51    }
52
53    /// Set the Perforce command path.
54    pub fn bin(mut self, bin: impl Into<PathBuf>) -> Self {
55        self.set_bin(bin);
56        self
57    }
58
59    /// # Description
60    ///
61    /// g-opts
62    ///
63    #[cfg_attr(
64        feature = "lt2014_2",
65        doc = "See the [Global Options](GlobalOpts) section."
66    )]
67    #[cfg_attr(
68        all(feature = "lt2015_1", not(feature = "lt2014_2")),
69        doc = "See the [“Global Options”](GlobalOpts) section."
70    )]
71    #[cfg_attr(
72        all(feature = "lt2017_1", not(feature = "lt2015_1")),
73        doc = "See [“Global Options”](GlobalOpts)."
74    )]
75    #[cfg_attr(
76        all(feature = "lt2018_2", not(feature = "lt2017_1")),
77        doc = "See [Global Options](GlobalOpts)."
78    )]
79    #[cfg_attr(not(feature = "lt2018_2"), doc = "See [Global options](GlobalOpts).")]
80    pub fn get_global_opts(&self) -> &GlobalOpts {
81        &self.global_opts
82    }
83
84    /// # Description
85    ///
86    /// g-opts
87    ///
88    #[cfg_attr(
89        feature = "lt2014_2",
90        doc = "See the [Global Options](GlobalOpts) section."
91    )]
92    #[cfg_attr(
93        all(feature = "lt2015_1", not(feature = "lt2014_2")),
94        doc = "See the [“Global Options”](GlobalOpts) section."
95    )]
96    #[cfg_attr(
97        all(feature = "lt2017_1", not(feature = "lt2015_1")),
98        doc = "See [“Global Options”](GlobalOpts)."
99    )]
100    #[cfg_attr(
101        all(feature = "lt2018_2", not(feature = "lt2017_1")),
102        doc = "See [Global Options](GlobalOpts)."
103    )]
104    #[cfg_attr(not(feature = "lt2018_2"), doc = "See [Global options](GlobalOpts).")]
105    pub fn set_global_opts(&mut self, v: GlobalOpts) -> &mut Self {
106        self.global_opts = v;
107        self
108    }
109
110    /// # Description
111    ///
112    /// g-opts
113    ///
114    #[cfg_attr(
115        feature = "lt2014_2",
116        doc = "See the [Global Options](GlobalOpts) section."
117    )]
118    #[cfg_attr(
119        all(feature = "lt2015_1", not(feature = "lt2014_2")),
120        doc = "See the [“Global Options”](GlobalOpts) section."
121    )]
122    #[cfg_attr(
123        all(feature = "lt2017_1", not(feature = "lt2015_1")),
124        doc = "See [“Global Options”](GlobalOpts)."
125    )]
126    #[cfg_attr(
127        all(feature = "lt2018_2", not(feature = "lt2017_1")),
128        doc = "See [Global Options](GlobalOpts)."
129    )]
130    #[cfg_attr(not(feature = "lt2018_2"), doc = "See [Global options](GlobalOpts).")]
131    pub fn global_opts(mut self, v: GlobalOpts) -> Self {
132        self.global_opts = v;
133        self
134    }
135}
136
137impl SubCommand for CStat {
138    fn name(&self) -> &str {
139        "cstat"
140    }
141
142    fn inject_local_args(&self, _: &mut Command) {}
143
144    fn global_opts(&self) -> Option<&GlobalOpts> {
145        Some(&self.global_opts)
146    }
147}
148
149impl<S, I> ParameterizedSpawn<(S,)> for CStat
150where
151    S: IntoIterator<Item = I>,
152    I: AsRef<OsStr>,
153{
154    type Output = Child;
155    type Error = std::io::Error;
156
157    /// Spawns `p4 cstat` for the given files as a child process with piped
158    /// standard output and error streams; use the returned [`Child`] handle
159    /// to wait for it or interact with it.
160    fn spawn_with(&mut self, (files,): (S,)) -> Result<Self::Output, Self::Error> {
161        self.setup_command(&self.bin)
162            .args(files)
163            .stdout(Stdio::piped())
164            .stderr(Stdio::piped())
165            .spawn()
166    }
167}
168
169#[cfg(test)]
170mod tests {
171    use super::*;
172    use crate::cmd::args_of;
173
174    #[test]
175    fn bin_accessors_update_executable_path() {
176        let mut command = CStat::new("p4", GlobalOpts::new());
177        assert_eq!(command.get_bin(), Path::new("p4"));
178
179        command.set_bin("/usr/local/bin/p4");
180        assert_eq!(command.get_bin(), Path::new("/usr/local/bin/p4"));
181
182        let command = command.bin("p4.exe");
183        assert_eq!(command.get_bin(), Path::new("p4.exe"));
184    }
185
186    /// Dry-run checks of the assembled `p4 cstat` command line; no process is
187    /// spawned.
188    #[test]
189    fn without_files() {
190        let cstat = CStat::new("p4", GlobalOpts::new());
191
192        assert_eq!(args_of(&cstat.setup_command("p4")), ["cstat"]);
193    }
194
195    #[test]
196    fn global_opts_are_injected_once() {
197        let cstat = CStat::new(
198            "p4",
199            GlobalOpts::new().port("localhost:1666").quiet_mode(true),
200        );
201
202        assert_eq!(
203            args_of(&cstat.setup_command("p4")),
204            ["-p", "localhost:1666", "-q", "cstat"]
205        );
206    }
207}