ferriorm_codegen/formatter.rs
1//! Source code formatting for generated Rust code.
2//!
3//! Uses `syn` to parse the token stream and `prettyplease` to pretty-print it.
4//! This ensures that all generated files are human-readable and consistently
5//! formatted.
6
7/// Format a token stream into a pretty-printed Rust source string.
8pub fn format_token_stream(tokens: proc_macro2::TokenStream) -> String {
9 match syn::parse2::<syn::File>(tokens.clone()) {
10 Ok(file) => prettyplease::unparse(&file),
11 Err(e) => {
12 // On parse failure, output the raw tokens for debugging
13 eprintln!("Code generation syntax error: {e}");
14 eprintln!("Raw tokens:\n{tokens}");
15 panic!("generated code should be valid syntax: {e}");
16 }
17 }
18}