pub struct Krates<N = Package, E = Edge> { /* private fields */ }
Expand description
A crate graph. Each unique crate is a node, and each unique dependency between 2 crates is an edge.
Implementations§
source§impl<N, E> Krates<N, E>
impl<N, E> Krates<N, E>
sourcepub fn workspace_root(&self) -> &Utf8Path
pub fn workspace_root(&self) -> &Utf8Path
Path to the root of the workspace where the graph metadata was acquired from
sourcepub fn krates(&self) -> impl Iterator<Item = &N>
pub fn krates(&self) -> impl Iterator<Item = &N>
Get an iterator over the crate nodes in the graph. The crates are always ordered lexicographically by their identfier.
use krates::Krates;
fn print_krates(krates: &Krates) {
for (name, version) in krates.krates().map(|krate| (&krate.name, &krate.version)) {
println!("Crate {} @ {}", name, version);
}
}
sourcepub fn get_deps(&self, id: NodeId) -> impl Iterator<Item = (&Node<N>, &E)>
pub fn get_deps(&self, id: NodeId) -> impl Iterator<Item = (&Node<N>, &E)>
Get an iterator over each dependency of the specified crate. The same dependency can be returned multiple times if the crate depends on it with more than 1 dependency kind.
use krates::{Krates, Kid, DepKind};
fn count_build_deps(krates: &Krates, pkg: &Kid) -> usize {
krates.get_deps(krates.nid_for_kid(pkg).unwrap())
.filter(|(_, edge)| matches!(
edge,
krates::Edge::Dep { kind: DepKind::Build, .. } |
krates::Edge::DepFeature { kind: DepKind::Build, .. }
))
.count()
}
sourcepub fn direct_dependencies(&self, nid: NodeId) -> Vec<DirectDependency<'_, N>>
pub fn direct_dependencies(&self, nid: NodeId) -> Vec<DirectDependency<'_, N>>
Gets crates directly depended upon by the specified node
sourcepub fn direct_dependents(&self, nid: NodeId) -> Vec<DirectDependent<'_, N>>
pub fn direct_dependents(&self, nid: NodeId) -> Vec<DirectDependent<'_, N>>
Gets the crates that have a direct dependency on the specified node
sourcepub fn nid_for_kid(&self, kid: &Kid) -> Option<NodeId>
pub fn nid_for_kid(&self, kid: &Kid) -> Option<NodeId>
Get the node identifier for the specified crate identifier
sourcepub fn node_for_kid(&self, kid: &Kid) -> Option<&Node<N>>
pub fn node_for_kid(&self, kid: &Kid) -> Option<&Node<N>>
Get the node for the specified crate identifier
pub fn get_node( &self, kid: &Kid, feature: Option<&str> ) -> Option<(NodeId, &Node<N>)>
sourcepub fn get_enabled_features(&self, kid: &Kid) -> Option<&EnabledFeatures>
pub fn get_enabled_features(&self, kid: &Kid) -> Option<&EnabledFeatures>
Gets the features enabled for the specified crate
sourcepub fn workspace_members(&self) -> impl Iterator<Item = &Node<N>>
pub fn workspace_members(&self) -> impl Iterator<Item = &Node<N>>
Get an iterator over the nodes for the members of the workspace
source§impl<N> Krates<N, Edge>where
N: Debug,
impl<N> Krates<N, Edge>where
N: Debug,
sourcepub fn krates_filtered(&self, filter: DepKind) -> Vec<&N>
pub fn krates_filtered(&self, filter: DepKind) -> Vec<&N>
Removes all of the crates that are only referenced via the specified dependency kind.
This gives the same output as if the graph had been built by using
ignore_kind
with Scope::all
source§impl<N, E> Krates<N, E>where
N: KrateDetails,
impl<N, E> Krates<N, E>where
N: KrateDetails,
If the node type N supports KrateDetails
, we can also iterator over krates
of a given name and or version
sourcepub fn search_matches(
&self,
name: impl Into<String>,
req: VersionReq
) -> impl Iterator<Item = KrateMatch<'_, N>>
pub fn search_matches( &self, name: impl Into<String>, req: VersionReq ) -> impl Iterator<Item = KrateMatch<'_, N>>
Get an iterator over the crates that match the specified name, as well as satisfy the specified semver requirement.
use krates::{Krates, semver::VersionReq};
fn print(krates: &Krates, name: &str) {
let req = VersionReq::parse("=0.2").unwrap();
for vs in krates.search_matches(name, req.clone()).map(|km| &km.krate.version) {
println!("found version {vs} matching {req}!");
}
}
sourcepub fn krates_by_name(
&self,
name: impl Into<String>
) -> impl Iterator<Item = KrateMatch<'_, N>>
pub fn krates_by_name( &self, name: impl Into<String> ) -> impl Iterator<Item = KrateMatch<'_, N>>
Get an iterator over all of the crates in the graph with the given name, in the case there are multiple versions, or sources, of the crate.
use krates::Krates;
fn print_all_versions(krates: &Krates, name: &str) {
for vs in krates.krates_by_name(name).map(|km| &km.krate.version) {
println!("found version {vs}");
}
}