1#![allow(missing_docs)]
4
5pub mod args;
6
7pub mod source;
8
9pub mod config;
10
11pub mod entry;
12pub use entry::*;
13
14pub mod world;
15pub use world::*;
16
17pub mod font;
18pub mod package;
19pub mod parser;
20mod snapshot;
21pub use snapshot::*;
22mod compute;
23pub use compute::*;
24
25pub use tinymist_vfs as vfs;
26
27#[cfg(feature = "system")]
29pub mod system;
30#[cfg(feature = "system")]
31pub use system::{SystemCompilerFeat, TypstSystemUniverse, TypstSystemWorld};
32
33#[cfg(feature = "browser")]
35pub(crate) mod browser;
36#[cfg(feature = "browser")]
37pub use browser::{BrowserCompilerFeat, TypstBrowserUniverse, TypstBrowserWorld};
38
39use std::{path::Path, sync::Arc};
40
41use ecow::EcoVec;
42use tinymist_vfs::PathAccessModel as VfsAccessModel;
43use typst::diag::{At, FileResult, SourceResult};
44use typst::foundations::Bytes;
45use typst::syntax::{FileId, Span};
46
47use font::FontResolver;
48use package::PackageRegistry;
49
50pub trait ShadowApi {
52 fn shadow_paths(&self) -> Vec<Arc<Path>>;
54 fn shadow_ids(&self) -> Vec<FileId>;
56
57 fn reset_shadow(&mut self) {
59 for path in self.shadow_paths() {
60 self.unmap_shadow(&path).unwrap();
61 }
62 }
63
64 fn map_shadow(&mut self, path: &Path, content: Bytes) -> FileResult<()>;
66
67 fn unmap_shadow(&mut self, path: &Path) -> FileResult<()>;
69
70 fn map_shadow_by_id(&mut self, file_id: FileId, content: Bytes) -> FileResult<()>;
74
75 fn unmap_shadow_by_id(&mut self, file_id: FileId) -> FileResult<()>;
79}
80
81pub trait ShadowApiExt {
82 fn with_shadow_file<T>(
84 &mut self,
85 file_path: &Path,
86 content: Bytes,
87 f: impl FnOnce(&mut Self) -> SourceResult<T>,
88 ) -> SourceResult<T>;
89
90 fn with_shadow_file_by_id<T>(
95 &mut self,
96 file_id: FileId,
97 content: Bytes,
98 f: impl FnOnce(&mut Self) -> SourceResult<T>,
99 ) -> SourceResult<T>;
100}
101
102impl<C: ShadowApi> ShadowApiExt for C {
103 fn with_shadow_file<T>(
105 &mut self,
106 file_path: &Path,
107 content: Bytes,
108 f: impl FnOnce(&mut Self) -> SourceResult<T>,
109 ) -> SourceResult<T> {
110 self.map_shadow(file_path, content).at(Span::detached())?;
111 let res: Result<T, EcoVec<typst::diag::SourceDiagnostic>> = f(self);
112 self.unmap_shadow(file_path).at(Span::detached())?;
113 res
114 }
115
116 fn with_shadow_file_by_id<T>(
121 &mut self,
122 file_id: FileId,
123 content: Bytes,
124 f: impl FnOnce(&mut Self) -> SourceResult<T>,
125 ) -> SourceResult<T> {
126 self.map_shadow_by_id(file_id, content)
127 .at(Span::detached())?;
128 let res: Result<T, EcoVec<typst::diag::SourceDiagnostic>> = f(self);
129 self.unmap_shadow_by_id(file_id).at(Span::detached())?;
130 res
131 }
132}
133
134pub trait WorldDeps {
136 fn iter_dependencies(&self, f: &mut dyn FnMut(FileId));
137}
138
139pub trait CompilerFeat: Send + Sync + 'static {
141 type FontResolver: FontResolver + Send + Sync + Sized;
143 type AccessModel: VfsAccessModel + Clone + Send + Sync + Sized;
145 type Registry: PackageRegistry + Send + Sync + Sized;
147}
148
149pub mod build_info {
150 pub static VERSION: &str = env!("CARGO_PKG_VERSION");
152}