38 releases

0.11.2 Sep 7, 2020
0.11.1 Jul 28, 2020
0.10.1 Jul 18, 2020
0.9.1 Nov 30, 2019
0.2.0 Mar 17, 2017
Download history 44536/week @ 2020-05-30 47843/week @ 2020-06-06 39541/week @ 2020-06-13 35197/week @ 2020-06-20 37084/week @ 2020-06-27 38586/week @ 2020-07-04 42263/week @ 2020-07-11 43584/week @ 2020-07-18 39543/week @ 2020-07-25 41989/week @ 2020-08-01 40274/week @ 2020-08-08 39572/week @ 2020-08-15 41169/week @ 2020-08-22 40511/week @ 2020-08-29 38734/week @ 2020-09-05 39610/week @ 2020-09-12

170,202 downloads per month
Used in 372 crates (120 directly)

MIT license

38KB
595 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"])
    .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–1.7MB
~40K SLoC