48 releases

0.14.2 Feb 16, 2022
0.14.1 Oct 27, 2021
0.14.0 Jul 8, 2021
0.13.1 Mar 2, 2021
0.2.0 Mar 17, 2017

#130 in Programming languages

Download history 127866/week @ 2022-01-28 111053/week @ 2022-02-04 113394/week @ 2022-02-11 108041/week @ 2022-02-18 118655/week @ 2022-02-25 116746/week @ 2022-03-04 118751/week @ 2022-03-11 118824/week @ 2022-03-18 138624/week @ 2022-03-25 135934/week @ 2022-04-01 133704/week @ 2022-04-08 119798/week @ 2022-04-15 127765/week @ 2022-04-22 130331/week @ 2022-04-29 141212/week @ 2022-05-06 139954/week @ 2022-05-13

562,116 downloads per month
Used in 717 crates (262 directly)

MIT license

43KB
626 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


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

~0.9–1.6MB
~36K SLoC