use std::path::PathBuf;
use clap::Parser;
use miden_assembly::diagnostics::{IntoDiagnostic, Report, WrapErr};
use super::data::{Libraries, ProgramFile};
#[derive(Debug, Clone, Parser)]
#[command(about = "Assemble a Miden program")]
pub struct CompileCmd {
#[arg(short = 'a', long = "assembly", value_parser)]
assembly_file: PathBuf,
#[arg(short = 'l', long = "libraries", value_parser)]
library_paths: Vec<PathBuf>,
#[arg(short = 'o', long = "output", value_parser)]
output_file: Option<PathBuf>,
}
impl CompileCmd {
pub fn execute(&self) -> Result<(), Report> {
println!("============================================================");
println!("Compile program");
println!("============================================================");
let program = ProgramFile::read(&self.assembly_file)?;
let libraries = Libraries::new(&self.library_paths)?;
let compiled_program = program.compile(&libraries.libraries)?;
let program_hash: [u8; 32] = compiled_program.hash().into();
println!("program hash is {}", hex::encode(program_hash));
let out_path = self.output_file.clone().unwrap_or_else(|| {
let mut out_file = self.assembly_file.clone();
out_file.set_extension("masb");
out_file
});
compiled_program
.write_to_file(out_path)
.into_diagnostic()
.wrap_err("Failed to write the compiled file")
}
}