dependency-graph
Dependency-graph is a minimal library, exposing a DependencyGraph structure and a single Node trait.
To use the library, simply implement the Node trait's two functions for the object you wish to resolve dependencies for.
Example
In this example, we'll be using dependency-graph to resolve dependencies between our Package structs. We'll be using a custom Dependency type, because we want to include Semantic Versioning constraints in our dependencies. That way we'll be able to say that our package mypackage depends on version 2.0 of some package some-library for instance.
Our Package and Dependency structs use the semver::Version and semver::VersionReq types to define the versions of the Packages (such as 1.2.3-beta.4) and the dependency requirements such as >=2.0. See the semver crate for more information.
First we define the Package struct:
Where Dependency is:
Implementing the Node trait for our Package is pretty simple:
Let's define some packages and dependencies:
let packages = vec!
Now that we've defined all our packages as well as how dependencies between them are resolved (by implementing Node), we can build a DependencyGraph from it and traverse it, knowing that dependencies will always be visited before the packages that depend on them.
In our case, we would expect the base package to be first (since derived depends on it), then derived second, since second_order depends on it in turn, and therefore cannot be resolved until derived has been. And then finally second_order, since all its dependencies have now been resolved.
let graph = from;
for package in graph
If we run the above code, we can verify that they did indeed build in the right order:
Building base!
Building derived!
Building second_order!