checked_command/
wrapper.rs1use std::ffi::OsStr;
2use std::io::Error as IoError;
3use std::path::Path;
4use std::process;
5use std::process::Stdio;
6use std::process::{Child, Command};
7
8use ext::{ChildExt, CommandExt, Error, Output};
9
10#[derive(Debug)]
14pub struct CheckedChild {
15 child: Child,
16}
17
18impl From<Child> for CheckedChild {
19 fn from(child: Child) -> CheckedChild {
20 CheckedChild { child }
21 }
22}
23
24impl CheckedChild {
25 pub fn as_std_command(&mut self) -> &mut Child {
27 &mut self.child
28 }
29
30 pub fn into_std_command(self) -> Child {
33 self.child
34 }
35
36 pub fn stdin(&mut self) -> &mut Option<process::ChildStdin> {
38 &mut self.child.stdin
39 }
40
41 pub fn stdout(&mut self) -> &mut Option<process::ChildStdout> {
43 &mut self.child.stdout
44 }
45
46 pub fn stderr(&mut self) -> &mut Option<process::ChildStderr> {
48 &mut self.child.stderr
49 }
50
51 pub fn kill(&mut self) -> Result<(), IoError> {
53 self.child.kill()
54 }
55
56 pub fn id(&self) -> u32 {
58 self.child.id()
59 }
60
61 pub fn wait(&mut self) -> Result<(), Error> {
63 self.child.checked_wait()
64 }
65
66 #[cfg(feature = "process_try_wait")]
68 pub fn try_wait(&mut self) -> Result<bool, Error> {
69 self.child.checked_try_wait()
70 }
71
72 pub fn wait_with_output(self) -> Result<Output, Error> {
74 self.child.checked_wait_with_output()
75 }
76}
77
78#[derive(Debug)]
82pub struct CheckedCommand {
83 command: Command,
84}
85
86impl From<Command> for CheckedCommand {
87 fn from(command: Command) -> CheckedCommand {
88 CheckedCommand { command }
89 }
90}
91
92impl CheckedCommand {
93 pub fn as_std_command(&mut self) -> &mut Command {
97 &mut self.command
98 }
99
100 pub fn into_std_command(self) -> Command {
112 self.command
113 }
114
115 pub fn new<S: AsRef<OsStr>>(program: S) -> CheckedCommand {
118 CheckedCommand {
119 command: Command::new(program),
120 }
121 }
122
123 pub fn arg<S: AsRef<OsStr>>(&mut self, arg: S) -> &mut CheckedCommand {
125 self.command.arg(arg);
126 self
127 }
128
129 pub fn args<I, S>(&mut self, args: I) -> &mut CheckedCommand
131 where
132 I: IntoIterator<Item = S>,
133 S: AsRef<OsStr>,
134 {
135 self.command.args(args);
136 self
137 }
138
139 pub fn env<K, V>(&mut self, key: K, val: V) -> &mut CheckedCommand
141 where
142 K: AsRef<OsStr>,
143 V: AsRef<OsStr>,
144 {
145 self.command.env(key, val);
146 self
147 }
148
149 #[cfg(feature = "command_envs")]
151 pub fn envs<I, K, V>(&mut self, vars: I) -> &mut CheckedCommand
152 where
153 I: IntoIterator<Item = (K, V)>,
154 K: AsRef<OsStr>,
155 V: AsRef<OsStr>,
156 {
157 self.command.envs(vars);
158 self
159 }
160
161 pub fn env_remove<K: AsRef<OsStr>>(&mut self, key: K) -> &mut CheckedCommand {
163 self.command.env_remove(key);
164 self
165 }
166
167 pub fn env_clear(&mut self) -> &mut CheckedCommand {
169 self.command.env_clear();
170 self
171 }
172
173 pub fn current_dir<P: AsRef<Path>>(&mut self, dir: P) -> &mut CheckedCommand {
175 self.command.current_dir(dir);
176 self
177 }
178
179 pub fn stdin(&mut self, cfg: Stdio) -> &mut CheckedCommand {
181 self.command.stdin(cfg);
182 self
183 }
184
185 pub fn stdout(&mut self, cfg: Stdio) -> &mut CheckedCommand {
187 self.command.stdout(cfg);
188 self
189 }
190
191 pub fn stderr(&mut self, cfg: Stdio) -> &mut CheckedCommand {
193 self.command.stderr(cfg);
194 self
195 }
196
197 pub fn spawn(&mut self) -> Result<CheckedChild, IoError> {
200 self.command.spawn().map(Into::into)
201 }
202
203 pub fn output(&mut self) -> Result<Output, Error> {
205 self.command.checked_output()
206 }
207
208 pub fn status(&mut self) -> Result<(), Error> {
210 self.command.checked_status()
211 }
212}