pub fn rustfmt_str(source: &str) -> Option<String> {
let source = rustfmt_once(source)?;
rustfmt_once(&source)
}
fn rustfmt_once(source: &str) -> Option<String> {
use std::io::Write as _;
use std::process::Stdio;
let rust_fmt = std::env::var_os("RUSTFMT")
.map(|s| s.display().to_string())
.unwrap_or_else(|| String::from("rustfmt"));
let mut proc = std::process::Command::new(&rust_fmt)
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.arg("--edition=2024")
.spawn()
.ok()?;
let mut stdin = proc.stdin.take()?;
stdin.write_all(source.as_bytes()).ok()?;
drop(stdin);
let output = proc.wait_with_output().ok()?;
if output.status.success() {
String::from_utf8(output.stdout).ok()
} else {
None
}
}