From 51fc6c3abd590b4fb2d7bb695be57b1aee6e568c Mon Sep 17 00:00:00 2001 From: daimond113 Date: Thu, 6 Feb 2025 23:49:25 +0100 Subject: [PATCH] refactor: move schema gen to test Moves schema generation over to a test instead of as a feature. This allows us to publish the crate since we use a schemars from Git, which is not supported by crates.io. --- .gitignore | 3 ++- Cargo.toml | 7 ++++--- registry/Cargo.toml | 2 +- src/engine/mod.rs | 4 ++-- src/manifest/mod.rs | 30 +++++++++++++++++++----------- src/manifest/overrides.rs | 4 ++-- src/manifest/target.rs | 30 ++++++++++++------------------ src/names.rs | 8 ++++---- src/source/git/specifier.rs | 6 +++--- src/source/ids.rs | 2 +- src/source/path/specifier.rs | 2 +- src/source/pesde/specifier.rs | 4 ++-- src/source/specifiers.rs | 2 +- src/source/wally/specifier.rs | 4 ++-- src/source/workspace/specifier.rs | 18 +++++++++--------- 15 files changed, 65 insertions(+), 61 deletions(-) diff --git a/.gitignore b/.gitignore index 7809775..335e1eb 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,5 @@ cobertura.xml tarpaulin-report.html build_rs_cov.profraw registry/data -data \ No newline at end of file +data +manifest.schema.json \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml index 6a3e37c..b450753 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,7 @@ repository = "https://github.com/pesde-pkg/pesde" include = ["src/**/*", "Cargo.toml", "Cargo.lock", "README.md", "LICENSE", "CHANGELOG.md"] [features] +default = ["wally-compat", "patches", "version-management"] bin = [ "dep:clap", "dep:dirs", @@ -34,7 +35,6 @@ bin = [ wally-compat = ["dep:serde_json"] patches = ["dep:git2"] version-management = ["bin"] -schema = ["dep:schemars"] [[bin]] name = "pesde" @@ -75,8 +75,6 @@ git2 = { version = "0.20.0", optional = true } serde_json = { version = "1.0.136", optional = true } -schemars = { git = "https://github.com/daimond113/schemars", rev = "bc7c7d6", features = ["semver1", "url2"], optional = true } - anyhow = { version = "1.0.95", optional = true } open = { version = "5.3.2", optional = true } keyring = { version = "3.6.1", features = ["crypto-rust", "windows-native", "apple-native", "async-secret-service", "tokio"], optional = true } @@ -93,6 +91,9 @@ paste = { version = "1.0.15", optional = true } windows-registry = { version = "0.4.0", optional = true } windows = { version = "0.59.0", features = ["Win32_Storage", "Win32_Storage_FileSystem", "Win32_Security"], optional = true } +[dev-dependencies] +schemars = { git = "https://github.com/daimond113/schemars", rev = "bc7c7d6", features = ["semver1", "url2"] } + [workspace] resolver = "2" members = ["registry"] diff --git a/registry/Cargo.toml b/registry/Cargo.toml index 4237f04..0896846 100644 --- a/registry/Cargo.toml +++ b/registry/Cargo.toml @@ -48,4 +48,4 @@ tracing-actix-web = "0.7.15" sentry = { version = "0.36.0", default-features = false, features = ["backtrace", "contexts", "debug-images", "panic", "reqwest", "rustls", "tracing"] } sentry-actix = "0.36.0" -pesde = { path = "..", features = ["wally-compat"] } +pesde = { path = "..", default-features = false, features = ["wally-compat"] } diff --git a/src/engine/mod.rs b/src/engine/mod.rs index b984468..82e059d 100644 --- a/src/engine/mod.rs +++ b/src/engine/mod.rs @@ -6,8 +6,8 @@ use std::{fmt::Display, str::FromStr}; /// All supported engines #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)] -#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] -#[cfg_attr(feature = "schema", schemars(rename_all = "snake_case"))] +#[cfg_attr(test, derive(schemars::JsonSchema))] +#[cfg_attr(test, schemars(rename_all = "snake_case"))] pub enum EngineKind { /// The pesde package manager Pesde, diff --git a/src/manifest/mod.rs b/src/manifest/mod.rs index 93dd497..336bce3 100644 --- a/src/manifest/mod.rs +++ b/src/manifest/mod.rs @@ -25,7 +25,7 @@ pub mod target; /// A package manifest #[derive(Serialize, Deserialize, Debug, Clone)] -#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] +#[cfg_attr(test, derive(schemars::JsonSchema))] pub struct Manifest { /// The name of the package pub name: PackageName, @@ -50,10 +50,7 @@ pub struct Manifest { pub private: bool, /// The scripts of the package #[serde(default, skip_serializing)] - #[cfg_attr( - feature = "schema", - schemars(with = "BTreeMap") - )] + #[cfg_attr(test, schemars(with = "BTreeMap"))] pub scripts: BTreeMap, /// The indices to use for the package #[serde( @@ -61,7 +58,7 @@ pub struct Manifest { skip_serializing, deserialize_with = "crate::util::deserialize_gix_url_map" )] - #[cfg_attr(feature = "schema", schemars(with = "BTreeMap"))] + #[cfg_attr(test, schemars(with = "BTreeMap"))] pub indices: BTreeMap, /// The indices to use for the package's wally dependencies #[cfg(feature = "wally-compat")] @@ -70,7 +67,7 @@ pub struct Manifest { skip_serializing, deserialize_with = "crate::util::deserialize_gix_url_map" )] - #[cfg_attr(feature = "schema", schemars(with = "BTreeMap"))] + #[cfg_attr(test, schemars(with = "BTreeMap"))] pub wally_indices: BTreeMap, /// The overrides this package has #[serde(default, skip_serializing)] @@ -82,7 +79,7 @@ pub struct Manifest { #[cfg(feature = "patches")] #[serde(default, skip_serializing)] #[cfg_attr( - feature = "schema", + test, schemars( with = "BTreeMap>" ) @@ -99,7 +96,7 @@ pub struct Manifest { pub place: BTreeMap, /// The engines this package supports #[serde(default, skip_serializing_if = "BTreeMap::is_empty")] - #[cfg_attr(feature = "schema", schemars(with = "BTreeMap"))] + #[cfg_attr(test, schemars(with = "BTreeMap"))] pub engines: BTreeMap, /// The standard dependencies of the package @@ -112,7 +109,7 @@ pub struct Manifest { #[serde(default, skip_serializing_if = "BTreeMap::is_empty")] pub dev_dependencies: BTreeMap, /// The user-defined fields of the package - #[cfg_attr(feature = "schema", schemars(skip))] + #[cfg_attr(test, schemars(skip))] #[serde(flatten)] pub user_defined_fields: HashMap, } @@ -151,7 +148,7 @@ impl FromStr for Alias { } } -#[cfg(feature = "schema")] +#[cfg(test)] impl schemars::JsonSchema for Alias { fn schema_name() -> std::borrow::Cow<'static, str> { "Alias".into() @@ -249,3 +246,14 @@ pub mod errors { AliasConflict(Alias), } } + +#[cfg(test)] +mod tests { + #[test] + pub fn generate_schema() { + let schema = schemars::schema_for!(super::Manifest); + let schema = serde_json::to_string_pretty(&schema).unwrap(); + + std::fs::write("manifest.schema.json", schema).unwrap(); + } +} diff --git a/src/manifest/overrides.rs b/src/manifest/overrides.rs index 250c76d..b0e32ba 100644 --- a/src/manifest/overrides.rs +++ b/src/manifest/overrides.rs @@ -32,7 +32,7 @@ impl FromStr for OverrideKey { } } -#[cfg(feature = "schema")] +#[cfg(test)] impl schemars::JsonSchema for OverrideKey { fn schema_name() -> std::borrow::Cow<'static, str> { "OverrideKey".into() @@ -68,7 +68,7 @@ impl Display for OverrideKey { /// A specifier for an override #[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Eq, Hash)] -#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] +#[cfg_attr(test, derive(schemars::JsonSchema))] #[serde(untagged)] pub enum OverrideSpecifier { /// A specifier for a dependency diff --git a/src/manifest/target.rs b/src/manifest/target.rs index 92bfde7..62421d3 100644 --- a/src/manifest/target.rs +++ b/src/manifest/target.rs @@ -9,8 +9,8 @@ use std::{ /// A kind of target #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)] -#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] -#[cfg_attr(feature = "schema", schemars(rename_all = "snake_case"))] +#[cfg_attr(test, derive(schemars::JsonSchema))] +#[cfg_attr(test, schemars(rename_all = "snake_case"))] pub enum TargetKind { /// A Roblox target Roblox, @@ -78,14 +78,14 @@ impl TargetKind { /// A target of a package #[derive(Deserialize, Serialize, Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)] -#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] +#[cfg_attr(test, derive(schemars::JsonSchema))] #[serde(rename_all = "snake_case", tag = "environment")] pub enum Target { /// A Roblox target Roblox { /// The path to the lib export file #[serde(default, skip_serializing_if = "Option::is_none")] - #[cfg_attr(feature = "schema", schemars(with = "Option"))] + #[cfg_attr(test, schemars(with = "Option"))] lib: Option, /// The files to include in the sync tool's config #[serde(default)] @@ -95,7 +95,7 @@ pub enum Target { RobloxServer { /// The path to the lib export file #[serde(default, skip_serializing_if = "Option::is_none")] - #[cfg_attr(feature = "schema", schemars(with = "Option"))] + #[cfg_attr(test, schemars(with = "Option"))] lib: Option, /// The files to include in the sync tool's config #[serde(default)] @@ -105,36 +105,30 @@ pub enum Target { Lune { /// The path to the lib export file #[serde(default, skip_serializing_if = "Option::is_none")] - #[cfg_attr(feature = "schema", schemars(with = "Option"))] + #[cfg_attr(test, schemars(with = "Option"))] lib: Option, /// The path to the bin export file #[serde(default, skip_serializing_if = "Option::is_none")] - #[cfg_attr(feature = "schema", schemars(with = "Option"))] + #[cfg_attr(test, schemars(with = "Option"))] bin: Option, /// The exported scripts #[serde(default, skip_serializing_if = "BTreeMap::is_empty")] - #[cfg_attr( - feature = "schema", - schemars(with = "BTreeMap") - )] + #[cfg_attr(test, schemars(with = "BTreeMap"))] scripts: BTreeMap, }, /// A Luau target Luau { /// The path to the lib export file #[serde(default, skip_serializing_if = "Option::is_none")] - #[cfg_attr(feature = "schema", schemars(with = "Option"))] + #[cfg_attr(test, schemars(with = "Option"))] lib: Option, /// The path to the bin export file #[serde(default, skip_serializing_if = "Option::is_none")] - #[cfg_attr(feature = "schema", schemars(with = "Option"))] + #[cfg_attr(test, schemars(with = "Option"))] bin: Option, /// The exported scripts #[serde(default, skip_serializing_if = "BTreeMap::is_empty")] - #[cfg_attr( - feature = "schema", - schemars(with = "BTreeMap") - )] + #[cfg_attr(test, schemars(with = "BTreeMap"))] scripts: BTreeMap, }, } @@ -197,7 +191,7 @@ impl Display for Target { /// The kind of a Roblox place property #[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)] -#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] +#[cfg_attr(test, derive(schemars::JsonSchema))] #[serde(rename_all = "snake_case")] pub enum RobloxPlaceKind { /// The shared dependencies location diff --git a/src/names.rs b/src/names.rs index 41ae042..986a49d 100644 --- a/src/names.rs +++ b/src/names.rs @@ -72,7 +72,7 @@ impl Display for PackageName { } } -#[cfg(feature = "schema")] +#[cfg(test)] impl schemars::JsonSchema for PackageName { fn schema_name() -> std::borrow::Cow<'static, str> { "PackageName".into() @@ -110,8 +110,8 @@ impl PackageName { /// All possible package names #[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)] -#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] -#[cfg_attr(feature = "schema", schemars(untagged))] +#[cfg_attr(test, derive(schemars::JsonSchema))] +#[cfg_attr(test, schemars(untagged))] pub enum PackageNames { /// A pesde package name Pesde(PackageName), @@ -243,7 +243,7 @@ pub mod wally { } } - #[cfg(feature = "schema")] + #[cfg(test)] impl schemars::JsonSchema for WallyPackageName { fn schema_name() -> std::borrow::Cow<'static, str> { "WallyPackageName".into() diff --git a/src/source/git/specifier.rs b/src/source/git/specifier.rs index eea03f2..e78dab5 100644 --- a/src/source/git/specifier.rs +++ b/src/source/git/specifier.rs @@ -6,20 +6,20 @@ use crate::source::DependencySpecifier; /// The specifier for a Git dependency #[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, Hash)] -#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] +#[cfg_attr(test, derive(schemars::JsonSchema))] pub struct GitDependencySpecifier { /// The repository of the package #[serde( serialize_with = "crate::util::serialize_gix_url", deserialize_with = "crate::util::deserialize_git_like_url" )] - #[cfg_attr(feature = "schema", schemars(with = "url::Url"))] + #[cfg_attr(test, schemars(with = "url::Url"))] pub repo: gix::Url, /// The revision of the package pub rev: String, /// The path of the package in the repository #[serde(default, skip_serializing_if = "Option::is_none")] - #[cfg_attr(feature = "schema", schemars(with = "Option"))] + #[cfg_attr(test, schemars(with = "Option"))] pub path: Option, } impl DependencySpecifier for GitDependencySpecifier {} diff --git a/src/source/ids.rs b/src/source/ids.rs index dfeb16e..f3b37be 100644 --- a/src/source/ids.rs +++ b/src/source/ids.rs @@ -60,7 +60,7 @@ impl FromStr for VersionId { } } -#[cfg(feature = "schema")] +#[cfg(test)] impl schemars::JsonSchema for VersionId { fn schema_name() -> std::borrow::Cow<'static, str> { "VersionId".into() diff --git a/src/source/path/specifier.rs b/src/source/path/specifier.rs index da4c8eb..7a59892 100644 --- a/src/source/path/specifier.rs +++ b/src/source/path/specifier.rs @@ -4,7 +4,7 @@ use std::{fmt::Display, path::PathBuf}; /// The specifier for a path dependency #[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, Hash)] -#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] +#[cfg_attr(test, derive(schemars::JsonSchema))] pub struct PathDependencySpecifier { /// The path to the package pub path: PathBuf, diff --git a/src/source/pesde/specifier.rs b/src/source/pesde/specifier.rs index 2e3004c..1dd42bf 100644 --- a/src/source/pesde/specifier.rs +++ b/src/source/pesde/specifier.rs @@ -5,12 +5,12 @@ use std::fmt::Display; /// The specifier for a pesde dependency #[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, Hash)] -#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] +#[cfg_attr(test, derive(schemars::JsonSchema))] pub struct PesdeDependencySpecifier { /// The name of the package pub name: PackageName, /// The version requirement for the package - #[cfg_attr(feature = "schema", schemars(with = "String"))] + #[cfg_attr(test, schemars(with = "String"))] pub version: VersionReq, /// The index to use for the package #[serde(default, skip_serializing_if = "Option::is_none")] diff --git a/src/source/specifiers.rs b/src/source/specifiers.rs index d3e7e24..b73a746 100644 --- a/src/source/specifiers.rs +++ b/src/source/specifiers.rs @@ -4,7 +4,7 @@ use std::fmt::Display; /// All possible dependency specifiers #[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, Hash)] -#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] +#[cfg_attr(test, derive(schemars::JsonSchema))] #[serde(untagged)] pub enum DependencySpecifiers { /// A pesde dependency specifier diff --git a/src/source/wally/specifier.rs b/src/source/wally/specifier.rs index 078a97a..c8babe2 100644 --- a/src/source/wally/specifier.rs +++ b/src/source/wally/specifier.rs @@ -7,13 +7,13 @@ use crate::{names::wally::WallyPackageName, source::DependencySpecifier}; /// The specifier for a Wally dependency #[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, Hash)] -#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] +#[cfg_attr(test, derive(schemars::JsonSchema))] pub struct WallyDependencySpecifier { /// The name of the package #[serde(rename = "wally")] pub name: WallyPackageName, /// The version requirement for the package - #[cfg_attr(feature = "schema", schemars(with = "String"))] + #[cfg_attr(test, schemars(with = "String"))] pub version: VersionReq, /// The index to use for the package #[serde(default, skip_serializing_if = "Option::is_none")] diff --git a/src/source/workspace/specifier.rs b/src/source/workspace/specifier.rs index 7f97152..8664da9 100644 --- a/src/source/workspace/specifier.rs +++ b/src/source/workspace/specifier.rs @@ -7,7 +7,7 @@ use std::{fmt::Display, str::FromStr}; /// The specifier for a workspace dependency #[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, Hash)] -#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] +#[cfg_attr(test, derive(schemars::JsonSchema))] pub struct WorkspaceDependencySpecifier { /// The name of the workspace package #[serde(rename = "workspace")] @@ -28,20 +28,20 @@ impl Display for WorkspaceDependencySpecifier { /// The type of version to use when publishing a package #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)] -#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] +#[cfg_attr(test, derive(schemars::JsonSchema))] pub enum VersionType { /// The "^" version type #[default] - #[cfg_attr(feature = "schema", schemars(rename = "^"))] + #[cfg_attr(test, schemars(rename = "^"))] Caret, /// The "~" version type - #[cfg_attr(feature = "schema", schemars(rename = "~"))] + #[cfg_attr(test, schemars(rename = "~"))] Tilde, /// The "=" version type - #[cfg_attr(feature = "schema", schemars(rename = "="))] + #[cfg_attr(test, schemars(rename = "="))] Exact, /// The "*" version type - #[cfg_attr(feature = "schema", schemars(rename = "*"))] + #[cfg_attr(test, schemars(rename = "*"))] Wildcard, } ser_display_deser_fromstr!(VersionType); @@ -75,13 +75,13 @@ impl FromStr for VersionType { /// Either a version type or a version requirement #[derive(Debug, Clone, PartialEq, Eq, Hash)] -#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] -#[cfg_attr(feature = "schema", schemars(untagged))] +#[cfg_attr(test, derive(schemars::JsonSchema))] +#[cfg_attr(test, schemars(untagged))] pub enum VersionTypeOrReq { /// A version type VersionType(VersionType), /// A version requirement - #[cfg_attr(feature = "schema", schemars(with = "String"))] + #[cfg_attr(test, schemars(with = "String"))] Req(semver::VersionReq), } ser_display_deser_fromstr!(VersionTypeOrReq);