1pub mod source;
2
3pub mod config;
4
5pub mod entry;
6pub use entry::*;
7
8pub mod world;
9pub use world::*;
10
11pub mod font;
12pub mod package;
13pub mod parser;
14
15#[cfg(feature = "system")]
17pub mod system;
18#[cfg(feature = "system")]
19pub use system::{SystemCompilerFeat, TypstSystemUniverse, TypstSystemWorld};
20
21#[cfg(feature = "browser")]
23pub(crate) mod browser;
24#[cfg(feature = "browser")]
25pub use browser::{BrowserCompilerFeat, TypstBrowserUniverse, TypstBrowserWorld};
26
27use std::{
28 path::{Path, PathBuf},
29 sync::Arc,
30};
31
32use ecow::EcoVec;
33use reflexo::ImmutPath;
34use reflexo_vfs::AccessModel as VfsAccessModel;
35use typst::{
36 diag::{At, FileResult, SourceResult},
37 foundations::Bytes,
38 syntax::FileId,
39 syntax::Span,
40};
41
42use font::FontResolver;
43use package::PackageRegistry;
44
45pub trait ShadowApi {
47 fn _shadow_map_id(&self, _file_id: FileId) -> FileResult<PathBuf> {
48 unimplemented!()
49 }
50
51 fn shadow_paths(&self) -> Vec<Arc<Path>>;
53
54 fn reset_shadow(&mut self) {
56 for path in self.shadow_paths() {
57 self.unmap_shadow(&path).unwrap();
58 }
59 }
60
61 fn map_shadow(&mut self, path: &Path, content: Bytes) -> FileResult<()>;
63
64 fn unmap_shadow(&mut self, path: &Path) -> FileResult<()>;
66
67 fn map_shadow_by_id(&mut self, file_id: FileId, content: Bytes) -> FileResult<()> {
71 let file_path = self._shadow_map_id(file_id)?;
72 self.map_shadow(&file_path, content)
73 }
74
75 fn unmap_shadow_by_id(&mut self, file_id: FileId) -> FileResult<()> {
79 let file_path = self._shadow_map_id(file_id)?;
80 self.unmap_shadow(&file_path)
81 }
82}
83
84pub trait ShadowApiExt {
85 fn with_shadow_file<T>(
87 &mut self,
88 file_path: &Path,
89 content: Bytes,
90 f: impl FnOnce(&mut Self) -> SourceResult<T>,
91 ) -> SourceResult<T>;
92
93 fn with_shadow_file_by_id<T>(
98 &mut self,
99 file_id: FileId,
100 content: Bytes,
101 f: impl FnOnce(&mut Self) -> SourceResult<T>,
102 ) -> SourceResult<T>;
103}
104
105impl<C: ShadowApi> ShadowApiExt for C {
106 fn with_shadow_file<T>(
108 &mut self,
109 file_path: &Path,
110 content: Bytes,
111 f: impl FnOnce(&mut Self) -> SourceResult<T>,
112 ) -> SourceResult<T> {
113 self.map_shadow(file_path, content).at(Span::detached())?;
114 let res: Result<T, EcoVec<typst::diag::SourceDiagnostic>> = f(self);
115 self.unmap_shadow(file_path).at(Span::detached())?;
116 res
117 }
118
119 fn with_shadow_file_by_id<T>(
124 &mut self,
125 file_id: FileId,
126 content: Bytes,
127 f: impl FnOnce(&mut Self) -> SourceResult<T>,
128 ) -> SourceResult<T> {
129 let file_path = self._shadow_map_id(file_id).at(Span::detached())?;
130 self.with_shadow_file(&file_path, content, f)
131 }
132}
133
134pub trait WorldDeps {
136 fn iter_dependencies(&self, f: &mut dyn FnMut(ImmutPath));
137}
138
139type CodespanResult<T> = Result<T, CodespanError>;
140type CodespanError = codespan_reporting::files::Error;
141
142pub trait CompilerFeat {
144 type FontResolver: FontResolver + Send + Sync + Sized;
146 type AccessModel: VfsAccessModel + Clone + Send + Sync + Sized;
148 type Registry: PackageRegistry + Send + Sync + Sized;
150}
151
152pub mod build_info {
153 pub static VERSION: &str = env!("CARGO_PKG_VERSION");
155}