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