use anyhow::Result;
use candid_parser::bindings::{javascript, typescript};
use candid_parser::pretty;
use candid_parser::pretty_check_file;
use clap::Parser;
use std::fs::{create_dir_all, write};
use std::path::Path;
use std::path::PathBuf;
#[derive(Parser)]
#[command(version, about)]
struct Cli {
#[clap(short, long)]
input: PathBuf,
#[clap(short, long)]
output: Option<PathBuf>,
#[clap(short, long, value_parser = ["js", "ts", "did"])]
target: String,
}
fn write_output(output_path: &Path, content: &str) -> Result<()> {
if let Some(parent) = output_path.parent() {
create_dir_all(parent)?;
}
write(output_path, content)?;
Ok(())
}
fn main() -> Result<()> {
let cli = Cli::parse();
let (env, actor) = pretty_check_file(&cli.input)?;
let content = match cli.target.as_str() {
"ts" => typescript::compile(&env, &actor),
"js" => javascript::compile(&env, &actor),
"did" => pretty::candid::compile(&env, &actor),
_ => unreachable!(),
};
match cli.output {
None => println!("{content}"),
Some(output) => write_output(&output, &content)?,
}
Ok(())
}