Crates.io | rust-format |
lib.rs | rust-format |
version | 0.3.4 |
source | src |
created_at | 2022-04-04 21:23:31.200246 |
updated_at | 2022-04-11 22:48:27.315154 |
description | A Rust source code formatting crate with a unified interface for string, file, and TokenStream input |
homepage | |
repository | https://github.com/nu11ptr/flexgen/tree/master/rust_format |
max_upload_size | |
id | 562315 |
size | 58,018 |
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).
[dependencies]
rust-format = "0.3"
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 supporttoken_stream
- enables formatting from
TokenStream
inputSimple 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);
}
This project is licensed optionally under either: