• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use crate::workspace_path;
2 use anyhow::Result;
3 use proc_macro2::TokenStream;
4 use std::fs;
5 use std::io::Write;
6 use std::path::Path;
7 
write(relative_to_workspace_root: impl AsRef<Path>, content: TokenStream) -> Result<()>8 pub fn write(relative_to_workspace_root: impl AsRef<Path>, content: TokenStream) -> Result<()> {
9     let mut formatted = Vec::new();
10     writeln!(
11         formatted,
12         "// This file is @generated by syn-internal-codegen."
13     )?;
14     writeln!(formatted, "// It is not intended for manual editing.")?;
15     writeln!(formatted)?;
16 
17     let syntax_tree: syn::File = syn::parse2(content).unwrap();
18     let pretty = prettyplease::unparse(&syntax_tree);
19     write!(formatted, "{}", pretty)?;
20 
21     let path = workspace_path::get(relative_to_workspace_root);
22     if path.is_file() && fs::read(&path)? == formatted {
23         return Ok(());
24     }
25 
26     fs::write(path, formatted)?;
27     Ok(())
28 }
29