Trait TypstWorld
pub trait TypstWorld: Send + Sync {
// Required methods
fn library(&self) -> &LazyHash<Library>;
fn book(&self) -> &LazyHash<FontBook>;
fn main(&self) -> FileId;
fn source(&self, id: FileId) -> Result<Source, FileError>;
fn file(&self, id: FileId) -> Result<Bytes, FileError>;
fn font(&self, index: usize) -> Option<Font>;
fn today(&self, offset: Option<Duration>) -> Option<Datetime>;
}Expand description
The environment in which typesetting occurs.
All loading functions (main, source, file, font) should perform
internal caching so that they are relatively cheap on repeated invocations
with the same argument. [Source], Bytes, and Font are
all reference-counted and thus cheap to clone.
The compiler doesn’t do the caching itself because the world has much more
information on when something can change. For example, fonts typically don’t
change and can thus even be cached across multiple compilations (for
long-running applications like typst watch). Source files on the other
hand can change and should thus be cleared after each compilation. Advanced
clients like language servers can also retain the source files and
edit them in-place to benefit from better incremental
performance.
Required Methods§
fn library(&self) -> &LazyHash<Library>
fn library(&self) -> &LazyHash<Library>
The standard library.
Can be created through Library::build().
fn source(&self, id: FileId) -> Result<Source, FileError>
fn source(&self, id: FileId) -> Result<Source, FileError>
Try to access the specified file location as a source file.
fn file(&self, id: FileId) -> Result<Bytes, FileError>
fn file(&self, id: FileId) -> Result<Bytes, FileError>
Try to access the specified file.
For file locations for which source succeeds, this
should also succeed. The Bytes can be cheaply created as a view into
an existing [Source] through Bytes::from_string.
fn font(&self, index: usize) -> Option<Font>
fn font(&self, index: usize) -> Option<Font>
Try to access the font with the given index in the font book.
Note that the index is not guaranteed to be in bounds of the font book
returned by this world’s book() function. This is the case because
this function may be invoked with indices from an outdated or different
font book during incremental compilation validation.