Expand description
Format generated Rust source: pretty-print the AST with prettyplease,
then refine through rustfmt.
The quote-based emitters build a proc_macro2::TokenStream and render it
with TokenStream::to_string(), which is valid but unformatted Rust (no
line breaks, :: / . spacing). rustfmt alone is not enough. When a
line exceeds max_width and cannot be broken, for example the long
fully-qualified paths in to_owned such as
crate::owned::common::<msg>::<struct>::TypeName, rustfmt gives up on
that item and emits it verbatim. That leaves the raw quote! ::
spacing.
prettyplease pretty-prints the syn AST and always renders canonical,
correctly-spaced source whatever the line length, so this module runs it
first. The rustfmt pass then applies the project’s canonical style to
everything it can fit, and leaves the unbreakable long lines clean, as
prettyplease rendered them. Both passes also work as validation. Malformed
generated code fails to parse or exits non-zero here, so codegen bugs
surface at regeneration time rather than three crates downstream.
Between parsing and pretty-printing, hoist::hoist_absolute_paths
rewrites the long crate-rooted paths the emitters write into module-scope
use items, so the committed code passes clippy::absolute_paths.
Enums§
Functions§
- prettyprint
- The first pass of
rustfmt: hoist long paths into imports and pretty-printsrcwith prettyplease, keeping its leading banner. - rustfmt
- Hoist long paths into imports, pretty-print
srcwith prettyplease, refine it throughrustfmtat edition 2024, and return the formatted output.
Type Aliases§
- Formatter
- A pass that turns generated source into the text the generator writes:
rustfmtfor the committed tree, or one built onprettyprintwhere no rustfmt binary is available.