Skip to main content

dmc/engine/
mod.rs

1use std::path::Path;
2
3use dmc_diagnostic::Code;
4use duck_diagnostic::DiagnosticEngine;
5
6use crate::engine::config::EngineConfig;
7
8pub mod accumulator;
9pub mod cache;
10pub mod collection;
11pub mod compile;
12pub mod config;
13pub mod index;
14pub mod schema_ts;
15pub mod sidecar;
16pub mod utils;
17
18pub struct Engine;
19
20impl Engine {
21  /// Execute one build: optionally clean `output_dir`, process every
22  /// collection in parallel via rayon, then emit `index.js` + `index.d.ts`
23  /// re-exporting each `<name>.json`. With a TS/JS `config_path`, the
24  /// generated `index.d.ts` infers record types via `typeof import(...)`.
25  pub fn run(
26    cfg: &EngineConfig,
27    config_path: Option<&Path>,
28    diag_engine: &mut DiagnosticEngine<Code>,
29  ) -> std::io::Result<()> {
30    if cfg.clean && cfg.output_dir.exists() {
31      std::fs::remove_dir_all(&cfg.output_dir)?;
32    }
33    std::fs::create_dir_all(&cfg.output_dir)?;
34
35    // Warm the math (KaTeX/MathML) cache from disk so previously-rendered
36    // expressions skip the JS engine entirely on this build.
37    let math_cache_path = cfg.output_dir.join(".cache").join("math.json");
38    #[cfg(feature = "math")]
39    if cfg.cache_enabled {
40      dmc_transform::Math::load_cache(&math_cache_path);
41    }
42
43    for c in &cfg.collections {
44      let _ = c.process(cfg, diag_engine);
45    }
46
47    // Flush math cache so the next build starts warm.
48    #[cfg(feature = "math")]
49    if cfg.cache_enabled {
50      dmc_transform::Math::save_cache(&math_cache_path);
51    }
52    let _ = math_cache_path;
53
54    let format = cfg.output_format.as_deref().unwrap_or("esm");
55    index::write_index(&cfg.output_dir, &cfg.collections, format, config_path)?;
56
57    Ok(())
58  }
59}