Experimental Macro Formatter: Simple string ↔ tokentree ↔ string

https://github.com/kouhe3/quickmacrofmt

As we all know, rustfmt skips formatting macros containing custom syntax. Here's an experimental solution I've developed:

In this example, the to_hash_map macro accepts K:V pairs as input tokens and converts them into a HashMap structure. Since rustfmt doesn't format K:V pairs, I created a fmt.rs binary program. This program:

  1. Locates the positions of fmtbegin and fmtend comments in the code

  2. Parses the lines between these comments

  3. Converts them into syn token structures

  4. Uses a custom to_string method to convert the token structures back to strings

  5. Inserts the formatted result into the source code at the comment locations

This approach maintains code readability while working around rustfmt's limitations with custom macro syntax.

A better approach is to run as a HOOK after cargo fmt. There is still much to do, such as: hard - coded file paths, the need to identify which syn token struct a macro comes from, and each token struct must implement a to_string method to be formatted in this way. I know this method is inefficient; I'm just a Rust beginner. It is hoped that this can inspire everyone to provide better methods.