blob: da7aabee8b3a22f21fc0789e2cc7cfc18e6f3327 [file] [log] [blame]
use crate::error::Result;
use proc_macro2::TokenStream;
use std::fs::File;
use std::io::{BufWriter, Write};
use std::path::Path;
pub fn write<P: AsRef<Path>>(path: P, content: TokenStream) -> Result<()> {
let mut file = File::create(path).map(BufWriter::new)?;
write!(
file,
"// THIS FILE IS AUTOMATICALLY GENERATED; DO NOT EDIT\n\n"
)?;
let mut config = rustfmt::Config::default();
config.set().emit_mode(rustfmt::EmitMode::Stdout);
config.set().verbose(rustfmt::Verbosity::Quiet);
config.set().format_macro_matchers(true);
config.set().normalize_doc_attributes(true);
let mut session = rustfmt::Session::new(config, Some(&mut file));
session.format(rustfmt::Input::Text(content.to_string()))?;
Ok(())
}