#[cfg(test)]
mod tests;
use crate::Error;
use std::{path::Path, process::Command};
const EXPECT_MSG: &str = "If cargo fmt were to fail with an IO error, it would have already failed with 'cargo +nightly fmt --all'; qed;";
pub fn format_dir<P: AsRef<Path>>(path: P) -> Result<(), Error> {
fn do_format_dir(path: &Path) -> Result<(), Error> {
Command::new("cargo")
.arg("+nightly")
.arg("fmt")
.arg("--all")
.current_dir(path)
.output()
.map(|output| {
if output.status.success() {
output
} else {
Command::new("cargo")
.arg("fmt")
.arg("--all")
.current_dir(path)
.output()
.expect(EXPECT_MSG)
}
})
.map_or_else(
|err| Err(err.into()),
|output| {
if output.status.success() {
Ok(())
} else {
Err(Error::Descriptive(
String::from_utf8_lossy(&output.stderr).into_owned(),
))
}
},
)
}
do_format_dir(path.as_ref())
}