From 3738481c5b184b4c3686b70872c8710a836d8b0b Mon Sep 17 00:00:00 2001 From: Vito Secona Date: Fri, 19 Sep 2025 05:36:02 +0700 Subject: [PATCH] feat: add lockfile schema generation --- .../cargo-util-schemas/lockfile.schema.json | 133 ++++++++++++++++++ crates/cargo-util-schemas/src/lockfile.rs | 17 +++ 2 files changed, 150 insertions(+) create mode 100644 crates/cargo-util-schemas/lockfile.schema.json diff --git a/crates/cargo-util-schemas/lockfile.schema.json b/crates/cargo-util-schemas/lockfile.schema.json new file mode 100644 index 000000000..fe82ef6f3 --- /dev/null +++ b/crates/cargo-util-schemas/lockfile.schema.json @@ -0,0 +1,133 @@ +{ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "title": "EncodableResolve", + "description": "The `Cargo.lock` structure.", + "type": "object", + "properties": { + "version": { + "type": [ + "integer", + "null" + ], + "format": "uint32", + "minimum": 0 + }, + "package": { + "type": [ + "array", + "null" + ], + "items": { + "$ref": "#/$defs/EncodableDependency" + } + }, + "root": { + "description": "`root` is optional to allow backward compatibility.", + "anyOf": [ + { + "$ref": "#/$defs/EncodableDependency" + }, + { + "type": "null" + } + ] + }, + "metadata": { + "type": [ + "object", + "null" + ], + "additionalProperties": { + "type": "string" + } + }, + "patch": { + "$ref": "#/$defs/Patch" + } + }, + "$defs": { + "EncodableDependency": { + "type": "object", + "properties": { + "name": { + "type": "string" + }, + "version": { + "type": "string" + }, + "source": { + "type": [ + "string", + "null" + ] + }, + "checksum": { + "type": [ + "string", + "null" + ] + }, + "dependencies": { + "type": [ + "array", + "null" + ], + "items": { + "$ref": "#/$defs/EncodablePackageId" + } + }, + "replace": { + "anyOf": [ + { + "$ref": "#/$defs/EncodablePackageId" + }, + { + "type": "null" + } + ] + } + }, + "required": [ + "name", + "version" + ] + }, + "EncodablePackageId": { + "type": "object", + "properties": { + "name": { + "type": "string" + }, + "version": { + "type": [ + "string", + "null" + ] + }, + "source": { + "type": [ + "string", + "null" + ] + } + }, + "required": [ + "name" + ] + }, + "Patch": { + "type": "object", + "properties": { + "unused": { + "type": "array", + "items": { + "$ref": "#/$defs/EncodableDependency" + } + } + }, + "required": [ + "unused" + ] + } + } +} \ No newline at end of file diff --git a/crates/cargo-util-schemas/src/lockfile.rs b/crates/cargo-util-schemas/src/lockfile.rs index 84bf4b0ac..ddcca0998 100644 --- a/crates/cargo-util-schemas/src/lockfile.rs +++ b/crates/cargo-util-schemas/src/lockfile.rs @@ -9,6 +9,7 @@ use crate::core::{GitReference, SourceKind}; /// The `Cargo.lock` structure. #[derive(Serialize, Deserialize, Debug)] +#[cfg_attr(feature = "unstable-schema", derive(schemars::JsonSchema))] pub struct EncodableResolve { pub version: Option, pub package: Option>, @@ -20,6 +21,7 @@ pub struct EncodableResolve { } #[derive(Serialize, Deserialize, Debug, Default)] +#[cfg_attr(feature = "unstable-schema", derive(schemars::JsonSchema))] pub struct Patch { pub unused: Vec, } @@ -33,6 +35,7 @@ impl Patch { } #[derive(Serialize, Deserialize, Debug, PartialOrd, Ord, PartialEq, Eq)] +#[cfg_attr(feature = "unstable-schema", derive(schemars::JsonSchema))] pub struct EncodableDependency { pub name: String, pub version: String, @@ -43,6 +46,11 @@ pub struct EncodableDependency { } #[derive(Debug, Clone)] +#[cfg_attr( + feature = "unstable-schema", + derive(schemars::JsonSchema), + schemars(with = "String") +)] pub struct EncodableSourceId { /// Full string of the source source_str: String, @@ -150,6 +158,7 @@ impl Ord for EncodableSourceId { } #[derive(Debug, PartialOrd, Ord, PartialEq, Eq, Hash, Clone)] +#[cfg_attr(feature = "unstable-schema", derive(schemars::JsonSchema))] pub struct EncodablePackageId { pub name: String, pub version: Option, @@ -253,3 +262,11 @@ enum EncodablePackageIdErrorKind { #[error(transparent)] Source(#[from] EncodableSourceIdError), } + +#[cfg(feature = "unstable-schema")] +#[test] +fn dump_lockfile_schema() { + let schema = schemars::schema_for!(crate::lockfile::EncodableResolve); + let dump = serde_json::to_string_pretty(&schema).unwrap(); + snapbox::assert_data_eq!(dump, snapbox::file!("../lockfile.schema.json").raw()); +}