use active_core::fmt::format;
use active_core::{parse_file, ParseOptions, Style};
use clap::Parser;
use std::fs;
use std::path::PathBuf;
#[derive(Parser)]
#[command(name = "active-fmt", about = "Format Active files")]
struct Cli {
file: PathBuf,
#[arg(long, conflicts_with = "indented")]
bracketed: bool,
#[arg(long, conflicts_with = "bracketed")]
indented: bool,
#[arg(short, long)]
write: bool,
#[arg(long, conflicts_with = "write")]
check: bool,
}
fn main() -> anyhow::Result<()> {
let cli = Cli::parse();
let name = cli.file.to_string_lossy().to_string();
let style = match (cli.bracketed, cli.indented) {
(true, _) => Style::Brace,
_ => Style::Indent,
};
let options = ParseOptions {
resolve_includes: false,
..ParseOptions::default()
};
let module = parse_file(&cli.file, options).map_err(|diags| {
for d in &diags {
match &d.span {
Some(s) => eprintln!(
"{}:{}:{}: [Error] {}: {}",
name, s.line, s.column, d.code, d.message
),
None => eprintln!("[Error] {}: {}", d.code, d.message),
}
}
anyhow::anyhow!("cannot format a file with parse errors")
})?;
let out = format(&module, style);
if cli.check {
let current = fs::read_to_string(&cli.file)?;
if current != out {
println!("would reformat: {}", name);
std::process::exit(1);
}
return Ok(());
}
if cli.write {
fs::write(&cli.file, &out)?;
println!("formatted {}", cli.file.display());
} else {
print!("{out}");
}
Ok(())
}