Skip to content

Commit

Permalink
Extract finding a workspace dependency into its own function
Browse files Browse the repository at this point in the history
  • Loading branch information
Muscraft committed Apr 26, 2022
1 parent 7b64cb8 commit 4420152
Showing 1 changed file with 25 additions and 25 deletions.
50 changes: 25 additions & 25 deletions src/cargo/ops/cargo_add/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -543,31 +543,7 @@ fn populate_available_features(
let query = dependency.query(config)?;
let query = match query {
MaybeWorkspace::Workspace(_workspace) => {
let manifest = LocalManifest::try_new(ws.root_manifest())?;
let manifest = manifest
.data
.as_item()
.as_table_like()
.context("could not make `manifest.data` into a table")?;
let workspace = manifest
.get("workspace")
.context("could not find `workspace`")?
.as_table_like()
.context("could not make `manifest.data.workspace` into a table")?;
let dependencies = workspace
.get("dependencies")
.context("could not find `dependencies` table in `workspace`")?
.as_table_like()
.context("could not make `dependencies` into a table")?;
let dep_item = dependencies.get(dependency.toml_key()).context(format!(
"could not find {} in `workspace.dependencies`",
dependency.toml_key()
))?;
let dep = Dependency::from_toml(
ws.root_manifest().parent().unwrap(),
dependency.toml_key(),
dep_item,
)?;
let dep = find_workspace_dep(dependency.toml_key(), ws.root_manifest())?;
if let Some(features) = dep.features.clone() {
dependency = dependency.set_inherited_features(features);
}
Expand Down Expand Up @@ -712,3 +688,27 @@ fn is_sorted(mut it: impl Iterator<Item = impl PartialOrd>) -> bool {

true
}

fn find_workspace_dep(toml_key: &str, root_manifest: &Path) -> CargoResult<Dependency> {
let manifest = LocalManifest::try_new(root_manifest)?;
let manifest = manifest
.data
.as_item()
.as_table_like()
.context("could not make `manifest.data` into a table")?;
let workspace = manifest
.get("workspace")
.context("could not find `workspace`")?
.as_table_like()
.context("could not make `manifest.data.workspace` into a table")?;
let dependencies = workspace
.get("dependencies")
.context("could not find `dependencies` table in `workspace`")?
.as_table_like()
.context("could not make `dependencies` into a table")?;
let dep_item = dependencies.get(toml_key).context(format!(
"could not find {} in `workspace.dependencies`",
toml_key
))?;
Dependency::from_toml(root_manifest.parent().unwrap(), toml_key, dep_item)
}

0 comments on commit 4420152

Please sign in to comment.