#cargo #metadata #structured

build cargo_metadata

structured access to the output of cargo metadata

21 unstable releases (5 breaking)

0.6.0 Jul 11, 2018
0.5.6 Jun 6, 2018
0.5.4 Mar 27, 2018
0.4.0 Dec 21, 2017
0.2.0 Mar 17, 2017

#1 in Cargo plugins

Download history 14736/week @ 2018-08-07 17712/week @ 2018-08-14 16495/week @ 2018-08-21 16303/week @ 2018-08-28 10534/week @ 2018-09-04 10245/week @ 2018-09-11 10999/week @ 2018-09-18 10473/week @ 2018-09-25 11847/week @ 2018-10-02 11836/week @ 2018-10-09 11671/week @ 2018-10-16 10954/week @ 2018-10-23 12188/week @ 2018-10-30

58,985 downloads per month
Used in 199 crates (28 directly)

MIT license

15KB
190 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, and clap.


lib.rs:

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

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 manifest_path = match args.next() {
    Some(ref p) if p == "--manifest-path" => args.next(),
    Some(p) => Some(p.trim_left_matches("--manifest-path=").to_string()),
    None => None,
};

let _metadata = cargo_metadata::metadata(manifest_path.as_ref().map(Path::new)).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 _metadata =
    cargo_metadata::metadata(args.arg_manifest_path.as_ref().map(Path::new)).unwrap();
# }

With clap:

# // This should be kept in sync with the equivalent example in the readme.
# extern crate cargo_metadata;
# extern crate clap;
# use std::path::Path;

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 _metadata =
    cargo_metadata::metadata(matches.value_of("manifest-path").map(Path::new)).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;

let manifest_path = Path::new("./Cargo.toml");
let features = cargo_metadata::CargoOpt::AllFeatures;
let _metadata =
cargo_metadata::metadata_run(Some(manifest_path), false, Some(features)).unwrap();

Dependencies

~2MB
~40K SLoC