Wrap extension schema version in a newtype (#9872)

This PR wraps the extension schema version in a newtype, for some
additional type safety.

Release Notes:

- N/A
This commit is contained in:
Marshall Bowers 2024-03-27 12:11:12 -04:00 committed by GitHub
parent 8c56a4b305
commit 3f5f64a044
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 22 additions and 10 deletions

View file

@ -479,7 +479,7 @@ impl ExtensionBuilder {
fn populate_defaults(manifest: &mut ExtensionManifest, extension_path: &Path) -> Result<()> { fn populate_defaults(manifest: &mut ExtensionManifest, extension_path: &Path) -> Result<()> {
// For legacy extensions on the v0 schema (aka, using `extension.json`), clear out any existing // For legacy extensions on the v0 schema (aka, using `extension.json`), clear out any existing
// contents of the computed fields, since we don't care what the existing values are. // contents of the computed fields, since we don't care what the existing values are.
if manifest.schema_version == 0 { if manifest.schema_version.is_v0() {
manifest.languages.clear(); manifest.languages.clear();
manifest.grammars.clear(); manifest.grammars.clear();
manifest.themes.clear(); manifest.themes.clear();
@ -522,7 +522,7 @@ fn populate_defaults(manifest: &mut ExtensionManifest, extension_path: &Path) ->
// For legacy extensions on the v0 schema (aka, using `extension.json`), we want to populate the grammars in // For legacy extensions on the v0 schema (aka, using `extension.json`), we want to populate the grammars in
// the manifest using the contents of the `grammars` directory. // the manifest using the contents of the `grammars` directory.
if manifest.schema_version == 0 { if manifest.schema_version.is_v0() {
let grammars_dir = extension_path.join("grammars"); let grammars_dir = extension_path.join("grammars");
if grammars_dir.exists() { if grammars_dir.exists() {
for entry in fs::read_dir(&grammars_dir).context("failed to list grammars dir")? { for entry in fs::read_dir(&grammars_dir).context("failed to list grammars dir")? {

View file

@ -11,7 +11,7 @@ use std::{
use util::SemanticVersion; use util::SemanticVersion;
/// This is the old version of the extension manifest, from when it was `extension.json`. /// This is the old version of the extension manifest, from when it was `extension.json`.
#[derive(Clone, PartialEq, Eq, Debug, Deserialize, Serialize)] #[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
pub struct OldExtensionManifest { pub struct OldExtensionManifest {
pub name: String, pub name: String,
pub version: Arc<str>, pub version: Arc<str>,
@ -31,12 +31,23 @@ pub struct OldExtensionManifest {
pub grammars: BTreeMap<Arc<str>, PathBuf>, pub grammars: BTreeMap<Arc<str>, PathBuf>,
} }
#[derive(Clone, PartialEq, Eq, Debug, Deserialize, Serialize)] #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy, Serialize, Deserialize)]
pub struct SchemaVersion(pub i32);
impl SchemaVersion {
pub const ZERO: Self = Self(0);
pub fn is_v0(&self) -> bool {
self == &Self::ZERO
}
}
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
pub struct ExtensionManifest { pub struct ExtensionManifest {
pub id: Arc<str>, pub id: Arc<str>,
pub name: String, pub name: String,
pub version: Arc<str>, pub version: Arc<str>,
pub schema_version: i32, pub schema_version: SchemaVersion,
#[serde(default)] #[serde(default)]
pub description: Option<String>, pub description: Option<String>,
@ -122,7 +133,7 @@ fn manifest_from_old_manifest(
description: manifest_json.description, description: manifest_json.description,
repository: manifest_json.repository, repository: manifest_json.repository,
authors: manifest_json.authors, authors: manifest_json.authors,
schema_version: 0, schema_version: SchemaVersion::ZERO,
lib: Default::default(), lib: Default::default(),
themes: { themes: {
let mut themes = manifest_json.themes.into_values().collect::<Vec<_>>(); let mut themes = manifest_json.themes.into_values().collect::<Vec<_>>();

View file

@ -1,3 +1,4 @@
use crate::extension_manifest::SchemaVersion;
use crate::{ use crate::{
Event, ExtensionIndex, ExtensionIndexEntry, ExtensionIndexLanguageEntry, Event, ExtensionIndex, ExtensionIndexEntry, ExtensionIndexLanguageEntry,
ExtensionIndexThemeEntry, ExtensionManifest, ExtensionStore, GrammarManifestEntry, ExtensionIndexThemeEntry, ExtensionManifest, ExtensionStore, GrammarManifestEntry,
@ -146,7 +147,7 @@ async fn test_extension_store(cx: &mut TestAppContext) {
id: "zed-ruby".into(), id: "zed-ruby".into(),
name: "Zed Ruby".into(), name: "Zed Ruby".into(),
version: "1.0.0".into(), version: "1.0.0".into(),
schema_version: 0, schema_version: SchemaVersion::ZERO,
description: None, description: None,
authors: Vec::new(), authors: Vec::new(),
repository: None, repository: None,
@ -171,7 +172,7 @@ async fn test_extension_store(cx: &mut TestAppContext) {
id: "zed-monokai".into(), id: "zed-monokai".into(),
name: "Zed Monokai".into(), name: "Zed Monokai".into(),
version: "2.0.0".into(), version: "2.0.0".into(),
schema_version: 0, schema_version: SchemaVersion::ZERO,
description: None, description: None,
authors: vec![], authors: vec![],
repository: None, repository: None,
@ -328,7 +329,7 @@ async fn test_extension_store(cx: &mut TestAppContext) {
id: "zed-gruvbox".into(), id: "zed-gruvbox".into(),
name: "Zed Gruvbox".into(), name: "Zed Gruvbox".into(),
version: "1.0.0".into(), version: "1.0.0".into(),
schema_version: 0, schema_version: SchemaVersion::ZERO,
description: None, description: None,
authors: vec![], authors: vec![],
repository: None, repository: None,

View file

@ -95,7 +95,7 @@ async fn main() -> Result<()> {
version: manifest.version, version: manifest.version,
description: manifest.description, description: manifest.description,
authors: manifest.authors, authors: manifest.authors,
schema_version: Some(manifest.schema_version), schema_version: Some(manifest.schema_version.0),
repository: manifest repository: manifest
.repository .repository
.ok_or_else(|| anyhow!("missing repository in extension manifest"))?, .ok_or_else(|| anyhow!("missing repository in extension manifest"))?,