typst_shim/
syntax_only.rs

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