Skip to content

Instantly share code, notes, and snippets.

@hnorkowski
Last active July 17, 2024 16:20
Show Gist options
  • Save hnorkowski/2c30bc4ecb05fd4e26b6f07e6a0ba8ae to your computer and use it in GitHub Desktop.
Save hnorkowski/2c30bc4ecb05fd4e26b6f07e6a0ba8ae to your computer and use it in GitHub Desktop.
Rust Proc Macro Programming Notes

Useful crates for creating macros:

Crate Usage
prettyplease print token stream as rust code

print token stream as rust code

Uses bat::PrettyPinter and prettyplease::unparse

fn pretty_print(token_stream: &proc_macro2::TokenStream) {
    let token_stream = token_stream.to_string();
    let string = match syn::parse_file(&token_stream) {
        Ok(file) => prettyplease::unparse(&file),
        Err(error) => {
            format! {"Error parsing tokenstream: {error:?}\n\n{token_stream }\n\n"}
        }
    };

    bat::PrettyPrinter::new()
        .input_from_bytes(string.as_bytes())
        .language("Rust")
        .print()
        .unwrap();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment