#cargo #metadata #structured #types #output

cargo_metadata

structured access to the output of cargo metadata

35 releases

new 0.9.1 Nov 30, 2019
0.8.2 Aug 17, 2019
0.8.1 Jul 15, 2019
0.7.4 Mar 22, 2019
0.2.0 Mar 17, 2017
Download history 15881/week @ 2019-08-17 14532/week @ 2019-08-24 15398/week @ 2019-08-31 16213/week @ 2019-09-07 14808/week @ 2019-09-14 16200/week @ 2019-09-21 16672/week @ 2019-09-28 17448/week @ 2019-10-05 16945/week @ 2019-10-12 18542/week @ 2019-10-19 22050/week @ 2019-10-26 21104/week @ 2019-11-02 21749/week @ 2019-11-09 21266/week @ 2019-11-16 22576/week @ 2019-11-23

79,569 downloads per month
Used in 274 crates (60 directly)

MIT license

36KB
535 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

With std::env::args():

# // This should be kept in sync with the equivalent example in the readme.
# 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();

With docopt:

# // This should be kept in sync with the equivalent example in the readme.
# extern crate cargo_metadata;
# extern crate docopt;
# #[macro_use] extern crate serde_derive;
# use std::path::Path;
# use docopt::Docopt;
# fn main() {
const USAGE: &str = "
    Cargo metadata test function

    Usage:
      cargo_metadata [--manifest-path PATH]
";

#[derive(Debug, Deserialize)]
struct Args {
    arg_manifest_path: Option<String>,
}

let args: Args = Docopt::new(USAGE)
    .and_then(|d| d.deserialize())
    .unwrap_or_else(|e| e.exit());

let mut cmd = cargo_metadata::MetadataCommand::new();
if let Some(path) = args.arg_manifest_path {
    cmd.manifest_path(path);
}
let _metadata = cmd.exec().unwrap();
# }

With clap:

# // This should be kept in sync with the equivalent example in the readme.
# extern crate cargo_metadata;
# extern crate clap;
let matches = clap::App::new("myapp")
    .arg(
        clap::Arg::with_name("manifest-path")
            .long("manifest-path")
            .value_name("PATH")
            .takes_value(true),
    )
    .get_matches();

let mut cmd = cargo_metadata::MetadataCommand::new();
if let Some(path) = matches.value_of("manifest-path") {
    cmd.manifest_path(path);
}
let _metadata = cmd.exec().unwrap();

With structopt:

# // This should be kept in sync with the equivalent example in the readme.
# extern crate cargo_metadata;
# #[macro_use] extern crate structopt;
# use std::path::PathBuf;
# use structopt::StructOpt;
# fn main() {
#[derive(Debug, StructOpt)]
struct Opt {
    #[structopt(name = "PATH", long="manifest-path", parse(from_os_str))]
    manifest_path: Option<PathBuf>,
}

let opt = Opt::from_args();
let mut cmd = cargo_metadata::MetadataCommand::new();
if let Some(path) = opt.manifest_path {
    cmd.manifest_path(path);
}
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();

for message in cargo_metadata::parse_messages(command.stdout.take().unwrap()) {
    match message.unwrap() {
        Message::CompilerMessage(msg) => {
            println!("{:?}", msg);
        },
        Message::CompilerArtifact(artifact) => {
            println!("{:?}", artifact);
        },
        Message::BuildScriptExecuted(script) => {
            println!("{:?}", script);
        },
        _ => () // Unknown message
    }
}

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

Dependencies

~1.1–1.6MB
~37K SLoC