#formatter #rustfmt #quote #format-string #file-input #format-file #prettyplease

rust-format

A Rust source code formatting crate with a unified interface for string, file, and TokenStream input

8 releases

0.3.4 Apr 11, 2022
0.3.3 Apr 10, 2022
0.2.1 Apr 6, 2022
0.1.0 Apr 4, 2022

#37 in Value formatting

Download history 26260/week @ 2024-07-24 25729/week @ 2024-07-31 29711/week @ 2024-08-07 29091/week @ 2024-08-14 27559/week @ 2024-08-21 21416/week @ 2024-08-28 25684/week @ 2024-09-04 21608/week @ 2024-09-11 25524/week @ 2024-09-18 22667/week @ 2024-09-25 25820/week @ 2024-10-02 20612/week @ 2024-10-09 26677/week @ 2024-10-16 22613/week @ 2024-10-23 23614/week @ 2024-10-30 28769/week @ 2024-11-06

106,348 downloads per month
Used in 130 crates (31 directly)

MIT/Apache

59KB
1.5K SLoC

rust-format

Crate Docs

A Rust source code formatting crate with a unified interface for string, file, and TokenStream input. It currently supports rustfmt and prettyplease.

It optionally supports post-processing replacement of special blank/comment markers for inserting blank lines and comments in TokenStream generated source code respectively (as used by quote-doctest for inserting blanks/comments in generated doctests). It additionally supports converting doc blocks (#[doc =""]) into doc comments (///).

NOTE: This is primarily to support rustfmt as prettyplease automatically converts doc blocks into doc comments (but for rustfmt it requires nightly and a configuration option).

Usage

[dependencies]
rust-format = "0.3"

Optional Features

  • post_process - enables support for post-process conversion of special "marker macros" into blank lines/comments. It additionally supports converting doc blocks (#[doc]) into doc comments (///)
  • pretty_please - enables prettyplease formatting support
  • token_stream - enables formatting from TokenStream input

Examples

Simple example using default options of RustFmt:

use rust_format::{Formatter, RustFmt};

fn main() {
    let source = r#"fn main() { println!("Hello World!"); }"#;

    let actual = RustFmt::default().format_str(source).unwrap();
    let expected = r#"fn main() {
    println!("Hello World!");
}
"#;

    assert_eq!(expected, actual);
}

Using a custom configuration:

use rust_format::{Config, Edition, Formatter, RustFmt};

fn main() {
    let source = r#"use std::marker; use std::io; mod test; mod impls;"#;
  
    let mut config = Config::new_str()
        .edition(Edition::Rust2018)
        .option("reorder_imports", "false")
        .option("reorder_modules", "false");
    let rustfmt = RustFmt::from_config(config);
  
    let actual = rustfmt.format_str(source).unwrap();
    let expected = r#"use std::marker;
use std::io;
mod test;
mod impls;
"#;
  
    assert_eq!(expected, actual);
}

RustFmt with post-processing:

use quote::quote;
use rust_format::{Config, Formatter, PostProcess, RustFmt};

fn main() {
    let source = quote! {
        #[doc = " This is main"] 
        fn main() { 
            _blank_!();
            _comment_!("\nThis prints hello world\n\n"); 
            println!("Hello World!"); 
        }
    };

    let mut config = Config::new_str()
        .post_proc(PostProcess::ReplaceMarkersAndDocBlocks);
    let actual = RustFmt::from_config(config).format_tokens(source).unwrap();
    let expected = r#"/// This is main
fn main() {

    //
    // This prints hello world
    //
    println!("Hello World!");
}
"#;

    assert_eq!(expected, actual);
}

License

This project is licensed optionally under either:

Dependencies

~0–440KB
~11K SLoC