1use std::ffi::OsStr;
2use std::ffi::OsString;
3use std::path::Path;
4
5use tracing::trace;
6
7use crate::Cmd;
8
9pub trait CommandDebug {
10 fn debug(&mut self) -> &mut Self;
11 fn as_string(&self) -> String;
12}
13
14impl CommandDebug for std::process::Command {
15 fn debug(&mut self) -> &mut Self {
16 trace!("Executing `{}`...", self.as_string());
17 self
18 }
19
20 fn as_string(&self) -> String {
21 let path = Path::new(self.get_program());
22 let s = self.get_args().fold(vec![], |mut a: Vec<&OsStr>, b: &OsStr| {
23 a.push(b);
24 a
25 });
26 format!(
27 "{} {}",
28 path.file_name().unwrap().to_str().unwrap(),
29 s.join(OsString::from(" ").as_os_str()).to_str().unwrap().trim()
30 )
31 }
32}
33
34impl CommandDebug for Cmd {
35 fn debug(&mut self) -> &mut Self {
36 trace!("Executing `{}`...", self.as_string());
37 self
38 }
39
40 fn as_string(&self) -> String {
41 let path = Path::new(self.program.as_os_str());
42 let s = (&self.args)
43 .into_iter()
44 .fold(Vec::new(), |mut a: Vec<OsString>, b: &OsString| {
45 a.push(b.clone());
46 a
47 });
48 format!(
49 "{} {}",
50 path.file_name().unwrap().to_str().unwrap(),
51 s.join(OsString::from(" ").as_os_str()).to_str().unwrap().trim()
52 )
53 }
54}