Skip to content

Commit 3738481

Browse files
committed
feat: add lockfile schema generation
1 parent 949e863 commit 3738481

File tree

2 files changed

+150
-0
lines changed

2 files changed

+150
-0
lines changed
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
{
2+
"$schema": "https://json-schema.org/draft/2020-12/schema",
3+
"title": "EncodableResolve",
4+
"description": "The `Cargo.lock` structure.",
5+
"type": "object",
6+
"properties": {
7+
"version": {
8+
"type": [
9+
"integer",
10+
"null"
11+
],
12+
"format": "uint32",
13+
"minimum": 0
14+
},
15+
"package": {
16+
"type": [
17+
"array",
18+
"null"
19+
],
20+
"items": {
21+
"$ref": "#/$defs/EncodableDependency"
22+
}
23+
},
24+
"root": {
25+
"description": "`root` is optional to allow backward compatibility.",
26+
"anyOf": [
27+
{
28+
"$ref": "#/$defs/EncodableDependency"
29+
},
30+
{
31+
"type": "null"
32+
}
33+
]
34+
},
35+
"metadata": {
36+
"type": [
37+
"object",
38+
"null"
39+
],
40+
"additionalProperties": {
41+
"type": "string"
42+
}
43+
},
44+
"patch": {
45+
"$ref": "#/$defs/Patch"
46+
}
47+
},
48+
"$defs": {
49+
"EncodableDependency": {
50+
"type": "object",
51+
"properties": {
52+
"name": {
53+
"type": "string"
54+
},
55+
"version": {
56+
"type": "string"
57+
},
58+
"source": {
59+
"type": [
60+
"string",
61+
"null"
62+
]
63+
},
64+
"checksum": {
65+
"type": [
66+
"string",
67+
"null"
68+
]
69+
},
70+
"dependencies": {
71+
"type": [
72+
"array",
73+
"null"
74+
],
75+
"items": {
76+
"$ref": "#/$defs/EncodablePackageId"
77+
}
78+
},
79+
"replace": {
80+
"anyOf": [
81+
{
82+
"$ref": "#/$defs/EncodablePackageId"
83+
},
84+
{
85+
"type": "null"
86+
}
87+
]
88+
}
89+
},
90+
"required": [
91+
"name",
92+
"version"
93+
]
94+
},
95+
"EncodablePackageId": {
96+
"type": "object",
97+
"properties": {
98+
"name": {
99+
"type": "string"
100+
},
101+
"version": {
102+
"type": [
103+
"string",
104+
"null"
105+
]
106+
},
107+
"source": {
108+
"type": [
109+
"string",
110+
"null"
111+
]
112+
}
113+
},
114+
"required": [
115+
"name"
116+
]
117+
},
118+
"Patch": {
119+
"type": "object",
120+
"properties": {
121+
"unused": {
122+
"type": "array",
123+
"items": {
124+
"$ref": "#/$defs/EncodableDependency"
125+
}
126+
}
127+
},
128+
"required": [
129+
"unused"
130+
]
131+
}
132+
}
133+
}

crates/cargo-util-schemas/src/lockfile.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::core::{GitReference, SourceKind};
99

1010
/// The `Cargo.lock` structure.
1111
#[derive(Serialize, Deserialize, Debug)]
12+
#[cfg_attr(feature = "unstable-schema", derive(schemars::JsonSchema))]
1213
pub struct EncodableResolve {
1314
pub version: Option<u32>,
1415
pub package: Option<Vec<EncodableDependency>>,
@@ -20,6 +21,7 @@ pub struct EncodableResolve {
2021
}
2122

2223
#[derive(Serialize, Deserialize, Debug, Default)]
24+
#[cfg_attr(feature = "unstable-schema", derive(schemars::JsonSchema))]
2325
pub struct Patch {
2426
pub unused: Vec<EncodableDependency>,
2527
}
@@ -33,6 +35,7 @@ impl Patch {
3335
}
3436

3537
#[derive(Serialize, Deserialize, Debug, PartialOrd, Ord, PartialEq, Eq)]
38+
#[cfg_attr(feature = "unstable-schema", derive(schemars::JsonSchema))]
3639
pub struct EncodableDependency {
3740
pub name: String,
3841
pub version: String,
@@ -43,6 +46,11 @@ pub struct EncodableDependency {
4346
}
4447

4548
#[derive(Debug, Clone)]
49+
#[cfg_attr(
50+
feature = "unstable-schema",
51+
derive(schemars::JsonSchema),
52+
schemars(with = "String")
53+
)]
4654
pub struct EncodableSourceId {
4755
/// Full string of the source
4856
source_str: String,
@@ -150,6 +158,7 @@ impl Ord for EncodableSourceId {
150158
}
151159

152160
#[derive(Debug, PartialOrd, Ord, PartialEq, Eq, Hash, Clone)]
161+
#[cfg_attr(feature = "unstable-schema", derive(schemars::JsonSchema))]
153162
pub struct EncodablePackageId {
154163
pub name: String,
155164
pub version: Option<String>,
@@ -253,3 +262,11 @@ enum EncodablePackageIdErrorKind {
253262
#[error(transparent)]
254263
Source(#[from] EncodableSourceIdError),
255264
}
265+
266+
#[cfg(feature = "unstable-schema")]
267+
#[test]
268+
fn dump_lockfile_schema() {
269+
let schema = schemars::schema_for!(crate::lockfile::EncodableResolve);
270+
let dump = serde_json::to_string_pretty(&schema).unwrap();
271+
snapbox::assert_data_eq!(dump, snapbox::file!("../lockfile.schema.json").raw());
272+
}

0 commit comments

Comments
 (0)