Struct krates::Krates

source ·
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>

source

pub fn len(&self) -> usize

The number of unique crates in the graph

source

pub fn workspace_root(&self) -> &Utf8Path

Path to the root of the workspace where the graph metadata was acquired from

source

pub fn graph(&self) -> &Graph<Node<N>, E>

Get access to the raw petgraph

source

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);
    }
}
source

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()
}
source

pub fn direct_dependencies(&self, nid: NodeId) -> Vec<DirectDependency<'_, N>>

Gets crates directly depended upon by the specified node

source

pub fn direct_dependents(&self, nid: NodeId) -> Vec<DirectDependent<'_, N>>

Gets the crates that have a direct dependency on the specified node

source

pub fn nid_for_kid(&self, kid: &Kid) -> Option<NodeId>

Get the node identifier for the specified crate identifier

source

pub fn node_for_kid(&self, kid: &Kid) -> Option<&Node<N>>

Get the node for the specified crate identifier

source

pub fn get_node( &self, kid: &Kid, feature: Option<&str> ) -> Option<(NodeId, &Node<N>)>

source

pub fn get_enabled_features(&self, kid: &Kid) -> Option<&EnabledFeatures>

Gets the features enabled for the specified crate

source

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,

source

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,

If the node type N supports KrateDetails, we can also iterator over krates of a given name and or version

source

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}!");
    }
}
source

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}");
    }
}

Trait Implementations§

source§

impl<N, E> Index<NodeIndex> for Krates<N, E>

§

type Output = N

The returned type after indexing.
source§

fn index(&self, id: NodeId) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
source§

impl<N, E> Index<usize> for Krates<N, E>

§

type Output = N

The returned type after indexing.
source§

fn index(&self, idx: usize) -> &Self::Output

Performs the indexing (container[index]) operation. Read more

Auto Trait Implementations§

§

impl<N, E> Freeze for Krates<N, E>

§

impl<N, E> RefUnwindSafe for Krates<N, E>

§

impl<N, E> Send for Krates<N, E>
where E: Send, N: Send,

§

impl<N, E> Sync for Krates<N, E>
where E: Sync, N: Sync,

§

impl<N, E> Unpin for Krates<N, E>
where E: Unpin, N: Unpin,

§

impl<N, E> UnwindSafe for Krates<N, E>
where E: UnwindSafe, N: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.