Coming soon - Get a detailed view of why an account is flagged as spam!
view details

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.

7
rust-format: unified interface to `rustfmt` and `prettyplease`
Post Body

Github | Crates.io | Docs.rs

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.

Author
Account Strength
90%
Account Age
12 years
Verified Email
Yes
Verified Flair
No
Total Karma
2,263
Link Karma
352
Comment Karma
1,891
Profile updated: 17 hours ago
Posts updated: 1 year ago

Subreddit

Post Details

We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
2 years ago