Skip to content

Commit ee515e6

Browse files
authored
docs: Add Lockfile schemas docs (#15989)
### What does this PR try to resolve? Add documentation efforts to the lockfile schemas. Continuation of #15980 ### How to test and review this PR?
2 parents 5818dcd + 9730a37 commit ee515e6

File tree

2 files changed

+88
-15
lines changed

2 files changed

+88
-15
lines changed

crates/cargo-util-schemas/lockfile.schema.json

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
{
22
"$schema": "https://json-schema.org/draft/2020-12/schema",
33
"title": "TomlLockfile",
4-
"description": "The `Cargo.lock` structure.",
4+
"description": "Serialization of `Cargo.lock`",
55
"type": "object",
66
"properties": {
77
"version": {
8+
"description": "The lockfile format version (`version =` field).\n\nThis field is optional for backward compatibility. Older lockfiles, i.e. V1 and V2, does\nnot have the version field serialized.",
89
"type": [
910
"integer",
1011
"null"
@@ -13,6 +14,7 @@
1314
"minimum": 0
1415
},
1516
"package": {
17+
"description": "The list of `[[package]]` entries describing each resolved dependency.",
1618
"type": [
1719
"array",
1820
"null"
@@ -22,7 +24,7 @@
2224
}
2325
},
2426
"root": {
25-
"description": "`root` is optional to allow backward compatibility.",
27+
"description": "The `[root]` table describing the root package.\n\nThis field is optional for backward compatibility. Older lockfiles have the root package\nseparated, whereas newer lockfiles have the root package as part of `[[package]]`.",
2628
"anyOf": [
2729
{
2830
"$ref": "#/$defs/TomlLockfileDependency"
@@ -33,6 +35,7 @@
3335
]
3436
},
3537
"metadata": {
38+
"description": "The `[metadata]` table\n\n\nIn older lockfile versions, dependency checksums were stored here instead of alongside each\npackage entry.",
3639
"type": [
3740
"object",
3841
"null"
@@ -42,32 +45,43 @@
4245
}
4346
},
4447
"patch": {
48+
"description": "The `[patch]` table describing unused patches.\n\nThe lockfile stores them as a list of `[[patch.unused]]` entries.",
4549
"$ref": "#/$defs/TomlLockfilePatch"
4650
}
4751
},
4852
"$defs": {
4953
"TomlLockfileDependency": {
54+
"description": "Serialization of lockfiles dependencies",
5055
"type": "object",
5156
"properties": {
5257
"name": {
58+
"description": "The name of the dependency.",
5359
"type": "string"
5460
},
5561
"version": {
62+
"description": "The version of the dependency.",
5663
"type": "string"
5764
},
5865
"source": {
59-
"type": [
60-
"string",
61-
"null"
66+
"description": "The source of the dependency.\n\nCargo does not serialize path dependencies.",
67+
"anyOf": [
68+
{
69+
"$ref": "#/$defs/TomlLockfileSourceId"
70+
},
71+
{
72+
"type": "null"
73+
}
6274
]
6375
},
6476
"checksum": {
77+
"description": "The checksum of the dependency.\n\nIn older lockfiles, checksums were not stored here and instead on a separate `[metadata]`\ntable (see [`TomlLockfileMetadata`]).",
6578
"type": [
6679
"string",
6780
"null"
6881
]
6982
},
7083
"dependencies": {
84+
"description": "The transitive dependencies used by this dependency.",
7185
"type": [
7286
"array",
7387
"null"
@@ -77,6 +91,7 @@
7791
}
7892
},
7993
"replace": {
94+
"description": "The replace of the dependency.",
8095
"anyOf": [
8196
{
8297
"$ref": "#/$defs/TomlLockfilePackageId"
@@ -92,7 +107,12 @@
92107
"version"
93108
]
94109
},
110+
"TomlLockfileSourceId": {
111+
"description": "Serialization of dependency's source",
112+
"type": "string"
113+
},
95114
"TomlLockfilePackageId": {
115+
"description": "Serialization of package IDs.\n\nThe version and source are only included when necessary to disambiguate between packages:\n- If multiple packages share the same name, the version is included.\n- If multiple packages share the same name and version, the source is included.",
96116
"type": "object",
97117
"properties": {
98118
"name": {
@@ -105,9 +125,13 @@
105125
]
106126
},
107127
"source": {
108-
"type": [
109-
"string",
110-
"null"
128+
"anyOf": [
129+
{
130+
"$ref": "#/$defs/TomlLockfileSourceId"
131+
},
132+
{
133+
"type": "null"
134+
}
111135
]
112136
}
113137
},
@@ -116,9 +140,11 @@
116140
]
117141
},
118142
"TomlLockfilePatch": {
143+
"description": "Serialization of unused patches\n\nCargo stores patches that were declared but not used during resolution.",
119144
"type": "object",
120145
"properties": {
121146
"unused": {
147+
"description": "The list of unused dependency patches.",
122148
"type": "array",
123149
"items": {
124150
"$ref": "#/$defs/TomlLockfileDependency"

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

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! `Cargo.lock` / Lockfile schema definition
2+
13
use std::collections::BTreeMap;
24
use std::fmt;
35
use std::{cmp::Ordering, str::FromStr};
@@ -7,56 +9,96 @@ use url::Url;
79

810
use crate::core::{GitReference, SourceKind};
911

10-
/// The `Cargo.lock` structure.
12+
/// Serialization of `Cargo.lock`
1113
#[derive(Serialize, Deserialize, Debug)]
1214
#[cfg_attr(feature = "unstable-schema", derive(schemars::JsonSchema))]
1315
pub struct TomlLockfile {
16+
/// The lockfile format version (`version =` field).
17+
///
18+
/// This field is optional for backward compatibility. Older lockfiles, i.e. V1 and V2, does
19+
/// not have the version field serialized.
1420
pub version: Option<u32>,
21+
/// The list of `[[package]]` entries describing each resolved dependency.
1522
pub package: Option<Vec<TomlLockfileDependency>>,
16-
/// `root` is optional to allow backward compatibility.
23+
/// The `[root]` table describing the root package.
24+
///
25+
/// This field is optional for backward compatibility. Older lockfiles have the root package
26+
/// separated, whereas newer lockfiles have the root package as part of `[[package]]`.
1727
pub root: Option<TomlLockfileDependency>,
28+
/// The `[metadata]` table
29+
///
30+
///
31+
/// In older lockfile versions, dependency checksums were stored here instead of alongside each
32+
/// package entry.
1833
pub metadata: Option<TomlLockfileMetadata>,
34+
/// The `[patch]` table describing unused patches.
35+
///
36+
/// The lockfile stores them as a list of `[[patch.unused]]` entries.
1937
#[serde(default, skip_serializing_if = "TomlLockfilePatch::is_empty")]
2038
pub patch: TomlLockfilePatch,
2139
}
2240

41+
/// Serialization of lockfiles metadata
42+
///
43+
/// Older versions of lockfiles have their dependencies' checksums on this `[metadata]` table.
44+
pub type TomlLockfileMetadata = BTreeMap<String, String>;
45+
46+
/// Serialization of unused patches
47+
///
48+
/// Cargo stores patches that were declared but not used during resolution.
2349
#[derive(Serialize, Deserialize, Debug, Default)]
2450
#[cfg_attr(feature = "unstable-schema", derive(schemars::JsonSchema))]
2551
pub struct TomlLockfilePatch {
52+
/// The list of unused dependency patches.
2653
pub unused: Vec<TomlLockfileDependency>,
2754
}
2855

29-
pub type TomlLockfileMetadata = BTreeMap<String, String>;
30-
3156
impl TomlLockfilePatch {
3257
fn is_empty(&self) -> bool {
3358
self.unused.is_empty()
3459
}
3560
}
3661

62+
/// Serialization of lockfiles dependencies
3763
#[derive(Serialize, Deserialize, Debug, PartialOrd, Ord, PartialEq, Eq)]
3864
#[cfg_attr(feature = "unstable-schema", derive(schemars::JsonSchema))]
3965
pub struct TomlLockfileDependency {
66+
/// The name of the dependency.
4067
pub name: String,
68+
/// The version of the dependency.
4169
pub version: String,
70+
/// The source of the dependency.
71+
///
72+
/// Cargo does not serialize path dependencies.
4273
pub source: Option<TomlLockfileSourceId>,
74+
/// The checksum of the dependency.
75+
///
76+
/// In older lockfiles, checksums were not stored here and instead on a separate `[metadata]`
77+
/// table (see [`TomlLockfileMetadata`]).
4378
pub checksum: Option<String>,
79+
/// The transitive dependencies used by this dependency.
4480
pub dependencies: Option<Vec<TomlLockfilePackageId>>,
81+
/// The replace of the dependency.
4582
pub replace: Option<TomlLockfilePackageId>,
4683
}
4784

85+
/// Serialization of dependency's source
4886
#[derive(Debug, Clone)]
4987
#[cfg_attr(
5088
feature = "unstable-schema",
5189
derive(schemars::JsonSchema),
5290
schemars(with = "String")
5391
)]
5492
pub struct TomlLockfileSourceId {
55-
/// Full string of the source
93+
/// The string representation of the source as it appears in the lockfile.
5694
source_str: String,
57-
/// Used for sources ordering
95+
/// The parsed source type, e.g. `git`, `registry`.
96+
///
97+
/// Used for sources ordering.
5898
kind: SourceKind,
59-
/// Used for sources ordering
99+
/// The parsed URL of the source.
100+
///
101+
/// Used for sources ordering.
60102
url: Url,
61103
}
62104

@@ -157,6 +199,11 @@ impl Ord for TomlLockfileSourceId {
157199
}
158200
}
159201

202+
/// Serialization of package IDs.
203+
///
204+
/// The version and source are only included when necessary to disambiguate between packages:
205+
/// - If multiple packages share the same name, the version is included.
206+
/// - If multiple packages share the same name and version, the source is included.
160207
#[derive(Debug, PartialOrd, Ord, PartialEq, Eq, Hash, Clone)]
161208
#[cfg_attr(feature = "unstable-schema", derive(schemars::JsonSchema))]
162209
pub struct TomlLockfilePackageId {

0 commit comments

Comments
 (0)