refactor(toml): Better abstract inheritance details

There is no reason that the caller for inheriting needs to know the
details of the inner types doing inheritance.
This commit is contained in:
Ed Page 2023-11-21 20:41:31 -06:00
parent 71cd3a926f
commit 7bd5aa3fe8

View file

@ -661,9 +661,7 @@ impl schema::TomlManifest {
let mut deps: BTreeMap<String, schema::InheritableDependency> = BTreeMap::new();
for (n, v) in dependencies.iter() {
let resolved = v
.clone()
.inherit_with(n, |dep| dep.inherit_with(n, inheritable, cx))?;
let resolved = v.clone().inherit_with(n, inheritable, cx)?;
let dep = resolved.to_dependency(n, cx, kind)?;
let name_in_toml = dep.name_in_toml().as_str();
validate_package_name(name_in_toml, "dependency name", "")?;
@ -1573,15 +1571,16 @@ impl<T> schema::InheritableField<T> {
impl schema::InheritableDependency {
fn inherit_with<'a>(
self,
label: &str,
get_ws_inheritable: impl FnOnce(&schema::TomlInheritedDependency) -> CargoResult<TomlDependency>,
name: &str,
inheritable: impl FnOnce() -> CargoResult<&'a InheritableFields>,
cx: &mut Context<'_, '_>,
) -> CargoResult<TomlDependency> {
match self {
schema::InheritableDependency::Value(value) => Ok(value),
schema::InheritableDependency::Inherit(w) => {
get_ws_inheritable(&w).with_context(|| {
w.inherit_with(name, inheritable, cx).with_context(|| {
format!(
"error inheriting `{label}` from workspace root manifest's `workspace.dependencies.{label}`",
"error inheriting `{name}` from workspace root manifest's `workspace.dependencies.{name}`",
)
})
}