use pathsearch::find_executable_in_path;
use subprocess::{Exec, Redirection};
pub fn format_rust(code: &str) -> String {
let Some(rustfmt_path) = find_executable_in_path("rustfmt") else {
println!("cargo:warning=rustfmt not found in PATH, skipping formatting");
return code.to_string();
};
let Ok(rustfmt_run) = Exec::cmd(rustfmt_path)
.args(&["--edition", "2021"])
.stdin(code)
.stdout(Redirection::Pipe)
.capture()
else {
println!("cargo:warning=rustfmt failed, wrong code?");
return code.to_string();
};
if !rustfmt_run.exit_status.success() {
println!("cargo:warning=rustfmt failed, wrong code?");
return code.to_string();
}
rustfmt_run.stdout_str()
}