pipe debug output through rustfmt

is there a nicer way to do this?!
This commit is contained in:
Niko Matsakis 2024-05-05 10:11:21 -04:00
parent 04e041b4a2
commit 4f74037f41

View file

@ -1,3 +1,5 @@
use std::io::Write;
use std::process::{Command, Stdio};
use std::sync::OnceLock;
use proc_macro2::TokenStream;
@ -16,7 +18,22 @@ pub(crate) fn debug_enabled(input_name: impl ToString) -> bool {
pub(crate) fn dump_tokens(input_name: impl ToString, tokens: TokenStream) -> TokenStream {
if debug_enabled(input_name) {
eprintln!("{}", tokens);
let token_string = tokens.to_string();
let _: Result<(), ()> = Command::new("rustfmt")
.arg("--emit=stdout")
.stdin(Stdio::piped())
.spawn()
.and_then(|mut rustfmt| {
rustfmt
.stdin
.take()
.unwrap()
.write_all(token_string.as_bytes())?;
rustfmt.wait_with_output()
})
.and_then(|output| Ok(eprintln!("{}", String::from_utf8_lossy(&output.stdout))))
.or_else(|_| Ok(eprintln!("{token_string}")));
}
tokens