Skip to content

Commit 3047a41

Browse files
committed
refactor(toml): Make InheritableField::inherit_with a free function
1 parent 3cb3130 commit 3047a41

File tree

1 file changed

+28
-31
lines changed

1 file changed

+28
-31
lines changed

src/cargo/util/toml/mod.rs

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ impl schema::TomlManifest {
503503
let version = package
504504
.version
505505
.clone()
506-
.map(|version| version.inherit_with("version", || inherit()?.version()))
506+
.map(|version| field_inherit_with(version, "version", || inherit()?.version()))
507507
.transpose()?;
508508

509509
package.version = version.clone().map(schema::InheritableField::Value);
@@ -517,8 +517,7 @@ impl schema::TomlManifest {
517517
);
518518

519519
let edition = if let Some(edition) = package.edition.clone() {
520-
let edition: Edition = edition
521-
.inherit_with("edition", || inherit()?.edition())?
520+
let edition: Edition = field_inherit_with(edition, "edition", || inherit()?.edition())?
522521
.parse()
523522
.with_context(|| "failed to parse the `edition` key")?;
524523
package.edition = Some(schema::InheritableField::Value(edition.to_string()));
@@ -543,9 +542,9 @@ impl schema::TomlManifest {
543542
}
544543

545544
let rust_version = if let Some(rust_version) = &package.rust_version {
546-
let rust_version = rust_version
547-
.clone()
548-
.inherit_with("rust_version", || inherit()?.rust_version())?;
545+
let rust_version = field_inherit_with(rust_version.clone(), "rust_version", || {
546+
inherit()?.rust_version()
547+
})?;
549548
let req = rust_version.to_caret_req();
550549
if let Some(first_version) = edition.first_version() {
551550
let unsupported =
@@ -800,13 +799,13 @@ impl schema::TomlManifest {
800799
let exclude = package
801800
.exclude
802801
.clone()
803-
.map(|mw| mw.inherit_with("exclude", || inherit()?.exclude()))
802+
.map(|mw| field_inherit_with(mw, "exclude", || inherit()?.exclude()))
804803
.transpose()?
805804
.unwrap_or_default();
806805
let include = package
807806
.include
808807
.clone()
809-
.map(|mw| mw.inherit_with("include", || inherit()?.include()))
808+
.map(|mw| field_inherit_with(mw, "include", || inherit()?.include()))
810809
.transpose()?
811810
.unwrap_or_default();
812811
let empty_features = BTreeMap::new();
@@ -833,70 +832,72 @@ impl schema::TomlManifest {
833832
description: package
834833
.description
835834
.clone()
836-
.map(|mw| mw.inherit_with("description", || inherit()?.description()))
835+
.map(|mw| field_inherit_with(mw, "description", || inherit()?.description()))
837836
.transpose()?,
838837
homepage: package
839838
.homepage
840839
.clone()
841-
.map(|mw| mw.inherit_with("homepage", || inherit()?.homepage()))
840+
.map(|mw| field_inherit_with(mw, "homepage", || inherit()?.homepage()))
842841
.transpose()?,
843842
documentation: package
844843
.documentation
845844
.clone()
846-
.map(|mw| mw.inherit_with("documentation", || inherit()?.documentation()))
845+
.map(|mw| field_inherit_with(mw, "documentation", || inherit()?.documentation()))
847846
.transpose()?,
848847
readme: readme_for_package(
849848
package_root,
850849
package
851850
.readme
852851
.clone()
853-
.map(|mw| mw.inherit_with("readme", || inherit()?.readme(package_root)))
852+
.map(|mw| field_inherit_with(mw, "readme", || inherit()?.readme(package_root)))
854853
.transpose()?
855854
.as_ref(),
856855
),
857856
authors: package
858857
.authors
859858
.clone()
860-
.map(|mw| mw.inherit_with("authors", || inherit()?.authors()))
859+
.map(|mw| field_inherit_with(mw, "authors", || inherit()?.authors()))
861860
.transpose()?
862861
.unwrap_or_default(),
863862
license: package
864863
.license
865864
.clone()
866-
.map(|mw| mw.inherit_with("license", || inherit()?.license()))
865+
.map(|mw| field_inherit_with(mw, "license", || inherit()?.license()))
867866
.transpose()?,
868867
license_file: package
869868
.license_file
870869
.clone()
871-
.map(|mw| mw.inherit_with("license", || inherit()?.license_file(package_root)))
870+
.map(|mw| {
871+
field_inherit_with(mw, "license", || inherit()?.license_file(package_root))
872+
})
872873
.transpose()?,
873874
repository: package
874875
.repository
875876
.clone()
876-
.map(|mw| mw.inherit_with("repository", || inherit()?.repository()))
877+
.map(|mw| field_inherit_with(mw, "repository", || inherit()?.repository()))
877878
.transpose()?,
878879
keywords: package
879880
.keywords
880881
.clone()
881-
.map(|mw| mw.inherit_with("keywords", || inherit()?.keywords()))
882+
.map(|mw| field_inherit_with(mw, "keywords", || inherit()?.keywords()))
882883
.transpose()?
883884
.unwrap_or_default(),
884885
categories: package
885886
.categories
886887
.clone()
887-
.map(|mw| mw.inherit_with("categories", || inherit()?.categories()))
888+
.map(|mw| field_inherit_with(mw, "categories", || inherit()?.categories()))
888889
.transpose()?
889890
.unwrap_or_default(),
890891
badges: me
891892
.badges
892893
.clone()
893-
.map(|mw| mw.inherit_with("badges", || inherit()?.badges()))
894+
.map(|mw| field_inherit_with(mw, "badges", || inherit()?.badges()))
894895
.transpose()?
895896
.unwrap_or_default(),
896897
links: package.links.clone(),
897898
rust_version: package
898899
.rust_version
899-
.map(|mw| mw.inherit_with("rust-version", || inherit()?.rust_version()))
900+
.map(|mw| field_inherit_with(mw, "rust-version", || inherit()?.rust_version()))
900901
.transpose()?,
901902
};
902903
package.description = metadata
@@ -958,9 +959,7 @@ impl schema::TomlManifest {
958959
}
959960

960961
let publish = package.publish.clone().map(|publish| {
961-
publish
962-
.inherit_with("publish", || inherit()?.publish())
963-
.unwrap()
962+
field_inherit_with(publish, "publish", || inherit()?.publish()).unwrap()
964963
});
965964

966965
package.publish = publish.clone().map(|p| schema::InheritableField::Value(p));
@@ -1559,21 +1558,19 @@ impl InheritableFields {
15591558
}
15601559
}
15611560

1562-
impl<T> schema::InheritableField<T> {
1563-
fn inherit_with<'a>(
1564-
self,
1565-
label: &str,
1566-
get_ws_inheritable: impl FnOnce() -> CargoResult<T>,
1567-
) -> CargoResult<T> {
1568-
match self {
1561+
fn field_inherit_with<'a, T>(
1562+
field: schema::InheritableField<T>,
1563+
label: &str,
1564+
get_ws_inheritable: impl FnOnce() -> CargoResult<T>,
1565+
) -> CargoResult<T> {
1566+
match field {
15691567
schema::InheritableField::Value(value) => Ok(value),
15701568
schema::InheritableField::Inherit(_) => get_ws_inheritable().with_context(|| {
15711569
format!(
15721570
"error inheriting `{label}` from workspace root manifest's `workspace.package.{label}`",
15731571
)
15741572
}),
15751573
}
1576-
}
15771574
}
15781575

15791576
impl schema::InheritableLints {

0 commit comments

Comments
 (0)