use std::fs;
use std::io::{self, Write as _};
use std::path::PathBuf;
use structopt::StructOpt;
use wasmfmt::{fmt, Diff, Error, Options};
#[derive(StructOpt)]
enum Cli {
Fix(Input),
Check(Input),
Print(Input),
}
#[derive(StructOpt)]
struct Input {
#[structopt(parse(from_os_str))]
file: PathBuf,
#[structopt(flatten)]
flags: Flags,
}
#[derive(StructOpt, Debug)]
struct Flags {
#[structopt(short, long)]
resolve_names: bool,
}
enum Command {
Fix,
Check,
Print,
}
impl From<Cli> for (Command, Input) {
fn from(cli: Cli) -> (Command, Input) {
match cli {
Cli::Print(input) => (Command::Print, input),
Cli::Fix(input) => (Command::Fix, input),
Cli::Check(input) => (Command::Check, input),
}
}
}
fn main() -> Result<(), Error> {
let (command, input) = Cli::from_args().into();
let source = fs::read_to_string(input.file.as_path())?;
let formatted = fmt(
&source,
Options {
resolve_names: input.flags.resolve_names,
},
);
match command {
Command::Fix => {
fs::write(input.file, formatted)?;
Ok(())
}
Command::Check => {
if let Some(diff) = Diff::from(&source, &formatted) {
io::stdout().write_fmt(format_args!("{}", diff))?;
io::stdout().flush().unwrap();
std::process::exit(1);
};
Ok(())
}
Command::Print => {
io::stdout().write_all(formatted.as_bytes())?;
Ok(())
}
}
}