From 22748aaf2e1938f955dc1f98520d6c57a20af097 Mon Sep 17 00:00:00 2001 From: Stephen Waits Date: Tue, 26 Nov 2024 12:50:30 -0800 Subject: [PATCH] Replace _magic_ "preview" literals with a constant (#5452) --- crates/typst-ide/src/tests.rs | 4 ++++ crates/typst-kit/src/package.rs | 9 ++++++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/crates/typst-ide/src/tests.rs b/crates/typst-ide/src/tests.rs index 52b189fa0..5a73fa375 100644 --- a/crates/typst-ide/src/tests.rs +++ b/crates/typst-ide/src/tests.rs @@ -119,6 +119,10 @@ impl IdeWorld for TestWorld { fn packages(&self) -> &[(PackageSpec, Option)] { const LIST: &[(PackageSpec, Option)] = &[( 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 }, diff --git a/crates/typst-kit/src/package.rs b/crates/typst-kit/src/package.rs index 412c7982f..bb0ad7c25 100644 --- a/crates/typst-kit/src/package.rs +++ b/crates/typst-kit/src/package.rs @@ -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 { - 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);