reflexo_world/
lib.rs

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/// Run the compiler in the system environment.
16#[cfg(feature = "system")]
17pub mod system;
18#[cfg(feature = "system")]
19pub use system::{SystemCompilerFeat, TypstSystemUniverse, TypstSystemWorld};
20
21/// Run the compiler in the browser environment.
22#[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
45/// Latest version of the shadow api, which is in beta.
46pub trait ShadowApi {
47    fn _shadow_map_id(&self, _file_id: FileId) -> FileResult<PathBuf> {
48        unimplemented!()
49    }
50
51    /// Get the shadow files.
52    fn shadow_paths(&self) -> Vec<Arc<Path>>;
53
54    /// Reset the shadow files.
55    fn reset_shadow(&mut self) {
56        for path in self.shadow_paths() {
57            self.unmap_shadow(&path).unwrap();
58        }
59    }
60
61    /// Add a shadow file to the driver.
62    fn map_shadow(&mut self, path: &Path, content: Bytes) -> FileResult<()>;
63
64    /// Add a shadow file to the driver.
65    fn unmap_shadow(&mut self, path: &Path) -> FileResult<()>;
66
67    /// Add a shadow file to the driver by file id.
68    /// Note: to enable this function, `ShadowApi` must implement
69    /// `_shadow_map_id`.
70    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    /// Add a shadow file to the driver by file id.
76    /// Note: to enable this function, `ShadowApi` must implement
77    /// `_shadow_map_id`.
78    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    /// Wrap the driver with a given shadow file and run the inner function.
86    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    /// Wrap the driver with a given shadow file and run the inner function by
94    /// file id.
95    /// Note: to enable this function, `ShadowApi` must implement
96    /// `_shadow_map_id`.
97    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    /// Wrap the driver with a given shadow file and run the inner function.
107    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    /// Wrap the driver with a given shadow file and run the inner function by
120    /// file id.
121    /// Note: to enable this function, `ShadowApi` must implement
122    /// `_shadow_map_id`.
123    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
134/// Latest version of the world dependencies api, which is in beta.
135pub 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
142/// type trait interface of [`CompilerWorld`].
143pub trait CompilerFeat {
144    /// Specify the font resolver for typst compiler.
145    type FontResolver: FontResolver + Send + Sync + Sized;
146    /// Specify the access model for VFS.
147    type AccessModel: VfsAccessModel + Clone + Send + Sync + Sized;
148    /// Specify the package registry.
149    type Registry: PackageRegistry + Send + Sync + Sized;
150}
151
152pub mod build_info {
153    /// The version of the reflexo-world crate.
154    pub static VERSION: &str = env!("CARGO_PKG_VERSION");
155}