ISLE: run rustfmt on generated code
This commit is contained in:
@@ -218,9 +218,44 @@ fn rebuild_isle(crate_dir: &std::path::Path) -> Result<(), Box<dyn std::error::E
|
||||
impl std::error::Error for DebugReport {}
|
||||
})?;
|
||||
|
||||
let code = rustfmt(&code).unwrap_or_else(|e| {
|
||||
println!(
|
||||
"cargo:warning=Failed to run `rustfmt` on ISLE-generated code: {:?}",
|
||||
e
|
||||
);
|
||||
code
|
||||
});
|
||||
|
||||
println!("Writing ISLE-generated Rust code to {}", out_file.display());
|
||||
std::fs::write(out_file, code)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
return Ok(());
|
||||
|
||||
fn rustfmt(code: &str) -> std::io::Result<String> {
|
||||
use std::io::Write;
|
||||
|
||||
let mut rustfmt = std::process::Command::new("rustfmt")
|
||||
.stdin(std::process::Stdio::piped())
|
||||
.stdout(std::process::Stdio::piped())
|
||||
.spawn()?;
|
||||
|
||||
let mut stdin = rustfmt.stdin.take().unwrap();
|
||||
stdin.write_all(code.as_bytes())?;
|
||||
drop(stdin);
|
||||
|
||||
let mut stdout = rustfmt.stdout.take().unwrap();
|
||||
let mut data = vec![];
|
||||
stdout.read_to_end(&mut data)?;
|
||||
|
||||
let status = rustfmt.wait()?;
|
||||
if !status.success() {
|
||||
return Err(std::io::Error::new(
|
||||
std::io::ErrorKind::Other,
|
||||
format!("`rustfmt` exited with status {}", status),
|
||||
));
|
||||
}
|
||||
|
||||
Ok(String::from_utf8(data).expect("rustfmt always writs utf-8 to stdout"))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user