Replace _magic_ "preview" literals with a constant (#5452)

This commit is contained in:
Stephen Waits 2024-11-26 12:50:30 -08:00 committed by GitHub
parent 39e41ba3c6
commit 22748aaf2e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 10 additions and 3 deletions

View File

@ -119,6 +119,10 @@ impl IdeWorld for TestWorld {
fn packages(&self) -> &[(PackageSpec, Option<EcoString>)] {
const LIST: &[(PackageSpec, Option<EcoString>)] = &[(
PackageSpec {
// NOTE: This literal, `"preview"`, should match the const, `DEFAULT_NAMESPACE`,
// defined in `crates/typst-kit/src/package.rs`. However, we should always use the
// literal here, not `DEFAULT_NAMESPACE`, so that this test fails if its value
// changes in an unexpected way.
namespace: EcoString::inline("preview"),
name: EcoString::inline("example"),
version: PackageVersion { major: 0, minor: 1, patch: 0 },

View File

@ -15,6 +15,9 @@ use crate::download::{Downloader, Progress};
/// The default Typst registry.
pub const DEFAULT_REGISTRY: &str = "https://packages.typst.org";
/// The public namespace in the default Typst registry.
pub const DEFAULT_NAMESPACE: &str = "preview";
/// The default packages sub directory within the package and package cache paths.
pub const DEFAULT_PACKAGES_SUBDIR: &str = "typst/packages";
@ -85,7 +88,7 @@ impl PackageStorage {
}
// Download from network if it doesn't exist yet.
if spec.namespace == "preview" {
if spec.namespace == DEFAULT_NAMESPACE {
self.download_package(spec, &dir, progress)?;
if dir.exists() {
return Ok(dir);
@ -101,7 +104,7 @@ impl PackageStorage {
&self,
spec: &VersionlessPackageSpec,
) -> StrResult<PackageVersion> {
if spec.namespace == "preview" {
if spec.namespace == DEFAULT_NAMESPACE {
// For `@preview`, download the package index and find the latest
// version.
self.download_index()?
@ -155,7 +158,7 @@ impl PackageStorage {
package_dir: &Path,
progress: &mut dyn Progress,
) -> PackageResult<()> {
assert_eq!(spec.namespace, "preview");
assert_eq!(spec.namespace, DEFAULT_NAMESPACE);
let url =
format!("{DEFAULT_REGISTRY}/preview/{}-{}.tar.gz", spec.name, spec.version);