简体   繁体   中英

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.

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.

Please see the new prettyplease crate. Advantages:

  1. It can be used directly as a library.
  2. It can handle code fragments while rustfmt only handles full files.
  3. It is fast because it uses a simpler algorithm.

As Martin mentioned in his answer, prettyplease can be used to format code fragments, which can be quite useful when testing proc macro where the standard to_string() on proc_macro2::TokenStream is rather hard to read.

Here a code sample to pretty print a proc_macro2::TokenStream parsable as a syn::Item :

fn pretty_print_item(item: proc_macro2::TokenStream) -> String {
    let item = syn::parse2(item).unwrap();
    let file = syn::File {
        attrs: vec![],
        items: vec![item],
        shebang: None,
    };

    prettyplease::unparse(&file)
}

I used this in my tests to help me understand where is the wrong generated code:

assert_eq!(
    expected.to_string(),
    generate_event().to_string(),
    "\n\nActual:\n {}",
    pretty_print_item(generate_event())
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM