use cargo_metadata::{Package, PackageId};
use petgraph::visit::Dfs;
use std::collections::HashMap;
pub(crate) mod resolver;
type PackageGraphIndex = usize;
type PackageGraph =
petgraph::stable_graph::StableDiGraph<cargo_metadata::Package, (), PackageGraphIndex>;
#[derive(Clone, Debug)]
pub struct DependencyGraph {
index: HashMap<PackageId, PackageGraphIndex>,
packages: PackageGraph,
root_crate: PackageId,
}
impl DependencyGraph {
pub fn empty(root_crate: PackageId) -> Self {
Self {
index: HashMap::default(),
packages: PackageGraph::with_capacity(0, 0),
root_crate,
}
}
pub fn with_capacity(root_crate: PackageId, cap: usize) -> Self {
Self {
index: HashMap::default(),
packages: PackageGraph::with_capacity(cap, cap),
root_crate,
}
}
pub fn index(&self) -> &HashMap<PackageId, PackageGraphIndex> {
&self.index
}
pub fn packages(&self) -> &PackageGraph {
&self.packages
}
pub fn root_crate(&self) -> &PackageId {
&self.root_crate
}
}
impl PartialEq for DependencyGraph {
fn eq(&self, other: &Self) -> bool {
fn packages(graph: &DependencyGraph) -> Vec<&Package> {
let package_id = graph.root_crate();
let root_index = graph.index()[package_id].into();
let mut res = Vec::with_capacity(graph.packages().node_count());
while let Some(dep) = Dfs::new(graph.packages(), root_index).next(graph.packages()) {
let package = &graph.packages()[dep];
res.push(package);
}
res
}
self.root_crate() == other.root_crate() && packages(self) == packages(other)
}
}