#rustfmt #generator #rustfmt-wrapper

rustfmt-wrapper

Library wrapper around rustfmt for use by code generators

3 unstable releases

0.2.1 Oct 23, 2023
0.2.0 Jun 21, 2022
0.1.0 Oct 9, 2021

#52 in Procedural macros

Download history 24039/week @ 2024-12-16 1757/week @ 2024-12-23 3991/week @ 2024-12-30 29463/week @ 2025-01-06 28104/week @ 2025-01-13 15295/week @ 2025-01-20 19928/week @ 2025-01-27 23550/week @ 2025-02-03 24434/week @ 2025-02-10 22766/week @ 2025-02-17 36453/week @ 2025-02-24 30028/week @ 2025-03-03 33837/week @ 2025-03-10 32843/week @ 2025-03-17 28408/week @ 2025-03-24 29241/week @ 2025-03-31

124,588 downloads per month
Used in 50 crates (26 directly)

Apache-2.0

21KB
332 lines

rustfmt-wrapper

Rust makes it easy to generate more Rust code--for macros, builders, whatever. It's nice to format that code to be a little easier on the eyes. While rustfmt does a pretty good job, it does a pretty good job as a command and isn't usable as a library. This is a probably-too-simple wrapper library.

It's pretty simple to use and mixes well with quote!:

let codegen = quote::quote!{ struct Foo { bar: String } };
let formatted: String = rustfmt_wrapper::rustfmt(codegen).unwrap();

If you need more control over the vast array of rustfmt configuration options, you can use the second form:

let codegen = quote::quote!{
    async fn go() {
        let _ = Client::new().operation_id().send().await?;
    }
};
let config = Config {
    max_width: Some(45),
    ..Default::default()
};

let narrow_formatted = rustfmt_config(config, codegen).unwrap();

Note that in order to use unstable configuration options, you will need to have a the nightly version of rustfmt installed.


Thanks to David Tolnay for so many tools including cargo-expand from which this borrows.

Dependencies

~5–15MB
~211K SLoC