Skip to content

Commit

Permalink
Add Krates::direct_dependents (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jake-Shadle authored Oct 25, 2022
1 parent ffd76b8 commit 8a8b1cc
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
**/target/
**/*.rs.bk
Cargo.lock
Cargo.lock
*.snap.new
28 changes: 28 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,34 @@ impl<N, E> Krates<N, E> {
})
}

/// Gets the crates that have a direct dependency on the specified node
#[inline]
pub fn direct_dependents(&self, nid: NodeId) -> Vec<&N> {
let graph = self.graph();
let mut direct_dependencies = Vec::new();
let mut stack = vec![nid];
let mut visited = std::collections::BTreeSet::new();

while let Some(nid) = stack.pop() {
for edge in graph.edges_directed(nid, Direction::Incoming) {
match &self.graph[edge.source()] {
Node::Krate { krate, .. } => {
if visited.insert(edge.source()) {
direct_dependencies.push(krate);
}
}
Node::Feature { krate_index, .. } => {
if *krate_index == nid {
stack.push(edge.source());
}
}
}
}
}

direct_dependencies
}

/// Get the node identifier for the specified crate identifier
#[inline]
pub fn nid_for_kid(&self, kid: &Kid) -> Option<NodeId> {
Expand Down
1 change: 1 addition & 0 deletions tests/direct.json

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions tests/direct/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "direct"
version = "0.1.0"
edition = "2021"

[dependencies]
google-cloud-pubsub = { version = "0.8", default_features = false, features = ["rustls-tls"] }
google-cloud-gax = { version = "0.9", default_features = false, features = ["rustls-tls"] }
google-cloud-googleapis = "0.6"
1 change: 1 addition & 0 deletions tests/direct/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

34 changes: 34 additions & 0 deletions tests/misc.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
mod util;

#[test]
fn iter_names() {
let contents = std::fs::read_to_string("tests/all-features.json")
Expand Down Expand Up @@ -123,3 +125,35 @@ fn iter_matches() {
assert!(iter.next().is_none());
}
}

#[test]
fn direct_dependents() {
let mut kb = krates::Builder::new();
kb.include_targets(std::iter::once((
krates::cfg_expr::targets::get_builtin_target_by_triple("x86_64-unknown-linux-gnu")
.unwrap()
.triple
.clone(),
vec![],
)));

let grafs = util::build("direct.json", kb).unwrap();

let id = grafs
.actual
.krates()
.find(|k| k.0.repr.starts_with("reqwest"))
.unwrap();

let dd = grafs
.actual
.direct_dependents(grafs.actual.nid_for_kid(&id.0).unwrap())
.into_iter()
.fold(String::new(), |mut acc, jid| {
acc.push_str(&jid.0.repr);
acc.push('\n');
acc
});

insta::assert_snapshot!(dd);
}
7 changes: 7 additions & 0 deletions tests/snapshots/misc__direct_dependents.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
source: tests/misc.rs
expression: dd
---
google-cloud-metadata 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)
google-cloud-auth 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)

2 changes: 1 addition & 1 deletion tests/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use std::{fmt, path::Path};

pub struct JustId(krates::Kid);
pub struct JustId(pub krates::Kid);

pub type Graph = krates::Krates<JustId>;

Expand Down

0 comments on commit 8a8b1cc

Please sign in to comment.