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

toml [dependencies] rust-format = "0.3"

Optional Features

Examples

Simple example using default options of RustFmt:

```rust 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:

```rust 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::newstr() .edition(Edition::Rust2018) .option("reorderimports", "false") .option("reordermodules", "false"); let rustfmt = RustFmt::fromconfig(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:

```rust use rust_format::{Config, Formatter, PostProcess, RustFmt};

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

let mut config = Config::newstr() .postproc(PostProcess::ReplaceMarkersAndDocBlocks); let actual = RustFmt::fromconfig(config).formatstr(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: