Experimental Macro Formatter: Simple string ↔ tokentree ↔ string
⚓ Rust 📅 2025-04-26 👤 surdeus 👁️ 14https://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:
-
Locates the positions of
fmtbeginandfmtendcomments in the code -
Parses the lines between these comments
-
Converts them into syn token structures
-
Uses a custom
to_stringmethod to convert the token structures back to strings -
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.
2 posts - 1 participant
🏷️ rust_feed