#rustfmt

rustfmt-wrapper

Library wrapper around rustfmt for use by code generators

2 unstable releases

0.2.0 Jun 21, 2022
0.1.0 Oct 9, 2021

#213 in Procedural macros

Download history 7237/week @ 2023-02-09 6512/week @ 2023-02-16 7769/week @ 2023-02-23 7607/week @ 2023-03-02 10949/week @ 2023-03-09 10317/week @ 2023-03-16 9530/week @ 2023-03-23 7060/week @ 2023-03-30 5487/week @ 2023-04-06 5281/week @ 2023-04-13 5391/week @ 2023-04-20 5597/week @ 2023-04-27 5436/week @ 2023-05-04 4215/week @ 2023-05-11 4119/week @ 2023-05-18 4235/week @ 2023-05-25

19,259 downloads per month
Used in 23 crates (15 directly)

Apache-2.0

20KB
329 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

~3.5–9.5MB
~188K SLoC