From 518d39cd2f1fde997b01acffe74e4a83fc906f86 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Mon, 24 May 2021 18:31:43 +0200 Subject: Duplicate dependencies that have multiple DepKinds --- crates/project_model/src/cargo_workspace.rs | 36 +++++++++++++++++------------ 1 file changed, 21 insertions(+), 15 deletions(-) (limited to 'crates/project_model/src/cargo_workspace.rs') diff --git a/crates/project_model/src/cargo_workspace.rs b/crates/project_model/src/cargo_workspace.rs index ad705c752..a8fee4f08 100644 --- a/crates/project_model/src/cargo_workspace.rs +++ b/crates/project_model/src/cargo_workspace.rs @@ -121,7 +121,7 @@ pub struct PackageDependency { pub kind: DepKind, } -#[derive(Debug, Clone, Eq, PartialEq)] +#[derive(Debug, Clone, Eq, PartialEq, PartialOrd, Ord)] pub enum DepKind { /// Available to the library, binary, and dev targets in the package (but not the build script). Normal, @@ -132,17 +132,23 @@ pub enum DepKind { } impl DepKind { - fn new(list: &[cargo_metadata::DepKindInfo]) -> Self { + fn iter(list: &[cargo_metadata::DepKindInfo]) -> impl Iterator + '_ { + let mut dep_kinds = Vec::new(); + if list.is_empty() { + dep_kinds.push(Self::Normal); + } for info in list { - match info.kind { - cargo_metadata::DependencyKind::Normal => return Self::Normal, - cargo_metadata::DependencyKind::Development => return Self::Dev, - cargo_metadata::DependencyKind::Build => return Self::Build, + let kind = match info.kind { + cargo_metadata::DependencyKind::Normal => Self::Normal, + cargo_metadata::DependencyKind::Development => Self::Dev, + cargo_metadata::DependencyKind::Build => Self::Build, cargo_metadata::DependencyKind::Unknown => continue, - } + }; + dep_kinds.push(kind); } - - Self::Normal + dep_kinds.sort_unstable(); + dep_kinds.dedup(); + dep_kinds.into_iter() } } @@ -317,7 +323,11 @@ impl CargoWorkspace { } }; node.deps.sort_by(|a, b| a.pkg.cmp(&b.pkg)); - for dep_node in node.deps { + for (dep_node, kind) in node + .deps + .iter() + .flat_map(|dep| DepKind::iter(&dep.dep_kinds).map(move |kind| (dep, kind))) + { let pkg = match pkg_by_id.get(&dep_node.pkg) { Some(&pkg) => pkg, None => { @@ -328,11 +338,7 @@ impl CargoWorkspace { continue; } }; - let dep = PackageDependency { - name: dep_node.name, - pkg, - kind: DepKind::new(&dep_node.dep_kinds), - }; + let dep = PackageDependency { name: dep_node.name.clone(), pkg, kind }; packages[source].dependencies.push(dep); } packages[source].active_features.extend(node.features); -- cgit v1.2.3