You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
aya/aya-tool/src/rustfmt.rs

26 lines
691 B
Rust

use std::{
io::{self, Write},
process::{Command, Stdio},
};
pub fn format(code: &str) -> Result<String, io::Error> {
let mut child = Command::new("rustfmt")
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.spawn()?;
let stdin = child.stdin.as_mut().unwrap();
stdin.write_all(code.as_bytes())?;
let output = child.wait_with_output()?;
if !output.status.success() {
return Err(io::Error::new(
io::ErrorKind::Other,
format!(
"rustfmt failed with exit code: {}",
output.status.code().unwrap()
),
));
}
Ok(String::from_utf8(output.stdout).unwrap())
}