#cargo #metadata #graph

krates

Create graphs of crates gathered from cargo metadata

7 releases (4 breaking)

new 0.5.0 Oct 20, 2020
0.4.2 Oct 13, 2020
0.4.1 Jul 28, 2020
0.3.1 Jul 18, 2020
0.1.0 Jan 14, 2020

#7 in Visualization

Download history 488/week @ 2020-07-04 440/week @ 2020-07-11 571/week @ 2020-07-18 736/week @ 2020-07-25 574/week @ 2020-08-01 1014/week @ 2020-08-08 867/week @ 2020-08-15 918/week @ 2020-08-22 810/week @ 2020-08-29 394/week @ 2020-09-05 423/week @ 2020-09-12 496/week @ 2020-09-19 550/week @ 2020-09-26 861/week @ 2020-10-03 674/week @ 2020-10-10 967/week @ 2020-10-17

2,954 downloads per month
Used in 3 crates

MIT/Apache

235KB
1K SLoC

📦 krates

Build Status Crates.io Docs Rust Version Contributor Covenant Embark

Creates graphs of crates from cargo_metadata metadata.

Usage

use krates::{Builder, Cmd, Krates, cm, petgraph};
fn main() -> Result<(), krates::Error> {
    let mut cmd = Cmd::new();
    cmd.manifest_path("path/to/a/Cargo.toml");
    // Enable all features, works for either an entire workspace or a single crate
    cmd.all_features();

    let mut builder = Builder::new();
    // Let's filter out any crates that aren't used by x86_64 windows
    builder.include_targets(std::iter::once(("x86_64-pc-windows-msvc", vec![])));

    let krates: Krates = builder.build(cmd, |pkg: cm::Package| {
        println!("Crate {} was filtered out", pkg.id);
    })?;

    // Print a dot graph of the entire crate graph
    println!("{:?}", petgraph::dot::Dot::new(krates.graph()));

    Ok(())
}

krates can also be used if you use cargo as a dependency. It doesn't depend on cargo itself since cargo moves quickly and we don't want to artificially limit which versions you use, but, at least with the current stable cargo crate, the following code works well.

fn get_metadata(
    no_default_features: bool,
    all_features: bool,
    features: Vec<String>,
    manifest_path: PathBuf,
) -> Result<krates::cm::Metadata, anyhow::Error> {
    let config = cargo::util::Config::default()?;
    let ws = cargo::core::Workspace::new(&manifest_path, &config)?;
    let options = cargo::ops::OutputMetadataOptions {
        features,
        no_default_features,
        all_features,
        no_deps: false,
        version: 1,
        filter_platforms: vec![],
    };

    let md = cargo::ops::output_metadata(&ws, &options)?;
    let md_value = serde_json::to_value(md)?;

    Ok(serde_json::from_value(md_value)?)
}

Contributing

We welcome community contributions to this project.

Please read our Contributor Guide for more information on how to get started.

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~4MB
~85K SLoC