tinymist_world/
lib.rs

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