feat: add lockfile schema generation

This commit is contained in:
Vito Secona 2025-09-19 05:36:02 +07:00
parent 949e863cb9
commit 3738481c5b
2 changed files with 150 additions and 0 deletions

View File

@ -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"
]
}
}
}

View File

@ -9,6 +9,7 @@ use crate::core::{GitReference, SourceKind};
/// The `Cargo.lock` structure. /// The `Cargo.lock` structure.
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
#[cfg_attr(feature = "unstable-schema", derive(schemars::JsonSchema))]
pub struct EncodableResolve { pub struct EncodableResolve {
pub version: Option<u32>, pub version: Option<u32>,
pub package: Option<Vec<EncodableDependency>>, pub package: Option<Vec<EncodableDependency>>,
@ -20,6 +21,7 @@ pub struct EncodableResolve {
} }
#[derive(Serialize, Deserialize, Debug, Default)] #[derive(Serialize, Deserialize, Debug, Default)]
#[cfg_attr(feature = "unstable-schema", derive(schemars::JsonSchema))]
pub struct Patch { pub struct Patch {
pub unused: Vec<EncodableDependency>, pub unused: Vec<EncodableDependency>,
} }
@ -33,6 +35,7 @@ impl Patch {
} }
#[derive(Serialize, Deserialize, Debug, PartialOrd, Ord, PartialEq, Eq)] #[derive(Serialize, Deserialize, Debug, PartialOrd, Ord, PartialEq, Eq)]
#[cfg_attr(feature = "unstable-schema", derive(schemars::JsonSchema))]
pub struct EncodableDependency { pub struct EncodableDependency {
pub name: String, pub name: String,
pub version: String, pub version: String,
@ -43,6 +46,11 @@ pub struct EncodableDependency {
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
#[cfg_attr(
feature = "unstable-schema",
derive(schemars::JsonSchema),
schemars(with = "String")
)]
pub struct EncodableSourceId { pub struct EncodableSourceId {
/// Full string of the source /// Full string of the source
source_str: String, source_str: String,
@ -150,6 +158,7 @@ impl Ord for EncodableSourceId {
} }
#[derive(Debug, PartialOrd, Ord, PartialEq, Eq, Hash, Clone)] #[derive(Debug, PartialOrd, Ord, PartialEq, Eq, Hash, Clone)]
#[cfg_attr(feature = "unstable-schema", derive(schemars::JsonSchema))]
pub struct EncodablePackageId { pub struct EncodablePackageId {
pub name: String, pub name: String,
pub version: Option<String>, pub version: Option<String>,
@ -253,3 +262,11 @@ enum EncodablePackageIdErrorKind {
#[error(transparent)] #[error(transparent)]
Source(#[from] EncodableSourceIdError), 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());
}