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
use std::ffi::OsStr;
use std::ffi::OsString;
use std::path::Path;

use tracing::trace;

use crate::Cmd;

pub trait CommandDebug {
	fn debug(&mut self) -> &mut Self;
	fn as_string(&self) -> String;
}

impl CommandDebug for std::process::Command {
	fn debug(&mut self) -> &mut Self {
		trace!("Executing `{}`...", self.as_string());
		self
	}

	fn as_string(&self) -> String {
		let path = Path::new(self.get_program());
		let s = self.get_args().fold(vec![], |mut a: Vec<&OsStr>, b: &OsStr| {
			a.push(b);
			a
		});
		format!(
			"{} {}",
			path.file_name().unwrap().to_str().unwrap(),
			s.join(OsString::from(" ").as_os_str()).to_str().unwrap().trim()
		)
	}
}

impl CommandDebug for Cmd {
	fn debug(&mut self) -> &mut Self {
		trace!("Executing `{}`...", self.as_string());
		self
	}

	fn as_string(&self) -> String {
		let path = Path::new(self.program.as_os_str());
		let s = (&self.args)
			.into_iter()
			.fold(Vec::new(), |mut a: Vec<OsString>, b: &OsString| {
				a.push(b.clone());
				a
			});
		format!(
			"{} {}",
			path.file_name().unwrap().to_str().unwrap(),
			s.join(OsString::from(" ").as_os_str()).to_str().unwrap().trim()
		)
	}
}