rustabstract-syntax-treerustfmt

How to pretty print Syn AST?


I'm trying to use syn to create an AST from a Rust file and then using quote to write it to another. However, when I write it, it puts extra spaces between everything.

Note that the example below is just to demonstrate the minimum reproducible problem I'm having. I realize that if I just wanted to copy the code over I could copy the file but it doesn't fit my case and I need to use an AST.

pub fn build_file() {
    let current_dir = std::env::current_dir().expect("Unable to get current directory");
    let rust_file = std::fs::read_to_string(current_dir.join("src").join("lib.rs")).expect("Unable to read rust file");
    let ast = syn::parse_file(&rust_file).expect("Unable to create AST from rust file");

    match std::fs::write("src/utils.rs", quote::quote!(#ast).to_string());
}

The file that it creates an AST of is this:

#[macro_use]
extern crate foo;
mod test;
fn init(handle: foo::InitHandle) {
    handle.add_class::<Test::test>();
}

What it outputs is this:

# [macro_use] extern crate foo ; mod test ; fn init (handle : foo :: InitHandle) { handle . add_class :: < Test :: test > () ; }

I've even tried running it through rustfmt after writing it to the file like so:

utils::write_file("src/utils.rs", quote::quote!(#ast).to_string());

match std::process::Command::new("cargo").arg("fmt").output() {
    Ok(_v) => (),
    Err(e) => std::process::exit(1),
}

But it doesn't seem to make any difference.


Solution

  • The quote crate is not really concerned with pretty printing the generated code. You can run it through rustfmt, you just have to execute rustfmt src/utils.rs or cargo fmt -- src/utils.rs.

    use std::fs;
    use std::io;
    use std::path::Path;
    use std::process::Command;
    
    fn write_and_fmt<P: AsRef<Path>, S: ToString>(path: P, code: S) -> io::Result<()> {
        fs::write(&path, code.to_string())?;
    
        Command::new("rustfmt")
            .arg(path.as_ref())
            .spawn()?
            .wait()?;
    
        Ok(())
    }
    

    Now you can just execute:

    write_and_fmt("src/utils.rs", quote::quote!(#ast)).expect("unable to save or format");
    

    See also "Any interest in a pretty-printing crate for Syn?" on the Rust forum.