tinymist_package/
registry.rs

1//! Package Registry.
2
3use std::num::NonZeroUsize;
4use std::{path::Path, sync::Arc};
5
6use ecow::EcoString;
7pub use typst::diag::PackageError;
8pub use typst::syntax::package::PackageSpec;
9
10mod dummy;
11pub use dummy::*;
12
13mod memory;
14pub use memory::*;
15
16#[cfg(feature = "browser")]
17mod browser;
18#[cfg(feature = "browser")]
19pub use browser::*;
20
21#[cfg(feature = "http-registry")]
22mod http;
23#[cfg(feature = "http-registry")]
24pub use http::*;
25
26/// The default Typst registry.
27pub const DEFAULT_REGISTRY: &str = "https://packages.typst.org";
28
29/// A trait for package registries.
30pub trait PackageRegistry {
31    /// A function to be called when the registry is reset.
32    fn reset(&mut self) {}
33
34    /// If the state of package registry can be well-defined by a revision, it
35    /// should return it. This is used to determine if the compiler should clean
36    /// and pull the registry again.
37    fn revision(&self) -> Option<NonZeroUsize> {
38        None
39    }
40
41    /// Resolves a package specification to a local path.
42    fn resolve(&self, spec: &PackageSpec) -> Result<Arc<Path>, PackageError>;
43
44    /// A list of all available packages and optionally descriptions for them.
45    ///
46    /// This function is optional to implement. It enhances the user experience
47    /// by enabling autocompletion for packages. Details about packages from the
48    /// `@preview` namespace are available from
49    /// `https://packages.typst.org/preview/index.json`.
50    fn packages(&self) -> &[(PackageSpec, Option<EcoString>)] {
51        &[]
52    }
53}
54
55/// A trait for package registries that can be notified.
56pub trait Notifier {
57    /// Called when a package is being downloaded.
58    fn downloading(&self, _spec: &PackageSpec) {}
59}
60
61/// A dummy notifier that does nothing.
62#[derive(Debug, Default, Clone, Copy, Hash)]
63pub struct DummyNotifier;
64
65impl Notifier for DummyNotifier {}