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  /// Read one mdx file, run the default pipeline, print `CompileOutput`
15  /// as pretty JSON to stdout.
16  pub fn run(self) -> DiagResult<Diagnostic<Code>> {
17    let mut diag_engine = DiagnosticEngine::<Code>::new();
18
19    let src = std::fs::read_to_string(&self.path).map_err(|e| {
20      diag!(
21        Code::Custom { code: String::from("N001"), severity: duck_diagnostic::Severity::Note },
22        format!("read error: {}", e.to_string())
23      )
24    })?;
25
26    let out = Compiler::compile(&src, &mut diag_engine);
27    let json = serde_json::to_string_pretty(&out).unwrap();
28    println!("{}", json);
29
30    Ok(diag!(
31      Code::Custom { code: String::from("N001"), severity: duck_diagnostic::Severity::Note },
32      format!("compiled successfully")
33    ))
34  }
35}