This post has been de-listed
It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.
I wrote this because I will need source code formatting for my upcoming Rust source code generator, flexgen (still in development). It has one very neat trick in that it can replace blank line and comment "marker macros" in the source with actual blank lines and comments which is very useful if you want your generated source code to be read by a human.
An example:
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);
}
I can't think of a lot of use cases for this other than generating source code, but in case someone is doing that I thought I would share (it does also serve as the foundation to my quote-doctest crate (which also just got a new 0.3 release) which could be very useful if someone is generating doctests either directly in source or via proc-macro).
UPDATE: FYI - version 0.3.4 is out and it adds white space insensitive matching of comment/blank markers. If you are using post-processing, 0.3.4 is a must have upgrade.
Post Details
- Posted
- 2 years ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/rust/commen...