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

Improve direct dependents #44

Merged
merged 3 commits 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

<!-- next-header -->
## [Unreleased] - ReleaseDate
### Added
- [PR#43](https://github.com/EmbarkStudios/krates/pull/43) and [PR#44](https://github.com/EmbarkStudios/krates/pull/44) added `Krates::direct_dependents` to more easily obtain the crates that directly depend on the specified crate/node, regardless of any features in between those crates.

## [0.12.0] - 2022-10-06
### Added
- [PR#42](https://github.com/EmbarkStudios/krates/pull/42) added support for features, adding nodes for each unique future, and linking edges between dependencies and features themselves. This (hopefully) properly takes into account the existing ways of pruning the graph via targets, exclusions etc. It also allows the retrieval of that final feature set via `Krates::get_enabled_features`.
Expand Down
20 changes: 17 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ 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> {
pub fn direct_dependents(&self, nid: NodeId) -> Vec<DirectDependent<'_, N>> {
let graph = self.graph();
let mut direct_dependencies = Vec::new();
let mut stack = vec![nid];
Expand All @@ -283,11 +283,15 @@ impl<N, E> Krates<N, E> {
match &self.graph[edge.source()] {
Node::Krate { krate, .. } => {
if visited.insert(edge.source()) {
direct_dependencies.push(krate);
direct_dependencies.push(DirectDependent {
krate,
node_id: edge.source(),
edge_id: edge.id(),
});
}
}
Node::Feature { krate_index, .. } => {
if *krate_index == nid {
if *krate_index == nid && visited.insert(edge.source()) {
stack.push(edge.source());
}
}
Expand Down Expand Up @@ -361,6 +365,16 @@ impl<N, E> Krates<N, E> {
}
}

/// A crate that has a direct dependency on another crate
pub struct DirectDependent<'krates, N> {
/// The crate in the node
pub krate: &'krates N,
/// The crate's node id
pub node_id: NodeId,
/// The edge that links the crate with the dependency
pub edge_id: EdgeId,
}

/// A trait that can be applied to the type stored in the graph nodes to give
/// additional features on `Krates`.
pub trait KrateDetails {
Expand Down
2 changes: 1 addition & 1 deletion tests/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ fn direct_dependents() {
.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_str(&jid.krate.0.repr);
acc.push('\n');
acc
});
Expand Down