libgir/
fmt.rs

1use std::{path::Path, process::Command};
2
3use log::warn;
4
5/// Check if `cargo fmt` available
6pub fn check_fmt() -> bool {
7    let output = Command::new("cargo").arg("fmt").arg("--version").output();
8    if let Ok(output) = output {
9        output.status.success()
10    } else {
11        false
12    }
13}
14
15/// Run `cargo fmt` on path
16pub fn format(path: &Path) {
17    let output = Command::new("cargo").arg("fmt").current_dir(path).output();
18    match output {
19        Ok(output) if output.status.success() => {}
20        Ok(output) => {
21            warn!(
22                "Failed to format {}:\n{}\n{}",
23                path.display(),
24                String::from_utf8_lossy(&output.stdout),
25                String::from_utf8_lossy(&output.stderr)
26            );
27        }
28        Err(_) => { /*We checked `cargo` fmt presence in check_fmt, so can ignore errors*/ }
29    }
30}