Repository navigation
Rework and expand typst-kit - #7710
Conversation
|
cc @tingerrr who originally introduced |
tingerrr
left a comment
There was a problem hiding this comment.
I've taken a quick look, quick for two reasons:
- The code is actually fairly familiar, I've written very similar things for Tytanic. So reviewing it feels like reviewing my own code.
- Stress at work means I have less energy in my free time.
Here's what I've done similarly:
- file storage and package access
- font loading
- terminal diagnostics
I think your implementations take care of the same problems mine faced and work quite nicely. For example, I had initially implemented a virtual storage and a system storage both implementing my equivalent of the FileStore trait, the Loader trait is a clever abstraction there.
There are some things I haven't implemented in Tytanic, like file watching and HTML support, so the server and watcher are things I generally skipped over due to my lack of experience.
I don't think I can try these out as a dirty implementation in Tytanic, I won't have time until next weekend. And since they are so similar to mine, I likely wouldn't find any issues with API being inflexible.
I suggest @Myriad-Dreamin and co take a look, perhaps some requirements for tinymist can be satisfied here.
add6dc1 to
434c862
Compare
Typst Kit provides reusable building blocks to downstream Typst integrations. This PR improves the API of existing functionality and adds a bunch of additional functionality. The original motivation for this was reusing some stuff from the CLI in the upcoming Typst-based docs.
Overview
I have attempted to design APIs that are as general and flexible as possible, supporting varied use cases. I'll give a brief overview over the changes and additions in each subsystem below. I've also remodelled the feature flags for more flexibility and naming consistency.
Fonts
Font loading is now a bit more generic: The new
FontStorecan still load embedded, path, and system fonts, but things can be composed somewhat more freely than before, and a font can also be loaded generically through a trait.Files
The largest (and probably most useful) additions is the new
FileStore, which is backed by aFileLoader. It takes care of creating, caching, and manages Typst source files, while delegating the raw loading to an underlying loader. From what I've seen, getting this right is (understandibly) the most difficult part about building aWorld. The new implementation is also a bit more efficient and correct than the old CLI's. When a file is used both as a source file and raw bytes, it's only read once now and the backing storage is reused as much as possible.Packages
The previous
PackageStorageis now broken up into three package providers:FsPackages,UniversePackages, andSystemPackages(which combines the two). Instead of being coupled to a fixed downloader implementation,UniversePackagesdepends on a genericimpl Downloader.Diagnostics
typst-kitnow has adiagnostics::emitfunction, which emits formatted CLI-style diagnostics to a colorized writable. It's backed by the newDiagnosticsWorld(an extension to theWorldwhich can resolve names of files). This is similar in spirit totypst_ide::IdeWorldwhich adds extra requirements to theWorldfor usage with IDE functionality.Downloader
As already mentioned, the concrete provided downloader (based on
ureq) is now decoupled from abstract downloading. I've also decoupled downloading itself from progress reporting by adding a newProgressDownloaderthat wraps a generic downloader. Now, individual download calls do not decide whether progress reporting is desired. Instead, they provide a dynamic key, which the progress downloader can use to decide whether it wants to emit something (e.g. printing to the console). I've also fixed a bug with the download status printing, which would swallow the initial "downloading ..." line on the first update and show the status line twice when finishing.Watcher
The file watching implementation of the CLI is now exposed in
typst-kit.Server
The live-reloading HTTP server implementation of the CLI is now also exposed in
typst-kit.Breakage
Given that the PR reworks the full API surface, it's obviously breaking. Beyond that, I've changed the default features from all to none. It is actually not that common to need all and selecting exactly what one needs is encouraged by this new default.
Beyond
typst-kit, in the process of adjusting the feature flags, I've also renamed the CLI'sembed-fontsfeature flag toembedded-fonts.Checking the API surface
For the best experience when browsing the new API's docs, I recommend the
docsrscfg because it gives nice docs for the future flag:RUSTDOCFLAGS="--cfg docsrs" cargo +nightly doc -p typst-kit --no-depsWhy now?
I fell down a rabbit hole while working on the Docs-in-Typst. While it took a couple of days, I am quite happy with the new APIs and in particular the
FileStoreshould make it considerably easier to build aWorld.Issues
SystemWorldis pretty thin now, I would say it's also fair to say that this closes Add implementation of SystemWorld to typst-kit #6044