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
55
56
57
58
59
60
#![cfg(not(target_arch = "wasm32"))]
use crate::{Error, Result};
pub struct RustFmtCommand<'cmd> {
program: &'cmd str,
edition: &'cmd str,
extra: Vec<&'cmd str>,
}
impl<'cmd> Default for RustFmtCommand<'cmd> {
fn default() -> Self {
RustFmtCommand {
program: "rustfmt",
edition: "2018",
extra: Vec::new(),
}
}
}
impl<'cmd> RustFmtCommand<'cmd> {
pub fn execute(&self, source_files: Vec<std::path::PathBuf>) -> Result<()> {
if !matches!(self.edition, "2015" | "2018" | "2021") {
return Err(Error::Rustfmt(format!("invalid edition: {}", self.edition)));
}
let missing = source_files
.iter()
.filter(|p| !p.is_file())
.map(|p| p.to_string_lossy().into_owned())
.collect::<Vec<String>>();
if !missing.is_empty() {
return Err(Error::Rustfmt(format!(
"missing source file(s) '{}'",
missing.join(",")
)));
}
let source_paths: Vec<std::borrow::Cow<'_, str>> =
source_files.iter().map(|p| p.to_string_lossy()).collect();
let mut args = vec!["--edition", self.edition];
args.extend(self.extra.iter());
args.extend(source_paths.iter().map(|p| p.as_ref()));
let mut child = std::process::Command::new(self.program)
.args(&args)
.spawn()
.map_err(|e| Error::Rustfmt(format!("failed to start: {}", e.to_string())))?;
let code = child.wait().map_err(|e| {
Error::Rustfmt(format!("failed waiting for rustfmt: {}", e.to_string()))
})?;
if !code.success() {
return Err(Error::Rustfmt(code.to_string()));
}
Ok(())
}
}