Skip to content

Commit d32e1f9

Browse files
committed
refactor(toml): Make InheritableDependency::inhert_with a free function
1 parent e48befe commit d32e1f9

File tree

1 file changed

+87
-88
lines changed

1 file changed

+87
-88
lines changed

src/cargo/util/toml/mod.rs

Lines changed: 87 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -661,7 +661,7 @@ impl schema::TomlManifest {
661661

662662
let mut deps: BTreeMap<String, schema::InheritableDependency> = BTreeMap::new();
663663
for (n, v) in dependencies.iter() {
664-
let resolved = v.clone().inherit_with(n, inheritable, cx)?;
664+
let resolved = dependency_inherit_with(v.clone(), n, inheritable, cx)?;
665665
let dep = resolved.to_dependency(n, cx, kind)?;
666666
let name_in_toml = dep.name_in_toml().as_str();
667667
validate_package_name(name_in_toml, "dependency name", "")?;
@@ -1589,110 +1589,109 @@ fn lints_inherit_with(
15891589
}
15901590
}
15911591

1592-
impl schema::InheritableDependency {
1593-
fn inherit_with<'a>(
1594-
self,
1595-
name: &str,
1596-
inheritable: impl FnOnce() -> CargoResult<&'a InheritableFields>,
1597-
cx: &mut Context<'_, '_>,
1598-
) -> CargoResult<TomlDependency> {
1599-
match self {
1592+
fn dependency_inherit_with<'a>(
1593+
dependency: schema::InheritableDependency,
1594+
name: &str,
1595+
inheritable: impl FnOnce() -> CargoResult<&'a InheritableFields>,
1596+
cx: &mut Context<'_, '_>,
1597+
) -> CargoResult<TomlDependency> {
1598+
match dependency {
16001599
schema::InheritableDependency::Value(value) => Ok(value),
16011600
schema::InheritableDependency::Inherit(w) => {
1602-
w.inherit_with(name, inheritable, cx).with_context(|| {
1601+
inner_dependency_inherit_with(w, name, inheritable, cx).with_context(|| {
16031602
format!(
16041603
"error inheriting `{name}` from workspace root manifest's `workspace.dependencies.{name}`",
16051604
)
16061605
})
16071606
}
16081607
}
1609-
}
16101608
}
16111609

1612-
impl schema::TomlInheritedDependency {
1613-
fn inherit_with<'a>(
1614-
&self,
1615-
name: &str,
1616-
inheritable: impl FnOnce() -> CargoResult<&'a InheritableFields>,
1617-
cx: &mut Context<'_, '_>,
1618-
) -> CargoResult<schema::TomlDependency> {
1619-
fn default_features_msg(label: &str, ws_def_feat: Option<bool>, cx: &mut Context<'_, '_>) {
1620-
let ws_def_feat = match ws_def_feat {
1621-
Some(true) => "true",
1622-
Some(false) => "false",
1623-
None => "not specified",
1624-
};
1625-
cx.warnings.push(format!(
1626-
"`default-features` is ignored for {label}, since `default-features` was \
1610+
fn inner_dependency_inherit_with<'a>(
1611+
dependency: schema::TomlInheritedDependency,
1612+
name: &str,
1613+
inheritable: impl FnOnce() -> CargoResult<&'a InheritableFields>,
1614+
cx: &mut Context<'_, '_>,
1615+
) -> CargoResult<schema::TomlDependency> {
1616+
fn default_features_msg(label: &str, ws_def_feat: Option<bool>, cx: &mut Context<'_, '_>) {
1617+
let ws_def_feat = match ws_def_feat {
1618+
Some(true) => "true",
1619+
Some(false) => "false",
1620+
None => "not specified",
1621+
};
1622+
cx.warnings.push(format!(
1623+
"`default-features` is ignored for {label}, since `default-features` was \
16271624
{ws_def_feat} for `workspace.dependencies.{label}`, \
16281625
this could become a hard error in the future"
1629-
))
1630-
}
1631-
if self.default_features.is_some() && self.default_features2.is_some() {
1632-
warn_on_deprecated("default-features", name, "dependency", cx.warnings);
1633-
}
1634-
inheritable()?.get_dependency(name, cx.root).map(|d| {
1635-
match d {
1636-
schema::TomlDependency::Simple(s) => {
1637-
if let Some(false) = self.default_features() {
1638-
default_features_msg(name, None, cx);
1639-
}
1640-
if self.optional.is_some() || self.features.is_some() || self.public.is_some() {
1641-
schema::TomlDependency::Detailed(schema::TomlDetailedDependency {
1642-
version: Some(s),
1643-
optional: self.optional,
1644-
features: self.features.clone(),
1645-
public: self.public,
1646-
..Default::default()
1647-
})
1648-
} else {
1649-
schema::TomlDependency::Simple(s)
1650-
}
1626+
))
1627+
}
1628+
if dependency.default_features.is_some() && dependency.default_features2.is_some() {
1629+
warn_on_deprecated("default-features", name, "dependency", cx.warnings);
1630+
}
1631+
inheritable()?.get_dependency(name, cx.root).map(|d| {
1632+
match d {
1633+
schema::TomlDependency::Simple(s) => {
1634+
if let Some(false) = dependency.default_features() {
1635+
default_features_msg(name, None, cx);
16511636
}
1652-
schema::TomlDependency::Detailed(d) => {
1653-
let mut d = d.clone();
1654-
match (self.default_features(), d.default_features()) {
1655-
// member: default-features = true and
1656-
// workspace: default-features = false should turn on
1657-
// default-features
1658-
(Some(true), Some(false)) => {
1659-
d.default_features = Some(true);
1660-
}
1661-
// member: default-features = false and
1662-
// workspace: default-features = true should ignore member
1663-
// default-features
1664-
(Some(false), Some(true)) => {
1665-
default_features_msg(name, Some(true), cx);
1666-
}
1667-
// member: default-features = false and
1668-
// workspace: dep = "1.0" should ignore member default-features
1669-
(Some(false), None) => {
1670-
default_features_msg(name, None, cx);
1671-
}
1672-
_ => {}
1637+
if dependency.optional.is_some()
1638+
|| dependency.features.is_some()
1639+
|| dependency.public.is_some()
1640+
{
1641+
schema::TomlDependency::Detailed(schema::TomlDetailedDependency {
1642+
version: Some(s),
1643+
optional: dependency.optional,
1644+
features: dependency.features.clone(),
1645+
public: dependency.public,
1646+
..Default::default()
1647+
})
1648+
} else {
1649+
schema::TomlDependency::Simple(s)
1650+
}
1651+
}
1652+
schema::TomlDependency::Detailed(d) => {
1653+
let mut d = d.clone();
1654+
match (dependency.default_features(), d.default_features()) {
1655+
// member: default-features = true and
1656+
// workspace: default-features = false should turn on
1657+
// default-features
1658+
(Some(true), Some(false)) => {
1659+
d.default_features = Some(true);
1660+
}
1661+
// member: default-features = false and
1662+
// workspace: default-features = true should ignore member
1663+
// default-features
1664+
(Some(false), Some(true)) => {
1665+
default_features_msg(name, Some(true), cx);
16731666
}
1674-
// Inherit the workspace configuration for `public` unless
1675-
// it's explicitly specified for this dependency.
1676-
if let Some(public) = self.public {
1677-
d.public = Some(public);
1667+
// member: default-features = false and
1668+
// workspace: dep = "1.0" should ignore member default-features
1669+
(Some(false), None) => {
1670+
default_features_msg(name, None, cx);
16781671
}
1679-
d.features = match (d.features.clone(), self.features.clone()) {
1680-
(Some(dep_feat), Some(inherit_feat)) => Some(
1681-
dep_feat
1682-
.into_iter()
1683-
.chain(inherit_feat)
1684-
.collect::<Vec<String>>(),
1685-
),
1686-
(Some(dep_fet), None) => Some(dep_fet),
1687-
(None, Some(inherit_feat)) => Some(inherit_feat),
1688-
(None, None) => None,
1689-
};
1690-
d.optional = self.optional;
1691-
schema::TomlDependency::Detailed(d)
1672+
_ => {}
1673+
}
1674+
// Inherit the workspace configuration for `public` unless
1675+
// it's explicitly specified for this dependency.
1676+
if let Some(public) = dependency.public {
1677+
d.public = Some(public);
16921678
}
1679+
d.features = match (d.features.clone(), dependency.features.clone()) {
1680+
(Some(dep_feat), Some(inherit_feat)) => Some(
1681+
dep_feat
1682+
.into_iter()
1683+
.chain(inherit_feat)
1684+
.collect::<Vec<String>>(),
1685+
),
1686+
(Some(dep_fet), None) => Some(dep_fet),
1687+
(None, Some(inherit_feat)) => Some(inherit_feat),
1688+
(None, None) => None,
1689+
};
1690+
d.optional = dependency.optional;
1691+
schema::TomlDependency::Detailed(d)
16931692
}
1694-
})
1695-
}
1693+
}
1694+
})
16961695
}
16971696

16981697
impl<P: ResolveToPath + Clone> schema::TomlDependency<P> {

0 commit comments

Comments
 (0)