Skip to main content

dmc/engine/
config.rs

1use dmc_diagnostic::{Code, DiagResult};
2use duck_diagnostic::diag;
3use serde::{Deserialize, Serialize};
4use std::path::PathBuf;
5
6use crate::engine::{collection::Collection, compile::CompileConfig};
7
8/// Top-level engine config. Drives `Engine::run`: collections, output
9/// location, schema strictness, JS plugin hooks (remark/rehype via the
10/// Node sidecar), and feature flags such as GFM toggling.
11#[derive(Debug, Deserialize, Serialize, Clone)]
12#[serde(default)]
13pub struct EngineConfig {
14  pub root: PathBuf,
15  pub output_dir: PathBuf,
16  pub output_name: Option<String>,
17  pub output_format: Option<String>,
18  pub clean: bool,
19  pub strict: bool,
20  pub collections: Vec<Collection>,
21  pub include_html: bool,
22  /// Persist per-file compile output to `<output_dir>/.cache/dmc/`. On
23  /// the next build, files whose source bytes + config are unchanged
24  /// skip lex/parse/transform/codegen + sidecar entirely.
25  pub cache_enabled: bool,
26
27  #[serde(flatten)]
28  pub compile: CompileConfig,
29}
30
31impl Default for EngineConfig {
32  fn default() -> Self {
33    Self {
34      root: PathBuf::new(),
35      output_dir: PathBuf::new(),
36      output_name: None,
37      output_format: None,
38      clean: false,
39      strict: false,
40      collections: Vec::new(),
41      include_html: false,
42      cache_enabled: true,
43      compile: CompileConfig::default(),
44    }
45  }
46}
47
48impl EngineConfig {
49  /// Read `dmc.toml` (or a `.ts` / `.js` / `.mjs` config) into an
50  /// `EngineConfig`. Routes through `load_ts` for JS-flavoured configs.
51  pub(crate) fn load(config_path: &PathBuf) -> DiagResult<EngineConfig> {
52    let raw = std::fs::read_to_string(config_path)
53      .map_err(|e| diag!(Code::InvalidConfigPath, format!("config error: {}", e.to_string())))?;
54
55    let cfg: EngineConfig =
56      toml::from_str(&raw).map_err(|e| diag!(Code::InvalidConfig, format!("config error: {}", e.to_string())))?;
57
58    Ok(cfg)
59  }
60}