Skip to content

Commit 4e5af28

Browse files
committed
refactor(schema): Group TomlManifest fields to clarify requires_package
I found a bug in the manifest parser and figured this would help make it more obvious. Since I was already changing the order, I figure I'm make things a little more logical (user-facing first, implementtion details later)
1 parent 876ea2c commit 4e5af28

File tree

5 files changed

+50
-48
lines changed

5 files changed

+50
-48
lines changed

crates/cargo-util-schemas/src/manifest/mod.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,13 @@ use crate::schema::TomlValueWrapper;
3535
#[serde(rename_all = "kebab-case")]
3636
#[cfg_attr(feature = "unstable-schema", derive(schemars::JsonSchema))]
3737
pub struct TomlManifest {
38-
// when adding new fields, be sure to check whether `requires_package` should disallow them
3938
pub cargo_features: Option<Vec<String>>,
39+
40+
// Update `requires_package` when adding new package-specific fields
4041
pub package: Option<Box<TomlPackage>>,
4142
pub project: Option<Box<TomlPackage>>,
42-
pub profile: Option<TomlProfiles>,
43+
pub badges: Option<BTreeMap<String, BTreeMap<String, String>>>,
44+
pub features: Option<BTreeMap<FeatureName, Vec<String>>>,
4345
pub lib: Option<TomlLibTarget>,
4446
pub bin: Option<Vec<TomlBinTarget>>,
4547
pub example: Option<Vec<TomlExampleTarget>>,
@@ -52,14 +54,14 @@ pub struct TomlManifest {
5254
pub build_dependencies: Option<BTreeMap<PackageName, InheritableDependency>>,
5355
#[serde(rename = "build_dependencies")]
5456
pub build_dependencies2: Option<BTreeMap<PackageName, InheritableDependency>>,
55-
pub features: Option<BTreeMap<FeatureName, Vec<String>>>,
5657
pub target: Option<BTreeMap<String, TomlPlatform>>,
57-
pub replace: Option<BTreeMap<String, TomlDependency>>,
58-
pub patch: Option<BTreeMap<String, BTreeMap<PackageName, TomlDependency>>>,
59-
pub workspace: Option<TomlWorkspace>,
60-
pub badges: Option<BTreeMap<String, BTreeMap<String, String>>>,
6158
pub lints: Option<InheritableLints>,
6259

60+
pub workspace: Option<TomlWorkspace>,
61+
pub profile: Option<TomlProfiles>,
62+
pub patch: Option<BTreeMap<String, BTreeMap<PackageName, TomlDependency>>>,
63+
pub replace: Option<BTreeMap<String, TomlDependency>>,
64+
6365
/// Report unused keys (see also nested `_unused_keys`)
6466
/// Note: this is populated by the caller, rather than automatically
6567
#[serde(skip)]
@@ -69,6 +71,8 @@ pub struct TomlManifest {
6971
impl TomlManifest {
7072
pub fn requires_package(&self) -> impl Iterator<Item = &'static str> {
7173
[
74+
self.badges.as_ref().map(|_| "badges"),
75+
self.features.as_ref().map(|_| "features"),
7276
self.lib.as_ref().map(|_| "lib"),
7377
self.bin.as_ref().map(|_| "bin"),
7478
self.example.as_ref().map(|_| "example"),
@@ -79,9 +83,7 @@ impl TomlManifest {
7983
self.build_dependencies()
8084
.as_ref()
8185
.map(|_| "build-dependencies"),
82-
self.features.as_ref().map(|_| "features"),
8386
self.target.as_ref().map(|_| "target"),
84-
self.badges.as_ref().map(|_| "badges"),
8587
self.lints.as_ref().map(|_| "lints"),
8688
]
8789
.into_iter()

src/cargo/util/toml/mod.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,8 @@ fn normalize_toml(
279279
cargo_features: original_toml.cargo_features.clone(),
280280
package: None,
281281
project: None,
282-
profile: original_toml.profile.clone(),
282+
badges: None,
283+
features: None,
283284
lib: None,
284285
bin: None,
285286
example: None,
@@ -290,13 +291,12 @@ fn normalize_toml(
290291
dev_dependencies2: None,
291292
build_dependencies: None,
292293
build_dependencies2: None,
293-
features: None,
294294
target: None,
295-
replace: original_toml.replace.clone(),
296-
patch: None,
297-
workspace: original_toml.workspace.clone(),
298-
badges: None,
299295
lints: None,
296+
workspace: original_toml.workspace.clone(),
297+
profile: original_toml.profile.clone(),
298+
patch: None,
299+
replace: original_toml.replace.clone(),
300300
_unused_keys: Default::default(),
301301
};
302302

@@ -2820,9 +2820,11 @@ fn prepare_toml_for_publish(
28202820

28212821
let all = |_d: &manifest::TomlDependency| true;
28222822
let mut manifest = manifest::TomlManifest {
2823+
cargo_features: me.cargo_features.clone(),
28232824
package: Some(package),
28242825
project: None,
2825-
profile: me.profile.clone(),
2826+
badges: me.badges.clone(),
2827+
features: me.features.clone(),
28262828
lib,
28272829
bin,
28282830
example,
@@ -2837,7 +2839,6 @@ fn prepare_toml_for_publish(
28372839
dev_dependencies2: None,
28382840
build_dependencies: map_deps(gctx, me.build_dependencies(), all)?,
28392841
build_dependencies2: None,
2840-
features: me.features.clone(),
28412842
target: match me.target.as_ref().map(|target_map| {
28422843
target_map
28432844
.iter()
@@ -2863,12 +2864,11 @@ fn prepare_toml_for_publish(
28632864
Some(Err(e)) => return Err(e),
28642865
None => None,
28652866
},
2866-
replace: None,
2867-
patch: None,
2868-
workspace: None,
2869-
badges: me.badges.clone(),
2870-
cargo_features: me.cargo_features.clone(),
28712867
lints: me.lints.clone(),
2868+
workspace: None,
2869+
profile: me.profile.clone(),
2870+
patch: None,
2871+
replace: None,
28722872
_unused_keys: Default::default(),
28732873
};
28742874
strip_features(&mut manifest);

tests/testsuite/features_namespaced.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,6 +1006,9 @@ homepage = "https://example.com/"
10061006
readme = false
10071007
license = "MIT"
10081008
1009+
[features]
1010+
feat = ["opt-dep1"]
1011+
10091012
[lib]
10101013
name = "foo"
10111014
path = "src/lib.rs"
@@ -1018,9 +1021,6 @@ optional = true
10181021
version = "1.0"
10191022
optional = true
10201023
1021-
[features]
1022-
feat = ["opt-dep1"]
1023-
10241024
"##]],
10251025
)],
10261026
);
@@ -1143,6 +1143,11 @@ homepage = "https://example.com/"
11431143
readme = false
11441144
license = "MIT"
11451145
1146+
[features]
1147+
feat1 = []
1148+
feat2 = ["dep:bar"]
1149+
feat3 = ["feat2"]
1150+
11461151
[lib]
11471152
name = "foo"
11481153
path = "src/lib.rs"
@@ -1151,11 +1156,6 @@ path = "src/lib.rs"
11511156
version = "1.0"
11521157
optional = true
11531158
1154-
[features]
1155-
feat1 = []
1156-
feat2 = ["dep:bar"]
1157-
feat3 = ["feat2"]
1158-
11591159
"##]],
11601160
)],
11611161
);

tests/testsuite/publish.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2015,6 +2015,20 @@ readme = false
20152015
license = "MIT"
20162016
repository = "foo"
20172017
2018+
[features]
2019+
foo_feature = [
2020+
"normal-only/cat",
2021+
"build-only/cat",
2022+
"normal-and-dev/cat",
2023+
"target-normal-only/cat",
2024+
"target-build-only/cat",
2025+
"target-normal-and-dev/cat",
2026+
"optional-dep-feature/cat",
2027+
"dep:optional-namespaced",
2028+
"optional-renamed-dep-feature10/cat",
2029+
"dep:optional-renamed-namespaced10",
2030+
]
2031+
20182032
[[bin]]
20192033
name = "foo"
20202034
path = "src/main.rs"
@@ -2057,20 +2071,6 @@ features = ["cat"]
20572071
version = "1.0"
20582072
features = ["cat"]
20592073
2060-
[features]
2061-
foo_feature = [
2062-
"normal-only/cat",
2063-
"build-only/cat",
2064-
"normal-and-dev/cat",
2065-
"target-normal-only/cat",
2066-
"target-build-only/cat",
2067-
"target-normal-and-dev/cat",
2068-
"optional-dep-feature/cat",
2069-
"dep:optional-namespaced",
2070-
"optional-renamed-dep-feature10/cat",
2071-
"dep:optional-renamed-namespaced10",
2072-
]
2073-
20742074
[target."cfg(unix)".dependencies.target-normal-and-dev]
20752075
version = "1.0"
20762076
features = ["cat"]

tests/testsuite/weak_dep_features.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,10 @@ homepage = "https://example.com/"
650650
readme = false
651651
license = "MIT"
652652
653+
[features]
654+
feat1 = []
655+
feat2 = ["bar?/feat"]
656+
653657
[lib]
654658
name = "foo"
655659
path = "src/lib.rs"
@@ -658,10 +662,6 @@ path = "src/lib.rs"
658662
version = "1.0"
659663
optional = true
660664
661-
[features]
662-
feat1 = []
663-
feat2 = ["bar?/feat"]
664-
665665
"##]],
666666
)],
667667
);

0 commit comments

Comments
 (0)