From 4f74037f4122f2eab6786106c8abda7540ee7a36 Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Sun, 5 May 2024 10:11:21 -0400 Subject: [PATCH] pipe debug output through rustfmt is there a nicer way to do this?! --- components/salsa-2022-macros/src/debug.rs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/components/salsa-2022-macros/src/debug.rs b/components/salsa-2022-macros/src/debug.rs index ed9e2a8b..22ba8561 100644 --- a/components/salsa-2022-macros/src/debug.rs +++ b/components/salsa-2022-macros/src/debug.rs @@ -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