tinymist_world/package/
mod.rs

1impl Notifier for DummyNotifier {}
2use std::num::NonZeroUsize;
3use std::{path::Path, sync::Arc};
4
5use ecow::EcoString;
6use tinymist_std::ImmutPath;
7use typst::diag::FileResult;
8pub use typst::diag::PackageError;
9pub use typst::syntax::package::PackageSpec;
10
11pub mod dummy;
12
13#[cfg(feature = "browser")]
14pub mod browser;
15
16#[cfg(feature = "http-registry")]
17pub mod http;
18
19pub trait PackageRegistry {
20    fn reset(&mut self) {}
21
22    fn revision(&self) -> Option<NonZeroUsize> {
23        None
24    }
25
26    fn resolve(&self, spec: &PackageSpec) -> Result<Arc<Path>, PackageError>;
27
28    /// A list of all available packages and optionally descriptions for them.
29    ///
30    /// This function is optional to implement. It enhances the user experience
31    /// by enabling autocompletion for packages. Details about packages from the
32    /// `@preview` namespace are available from
33    /// `https://packages.typst.org/preview/index.json`.
34    fn packages(&self) -> &[(PackageSpec, Option<EcoString>)] {
35        &[]
36    }
37}
38
39pub struct RegistryPathMapper<T> {
40    pub registry: Arc<T>,
41}
42
43impl<T> RegistryPathMapper<T> {
44    pub fn new(registry: Arc<T>) -> Self {
45        Self { registry }
46    }
47}
48
49impl<T: PackageRegistry> tinymist_vfs::RootResolver for RegistryPathMapper<T> {
50    fn resolve_package_root(&self, pkg: &PackageSpec) -> FileResult<ImmutPath> {
51        Ok(self.registry.resolve(pkg)?)
52    }
53}
54
55pub trait Notifier {
56    fn downloading(&self, _spec: &PackageSpec) {}
57}
58
59#[derive(Debug, Default, Clone, Copy, Hash)]
60pub struct DummyNotifier;