45 releases

0.13.1 Mar 2, 2021
0.12.3 Jan 15, 2021
0.12.1 Nov 22, 2020
0.11.1 Jul 28, 2020
0.2.0 Mar 17, 2017
Download history 61458/week @ 2021-01-13 63225/week @ 2021-01-20 58158/week @ 2021-01-27 58851/week @ 2021-02-03 56018/week @ 2021-02-10 65346/week @ 2021-02-17 62318/week @ 2021-02-24 60253/week @ 2021-03-03 65408/week @ 2021-03-10 59680/week @ 2021-03-17 63160/week @ 2021-03-24 61537/week @ 2021-03-31 63272/week @ 2021-04-07 57630/week @ 2021-04-14 60290/week @ 2021-04-21 56603/week @ 2021-04-28

242,559 downloads per month
Used in 510 crates (162 directly)

MIT license

42KB
608 lines

cargo_metadata

Structured access to the output of cargo metadata. Usually used from within a cargo-* executable.

Also supports serialization to aid in implementing --message-format=json-like output generation in cargo-* subcommands, since some of the types in what cargo --message-format=json emits are exactly the same as the ones from cargo metadata.

Build Status crates.io

Documentation - contains examples of use with std::env::args, docopt, clap, and structopt.


lib.rs:

Structured access to the output of cargo metadata and cargo --message-format=json. Usually used from within a cargo-* executable

See the cargo book for details on cargo itself.

Examples

# extern crate cargo_metadata;
# use std::path::Path;
let mut args = std::env::args().skip_while(|val| !val.starts_with("--manifest-path"));

let mut cmd = cargo_metadata::MetadataCommand::new();
let manifest_path = match args.next() {
    Some(ref p) if p == "--manifest-path" => {
        cmd.manifest_path(args.next().unwrap());
    }
    Some(p) => {
        cmd.manifest_path(p.trim_start_matches("--manifest-path="));
    }
    None => {}
};

let _metadata = cmd.exec().unwrap();

Pass features flags

# // This should be kept in sync with the equivalent example in the readme.
# extern crate cargo_metadata;
# use std::path::Path;
# fn main() {
use cargo_metadata::{MetadataCommand, CargoOpt};

let _metadata = MetadataCommand::new()
    .manifest_path("./Cargo.toml")
    .features(CargoOpt::AllFeatures)
    .exec()
    .unwrap();
# }

Parse message-format output:

# extern crate cargo_metadata;
use std::process::{Stdio, Command};
use cargo_metadata::Message;

let mut command = Command::new("cargo")
    .args(&["build", "--message-format=json-render-diagnostics"])
    .stdout(Stdio::piped())
    .spawn()
    .unwrap();

let reader = std::io::BufReader::new(command.stdout.take().unwrap());
for message in cargo_metadata::Message::parse_stream(reader) {
    match message.unwrap() {
        Message::CompilerMessage(msg) => {
            println!("{:?}", msg);
        },
        Message::CompilerArtifact(artifact) => {
            println!("{:?}", artifact);
        },
        Message::BuildScriptExecuted(script) => {
            println!("{:?}", script);
        },
        Message::BuildFinished(finished) => {
            println!("{:?}", finished);
        },
        _ => () // Unknown message
    }
}

let output = command.wait().expect("Couldn't get cargo's exit status");

Dependencies

~1.9–2.7MB
~59K SLoC