Skip to main content

dmc/cli/
compile.rs

1use std::path::PathBuf;
2
3use crate::engine::compile::Compiler;
4use dmc_diagnostic::{Code, DiagResult};
5use duck_diagnostic::{Diagnostic, DiagnosticEngine, diag};
6
7#[derive(clap::Args)]
8pub struct CompileCmd {
9  #[arg(long, default_value = "dmc.toml")]
10  pub path: PathBuf,
11}
12
13impl CompileCmd {
14  pub fn run(self) -> DiagResult<Diagnostic<Code>> {
15    let mut diag_engine = DiagnosticEngine::<Code>::new();
16
17    let src = std::fs::read_to_string(&self.path).map_err(|e| {
18      diag!(
19        Code::Custom { code: String::from("N001"), severity: duck_diagnostic::Severity::Note },
20        format!("read error: {}", e.to_string())
21      )
22    })?;
23
24    let out = Compiler::compile(&src, &mut diag_engine);
25    let json = serde_json::to_string_pretty(&out).unwrap();
26    println!("{}", json);
27
28    Ok(diag!(
29      Code::Custom { code: String::from("N001"), severity: duck_diagnostic::Severity::Note },
30      format!("compiled successfully")
31    ))
32  }
33}