prettyplease

A minimal syn syntax tree pretty-printer for formatting generated Rust code.

Library
Cargo
v0.3.0
774stars
MIT OR Apache-2.0

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
64/100Good
Development Activity52
Maintenance68
Community40
Maturity56
Momentum40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
86/100Excellent
Architecture88
Code Quality88
Innovation78
Learning Curve90

prettyplease is a minimal pretty-printer that turns a syn syntax tree into a String of well-formatted Rust source code. Unlike rustfmt, which prioritizes impeccable output using heavyweight algorithms that can bail out on hard-to-format code, prettyplease is designed to be robust and suitable for arbitrary generated code.

Its single-purpose unparse function makes it the go-to formatting layer for procedural macros, code generators, and build scripts that emit Rust and want readable output without shelling out to an external rustfmt binary.

What You Get

  • A single unparse entry point that formats a syn::File into a String
  • Robust formatting that does not bail out on unusual generated code
  • In-process operation with no dependency on an external rustfmt binary
  • Deterministic, readable output well suited to code generators and macros

Common Use Cases

  • Formatting Rust source emitted by procedural macros or build scripts
  • Producing readable output from code generators and bindings tools
  • Formatting generated code where invoking rustfmt is impractical

Under The Hood

Architecture - The crate mirrors syn’s node taxonomy with one module per construct — expr.rs, item.rs, stmt.rs, pat.rs, ty.rs, generics.rs, attr.rs, lit.rs, mac.rs, path.rs and more — each knowing how to print its node. At the core, algorithm.rs and ring.rs implement the classic Oppen-style pretty-printing algorithm (a ring buffer of tokens with break/box logic), while convenience.rs, fixup.rs, classify.rs, and precedence.rs handle spacing, parenthesization, and correctness details. lib.rs exposes the top-level unparse.

Tech Stack - Rust built with Cargo. Its primary dependencies are syn (for the syntax tree it consumes) and proc-macro2; the printing algorithm itself is dependency-light and self-contained.

Code Quality - The implementation is carefully structured and battle-tested through extremely wide ecosystem usage (hundreds of millions of downloads). Modules like fixup.rs and precedence.rs show attention to subtle correctness (operator precedence, needed parentheses), and the code adheres to dtolnay’s consistently high crate conventions.

API Design - The public API is a single unparse(&syn::File) -> String, which is about as minimal and discoverable as possible. There is no configuration to learn and no boilerplate; anyone with a syn tree can format it in one line.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search