tinymist_world/
lib.rs

1//! World implementation of typst for tinymist.
2
3#![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/// Run the compiler in the system environment.
28#[cfg(feature = "system")]
29pub mod system;
30#[cfg(feature = "system")]
31pub use system::{SystemCompilerFeat, TypstSystemUniverse, TypstSystemWorld};
32
33/// Run the compiler in the browser environment.
34#[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
50/// Latest version of the shadow api, which is in beta.
51pub trait ShadowApi {
52    /// Get the shadow files.
53    fn shadow_paths(&self) -> Vec<Arc<Path>>;
54    /// Get the shadow files by file id.
55    fn shadow_ids(&self) -> Vec<FileId>;
56
57    /// Reset the shadow files.
58    fn reset_shadow(&mut self) {
59        for path in self.shadow_paths() {
60            self.unmap_shadow(&path).unwrap();
61        }
62    }
63
64    /// Add a shadow file to the driver.
65    fn map_shadow(&mut self, path: &Path, content: Bytes) -> FileResult<()>;
66
67    /// Add a shadow file to the driver.
68    fn unmap_shadow(&mut self, path: &Path) -> FileResult<()>;
69
70    /// Add a shadow file to the driver by file id.
71    /// Note: If a *path* is both shadowed by id and by path, the shadow by id
72    /// will be used.
73    fn map_shadow_by_id(&mut self, file_id: FileId, content: Bytes) -> FileResult<()>;
74
75    /// Add a shadow file to the driver by file id.
76    /// Note: If a *path* is both shadowed by id and by path, the shadow by id
77    /// will be used.
78    fn unmap_shadow_by_id(&mut self, file_id: FileId) -> FileResult<()>;
79}
80
81pub trait ShadowApiExt {
82    /// Wrap the driver with a given shadow file and run the inner function.
83    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    /// Wrap the driver with a given shadow file and run the inner function by
91    /// file id.
92    /// Note: to enable this function, `ShadowApi` must implement
93    /// `_shadow_map_id`.
94    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    /// Wrap the driver with a given shadow file and run the inner function.
104    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    /// Wrap the driver with a given shadow file and run the inner function by
117    /// file id.
118    /// Note: to enable this function, `ShadowApi` must implement
119    /// `_shadow_map_id`.
120    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
134/// Latest version of the world dependencies api, which is in beta.
135pub trait WorldDeps {
136    fn iter_dependencies(&self, f: &mut dyn FnMut(FileId));
137}
138
139/// type trait interface of [`CompilerWorld`].
140pub trait CompilerFeat: Send + Sync + 'static {
141    /// Specify the font resolver for typst compiler.
142    type FontResolver: FontResolver + Send + Sync + Sized;
143    /// Specify the access model for VFS.
144    type AccessModel: VfsAccessModel + Clone + Send + Sync + Sized;
145    /// Specify the package registry.
146    type Registry: PackageRegistry + Send + Sync + Sized;
147}
148
149pub mod build_info {
150    /// The version of the reflexo-world crate.
151    pub static VERSION: &str = env!("CARGO_PKG_VERSION");
152}