typst_shim/
syntax_only.rs

1use std::sync::atomic::{AtomicBool, Ordering};
2use typst::{
3    Document, World,
4    diag::{SourceDiagnostic, SourceResult, Warned},
5    ecow::eco_vec,
6};
7use typst_syntax::Span;
8
9/// Global flag indicating whether syntax-only mode is enabled.
10pub static SYNTAX_ONLY: AtomicBool = AtomicBool::new(false);
11
12/// Check if syntax-only mode is enabled.
13pub fn is_syntax_only() -> bool {
14    SYNTAX_ONLY.load(Ordering::Acquire)
15}
16
17/// Compile the document if syntax-only mode is disabled; otherwise, return an
18/// error.
19pub fn compile_opt<D>(world: &dyn World) -> Warned<SourceResult<D>>
20where
21    D: Document,
22{
23    if is_syntax_only() {
24        Warned {
25            output: Err(eco_vec![SourceDiagnostic::error(
26                Span::detached(),
27                "Compilation is disabled in syntax-only mode.",
28            )]),
29            warnings: Default::default(),
30        }
31    } else {
32        typst::compile::<D>(world)
33    }
34}