tinymist_world/
lib.rs

1//! World implementation of typst for tinymist.
2
3pub mod args;
4pub mod config;
5pub mod debug_loc;
6pub mod diag;
7pub mod entry;
8pub mod font;
9pub mod package;
10pub mod parser;
11pub mod source;
12pub mod world;
13
14/// Provides mock world builders for tests.
15#[cfg(any(test, feature = "mock"))]
16pub mod mock;
17
18pub use compute::*;
19pub use entry::*;
20pub use snapshot::*;
21pub use world::*;
22
23pub use tinymist_vfs as vfs;
24
25mod compute;
26mod snapshot;
27
28/// Run the compiler in the system environment.
29#[cfg(feature = "system")]
30pub mod system;
31#[cfg(feature = "system")]
32pub use system::{SystemCompilerFeat, TypstSystemUniverse, TypstSystemWorld, print_diagnostics};
33
34/// Run the compiler in the browser environment.
35#[cfg(feature = "browser")]
36pub(crate) mod browser;
37#[cfg(feature = "browser")]
38pub use browser::{BrowserCompilerFeat, TypstBrowserUniverse, TypstBrowserWorld};
39
40use std::{path::Path, sync::Arc};
41
42use ecow::EcoVec;
43use tinymist_vfs::PathAccessModel as VfsAccessModel;
44use typst::diag::{At, FileResult, SourceResult};
45use typst::foundations::Bytes;
46use typst::syntax::{FileId, Span};
47
48use font::FontResolver;
49use package::PackageRegistry;
50
51/// Latest version of the shadow api, which is in beta.
52pub trait ShadowApi {
53    /// Gets the shadow files.
54    fn shadow_paths(&self) -> Vec<Arc<Path>>;
55    /// Gets the shadow files by file id.
56    fn shadow_ids(&self) -> Vec<FileId>;
57
58    /// Resets the shadow files.
59    fn reset_shadow(&mut self) {
60        for path in self.shadow_paths() {
61            self.unmap_shadow(&path).unwrap();
62        }
63    }
64
65    /// Adds a shadow file to the driver.
66    fn map_shadow(&mut self, path: &Path, content: Bytes) -> FileResult<()>;
67
68    /// Removes a shadow file from the driver.
69    fn unmap_shadow(&mut self, path: &Path) -> FileResult<()>;
70
71    /// Adds a shadow file to the driver by file id.
72    /// Note: If a *path* is both shadowed by id and by path, the shadow by id
73    /// will be used.
74    fn map_shadow_by_id(&mut self, file_id: FileId, content: Bytes) -> FileResult<()>;
75
76    /// Removes a shadow file from the driver by file id.
77    /// Note: If a *path* is both shadowed by id and by path, the shadow by id
78    /// will be used.
79    fn unmap_shadow_by_id(&mut self, file_id: FileId) -> FileResult<()>;
80}
81
82/// The extension trait for the shadow api.
83pub trait ShadowApiExt {
84    /// Wraps the universe or world with a given shadow file and runs the inner
85    /// 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    /// Wraps the universe or world with a given shadow file and runs the inner
94    /// function by 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    /// Wraps the universe or world with a given shadow file and runs the inner
107    /// function.
108    fn with_shadow_file<T>(
109        &mut self,
110        file_path: &Path,
111        content: Bytes,
112        f: impl FnOnce(&mut Self) -> SourceResult<T>,
113    ) -> SourceResult<T> {
114        self.map_shadow(file_path, content).at(Span::detached())?;
115        let res: Result<T, EcoVec<typst::diag::SourceDiagnostic>> = f(self);
116        self.unmap_shadow(file_path).at(Span::detached())?;
117        res
118    }
119
120    /// Wraps the universe or world with a given shadow file and runs the inner
121    /// function by file id.
122    /// Note: to enable this function, `ShadowApi` must implement
123    /// `_shadow_map_id`.
124    fn with_shadow_file_by_id<T>(
125        &mut self,
126        file_id: FileId,
127        content: Bytes,
128        f: impl FnOnce(&mut Self) -> SourceResult<T>,
129    ) -> SourceResult<T> {
130        self.map_shadow_by_id(file_id, content)
131            .at(Span::detached())?;
132        let res: Result<T, EcoVec<typst::diag::SourceDiagnostic>> = f(self);
133        self.unmap_shadow_by_id(file_id).at(Span::detached())?;
134        res
135    }
136}
137
138/// Latest version of the world dependencies api, which is in beta.
139pub trait WorldDeps {
140    /// Iterates over the dependencies of the world.
141    fn iter_dependencies(&self, f: &mut dyn FnMut(FileId));
142}
143
144/// The type trait interface of [`CompilerWorld`].
145pub trait CompilerFeat: Send + Sync + 'static {
146    /// The font resolver for the typst compiler.
147    type FontResolver: FontResolver + Send + Sync + Sized;
148    /// The access model for the VFS.
149    type AccessModel: VfsAccessModel + Clone + Send + Sync + Sized;
150    /// The package registry for the typst compiler.
151    type Registry: PackageRegistry + Send + Sync + Sized;
152}
153
154/// The format to use for diagnostics.
155#[derive(Debug, Copy, Clone, Default, Eq, PartialEq, Ord, PartialOrd)]
156pub enum DiagnosticFormat {
157    /// The human-readable format.
158    #[default]
159    Human,
160    /// The short (Unix-flavor) format.
161    Short,
162}
163
164/// The build information of the world crate.
165pub mod build_info {
166    /// The version of the world crate.
167    pub static VERSION: &str = env!("CARGO_PKG_VERSION");
168}