10434: Allow `Locate parent module` command in Cargo.toml r=Veykril a=rainy-me

close #10355

Co-authored-by: rainy-me <github@rainy.me>
Co-authored-by: rainy-me <github@yue.coffee>
This commit is contained in:
bors[bot] 2021-10-14 10:56:08 +00:00 committed by GitHub
commit f87debcf87
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 108 additions and 9 deletions

View file

@ -1,6 +1,6 @@
//! See [`CargoWorkspace`].
use std::convert::TryInto;
use std::convert::{TryFrom, TryInto};
use std::iter;
use std::path::PathBuf;
use std::{ops, process::Command};
@ -400,6 +400,39 @@ impl CargoWorkspace {
}
}
pub fn parent_manifests(&self, manifest_path: &ManifestPath) -> Option<Vec<ManifestPath>> {
let mut found = false;
let parent_manifests = self
.packages()
.filter_map(|pkg| {
if !found && &self[pkg].manifest == manifest_path {
found = true
}
self[pkg].dependencies.iter().find_map(|dep| {
if &self[dep.pkg].manifest == manifest_path {
return Some(self[pkg].manifest.clone());
}
None
})
})
.collect::<Vec<ManifestPath>>();
// some packages has this pkg as dep. return their manifests
if parent_manifests.len() > 0 {
return Some(parent_manifests);
}
// this pkg is inside this cargo workspace, fallback to workspace root
if found {
return Some(vec![
ManifestPath::try_from(self.workspace_root().join("Cargo.toml")).ok()?
]);
}
// not in this workspace
None
}
fn is_unique(&self, name: &str) -> bool {
self.packages.iter().filter(|(_, v)| v.name == name).count() == 1
}