Warning
This post was published 117 days ago. The information described in this article may have changed.
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:
Locates the positions of fmtbegin
and fmtend
comments in the code
Parses the lines between these comments
Converts them into syn token structures
Uses a custom to_string
method 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