58 releases

new 0.19.1 Dec 3, 2024
0.19.0 Nov 20, 2024
0.18.1 Oct 13, 2023
0.17.0 Jul 24, 2023
0.2.0 Mar 17, 2017

#1 in Cargo plugins

Download history 637910/week @ 2024-08-19 629622/week @ 2024-08-26 626422/week @ 2024-09-02 676081/week @ 2024-09-09 617709/week @ 2024-09-16 705123/week @ 2024-09-23 700687/week @ 2024-09-30 746298/week @ 2024-10-07 750048/week @ 2024-10-14 721197/week @ 2024-10-21 742025/week @ 2024-10-28 725906/week @ 2024-11-04 754865/week @ 2024-11-11 754759/week @ 2024-11-18 726426/week @ 2024-11-25 792279/week @ 2024-12-02

3,070,455 downloads per month
Used in 2,476 crates (595 directly)

MIT license

73KB
1K SLoC

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

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

use cargo_metadata::{MetadataCommand, CargoOpt};

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

Parse message-format output:

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.9MB
~38K SLoC