dmc-core 0.2.0

Engine, CLI, watch mode, and collection builds for the dmc MDX compiler
Documentation
use std::path::PathBuf;

use crate::{Engine, engine::config::EngineConfig};
use dmc_diagnostic::Code;
use duck_diagnostic::{DiagnosticEngine, print_all_smart};

/// `dmc build`: load config, run the engine once, print the report.
#[derive(clap::Args)]
pub struct BuildCmd {
  #[arg(long, default_value = "dmc.toml")]
  pub config: PathBuf,
  #[arg(short, long)]
  pub strict: bool,
  #[arg(long)]
  pub clean: bool,
}

impl BuildCmd {
  /// Load config, run the engine once, print the report. `strict` aborts
  /// on the first validation failure; `clean` wipes `output_dir` first.
  pub fn run(self) -> std::io::Result<()> {
    let mut diag_engine = DiagnosticEngine::<Code>::new();

    let mut engine_cfg = EngineConfig::load(&self.config)?;
    if self.strict {
      engine_cfg.strict = true;
    }
    if self.clean {
      engine_cfg.clean = true;
    }

    Engine::run(&engine_cfg, Some(&self.config), &mut diag_engine)?;

    // With-source rendering when the primary label points at a readable
    // file; compact otherwise (glob/config/IO errors with no source).
    print_all_smart(&diag_engine, None);

    Ok(())
  }
}