Skip to content

Commit 75768e0

Browse files
committed
Refactor LocalManifest::get_dependency_version
The new method get_dependencies doesn't take a dep key but instead returns all deps. We'll sometimes need this in coming patches, and when we don't need it it's easy to have the caller filter.
1 parent 2a9f9ac commit 75768e0

File tree

2 files changed

+10
-14
lines changed

2 files changed

+10
-14
lines changed

src/cargo/ops/cargo_add/mod.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -648,8 +648,11 @@ fn get_existing_dependency(
648648
}
649649

650650
let mut possible: Vec<_> = manifest
651-
.get_dependency_versions(dep_key, ws, unstable_features)
652-
.map(|(path, dep)| {
651+
.get_dependencies(ws, unstable_features)
652+
.filter_map(|(key, path, dep)| {
653+
if key.as_str() != dep_key {
654+
return None;
655+
}
653656
let key = if path == *section {
654657
(Key::Existing, true)
655658
} else if dep.is_err() {
@@ -662,7 +665,7 @@ fn get_existing_dependency(
662665
};
663666
(key, path.target().is_some())
664667
};
665-
(key, dep)
668+
Some((key, dep))
666669
})
667670
.collect();
668671
possible.sort_by_key(|(key, _)| *key);

src/cargo/util/toml_mut/manifest.rs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -327,12 +327,11 @@ impl LocalManifest {
327327
}
328328

329329
/// Lookup a dependency.
330-
pub fn get_dependency_versions<'s>(
330+
pub fn get_dependencies<'s>(
331331
&'s self,
332-
dep_key: &'s str,
333332
ws: &'s Workspace<'_>,
334333
unstable_features: &'s Features,
335-
) -> impl Iterator<Item = (DepTable, CargoResult<Dependency>)> + 's {
334+
) -> impl Iterator<Item = (String, DepTable, CargoResult<Dependency>)> + 's {
336335
let crate_root = self.path.parent().expect("manifest path is absolute");
337336
self.get_sections()
338337
.into_iter()
@@ -341,13 +340,7 @@ impl LocalManifest {
341340
Some(
342341
table
343342
.into_iter()
344-
.filter_map(|(key, item)| {
345-
if key.as_str() == dep_key {
346-
Some((table_path.clone(), key, item))
347-
} else {
348-
None
349-
}
350-
})
343+
.map(|(key, item)| (table_path.clone(), key, item))
351344
.collect::<Vec<_>>(),
352345
)
353346
})
@@ -361,7 +354,7 @@ impl LocalManifest {
361354
&dep_key,
362355
&dep_item,
363356
);
364-
(table_path, dep)
357+
(dep_key, table_path, dep)
365358
})
366359
}
367360

0 commit comments

Comments
 (0)