generalize the path_to_top from the links errors

This commit is contained in:
Eh2406 2018-02-07 17:50:03 -05:00
parent 22ff9fcb91
commit 5a5b5fce42
2 changed files with 26 additions and 22 deletions

View File

@ -118,28 +118,8 @@ struct Candidate {
impl Resolve {
/// Resolves one of the paths from the given dependent package up to
/// the root.
pub fn path_to_top<'a>(&'a self, mut pkg: &'a PackageId) -> Vec<&'a PackageId> {
// Note that this implementation isn't the most robust per se, we'll
// likely have to tweak this over time. For now though it works for what
// it's used for!
let mut result = vec![pkg];
let first_pkg_depending_on = |pkg: &PackageId| {
self.graph.get_nodes()
.iter()
.filter(|&(_node, adjacent)| adjacent.contains(pkg))
.next()
.map(|p| p.0)
};
while let Some(p) = first_pkg_depending_on(pkg) {
// Note that we can have "cycles" introduced through dev-dependency
// edges, so make sure we don't loop infinitely.
if result.contains(&p) {
break
}
result.push(p);
pkg = p;
}
result
pub fn path_to_top<'a>(&'a self, pkg: &'a PackageId) -> Vec<&'a PackageId> {
self.graph.path_to_top(pkg)
}
pub fn register_used_patches(&mut self,
patches: &HashMap<Url, Vec<Summary>>) {

View File

@ -67,6 +67,30 @@ impl<N: Eq + Hash + Clone> Graph<N> {
pub fn iter(&self) -> Nodes<N> {
self.nodes.keys()
}
/// Resolves one of the paths from the given dependent package up to
/// the root.
pub fn path_to_top<'a>(&'a self, mut pkg: &'a N) -> Vec<&'a N> {
// Note that this implementation isn't the most robust per se, we'll
// likely have to tweak this over time. For now though it works for what
// it's used for!
let mut result = vec![pkg];
let first_pkg_depending_on = |pkg: &N, res: &[&N]| {
self.get_nodes()
.iter()
.filter(|&(_node, adjacent)| adjacent.contains(pkg))
// Note that we can have "cycles" introduced through dev-dependency
// edges, so make sure we don't loop infinitely.
.filter(|&(_node, _)| !res.contains(&_node))
.next()
.map(|p| p.0)
};
while let Some(p) = first_pkg_depending_on(pkg, &result) {
result.push(p);
pkg = p;
}
result
}
}
impl<N: Eq + Hash + Clone> Default for Graph<N> {