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
14pub use compute::*;
15pub use entry::*;
16pub use snapshot::*;
17pub use world::*;
18
19pub use tinymist_vfs as vfs;
20
21mod compute;
22mod snapshot;
23
24/// Run the compiler in the system environment.
25#[cfg(feature = "system")]
26pub mod system;
27#[cfg(feature = "system")]
28pub use system::{SystemCompilerFeat, TypstSystemUniverse, TypstSystemWorld, print_diagnostics};
29
30/// Run the compiler in the browser environment.
31#[cfg(feature = "browser")]
32pub(crate) mod browser;
33#[cfg(feature = "browser")]
34pub use browser::{BrowserCompilerFeat, TypstBrowserUniverse, TypstBrowserWorld};
35
36use std::{path::Path, sync::Arc};
37
38use ecow::EcoVec;
39use tinymist_vfs::PathAccessModel as VfsAccessModel;
40use typst::diag::{At, FileResult, SourceResult};
41use typst::foundations::Bytes;
42use typst::syntax::{FileId, Span};
43
44use font::FontResolver;
45use package::PackageRegistry;
46
47/// Latest version of the shadow api, which is in beta.
48pub trait ShadowApi {
49    /// Gets the shadow files.
50    fn shadow_paths(&self) -> Vec<Arc<Path>>;
51    /// Gets the shadow files by file id.
52    fn shadow_ids(&self) -> Vec<FileId>;
53
54    /// Resets 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    /// Adds a shadow file to the driver.
62    fn map_shadow(&mut self, path: &Path, content: Bytes) -> FileResult<()>;
63
64    /// Removes a shadow file from the driver.
65    fn unmap_shadow(&mut self, path: &Path) -> FileResult<()>;
66
67    /// Adds a shadow file to the driver by file id.
68    /// Note: If a *path* is both shadowed by id and by path, the shadow by id
69    /// will be used.
70    fn map_shadow_by_id(&mut self, file_id: FileId, content: Bytes) -> FileResult<()>;
71
72    /// Removes a shadow file from the driver by file id.
73    /// Note: If a *path* is both shadowed by id and by path, the shadow by id
74    /// will be used.
75    fn unmap_shadow_by_id(&mut self, file_id: FileId) -> FileResult<()>;
76}
77
78/// The extension trait for the shadow api.
79pub trait ShadowApiExt {
80    /// Wraps the universe or world with a given shadow file and runs the inner
81    /// function.
82    fn with_shadow_file<T>(
83        &mut self,
84        file_path: &Path,
85        content: Bytes,
86        f: impl FnOnce(&mut Self) -> SourceResult<T>,
87    ) -> SourceResult<T>;
88
89    /// Wraps the universe or world with a given shadow file and runs the inner
90    /// function by file id.
91    /// Note: to enable this function, `ShadowApi` must implement
92    /// `_shadow_map_id`.
93    fn with_shadow_file_by_id<T>(
94        &mut self,
95        file_id: FileId,
96        content: Bytes,
97        f: impl FnOnce(&mut Self) -> SourceResult<T>,
98    ) -> SourceResult<T>;
99}
100
101impl<C: ShadowApi> ShadowApiExt for C {
102    /// Wraps the universe or world with a given shadow file and runs the inner
103    /// 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    /// Wraps the universe or world with a given shadow file and runs the inner
117    /// function by 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    /// Iterates over the dependencies of the world.
137    fn iter_dependencies(&self, f: &mut dyn FnMut(FileId));
138}
139
140/// The type trait interface of [`CompilerWorld`].
141pub trait CompilerFeat: Send + Sync + 'static {
142    /// The font resolver for the typst compiler.
143    type FontResolver: FontResolver + Send + Sync + Sized;
144    /// The access model for the VFS.
145    type AccessModel: VfsAccessModel + Clone + Send + Sync + Sized;
146    /// The package registry for the typst compiler.
147    type Registry: PackageRegistry + Send + Sync + Sized;
148}
149
150/// The format to use for diagnostics.
151#[derive(Debug, Copy, Clone, Default, Eq, PartialEq, Ord, PartialOrd)]
152pub enum DiagnosticFormat {
153    /// The human-readable format.
154    #[default]
155    Human,
156    /// The short (Unix-flavor) format.
157    Short,
158}
159
160/// The build information of the world crate.
161pub mod build_info {
162    /// The version of the world crate.
163    pub static VERSION: &str = env!("CARGO_PKG_VERSION");
164}