1
0
Fork 0
mirror of https://codeberg.org/demostf/parser.git synced 2026-06-04 02:24:12 +02:00

prettyplease

This commit is contained in:
Robin Appelman 2022-01-13 21:55:33 +01:00
commit 1118bd97d9
3 changed files with 238 additions and 383 deletions

View file

@ -1,44 +1,12 @@
use crate::gameevent::generate_game_events;
use prettyplease::unparse;
use std::env;
use std::fs;
use std::io;
use std::path::PathBuf;
use std::process::Command;
use syn::{parse2, File};
use tf_demo_parser::Demo;
mod gameevent;
fn which_rustfmt() -> Option<PathBuf> {
match env::var_os("RUSTFMT") {
Some(which) => {
if which.is_empty() {
None
} else {
Some(PathBuf::from(which))
}
}
None => toolchain_find::find_installed_component("rustfmt"),
}
}
fn format(code: &str) -> io::Result<String> {
let mut builder = tempfile::Builder::new();
builder.prefix("rustfmtr");
let outdir = builder.tempdir()?;
let outfile_path = outdir.path().join("code");
fs::write(&outfile_path, code)?;
let rustfmt = which_rustfmt().ok_or(io::Error::from(io::ErrorKind::NotFound))?;
let _status = Command::new(rustfmt)
.arg("--edition=2018")
.arg(&outfile_path)
.status();
fs::read_to_string(&outfile_path)
}
fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
better_panic::install();
@ -51,8 +19,8 @@ fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
let file = fs::read(path).expect("Unable to read file");
let demo = Demo::new(&file);
let tokens = generate_game_events(demo);
let code = tokens.to_string();
let formatted = format(&code)?;
println!("{}", formatted);
let file = parse2::<File>(tokens)?;
let code = unparse(&file);
println!("{}", code);
Ok(())
}