Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Krates::direct_dependents #43

Merged
merged 1 commit into from Oct 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
@@ -1,3 +1,4 @@
**/target/
**/*.rs.bk
Cargo.lock
Cargo.lock
*.snap.new
28 changes: 28 additions & 0 deletions src/lib.rs
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
@@ -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
@@ -0,0 +1 @@

34 changes: 34 additions & 0 deletions tests/misc.rs
@@ -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
@@ -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
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