Crate cargo_metadata

source ·
Expand description

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");

Re-exports

pub use camino;

Modules

This module contains Diagnostic and the types/functions it uses for deserialization.

Structs

A compiler-generated file.
Profile settings used to determine which compiler flags to use for a target.
Final result of a build.
Output of a build script execution.
Message left by the compiler
Information about a dependency kind.
A dependency of the main crate
An iterator of Messages.
Starting point for metadata returned by cargo metadata
A builder for configurating cargo metadata invocation.
A node in a dependencies graph
A dependency in a node
One or more crates described by a single Cargo.toml
An “opaque” identifier for a package. It is possible to inspect the repr field, if the need arises, but its precise format is an implementation detail and is subject to change.
A dependency graph
The source of a package such as crates.io.
A single target (lib, bin, example, …) provided by a crate
SemVer version as defined by https://semver.org.
SemVer version requirement describing the intersection of some version comparators, such as >=1.2.3, <1.8.

Enums

Cargo features flags
Dependencies can come in three kinds
Error returned when executing/parsing cargo metadata fails.
A cargo message

Functions

parse_messagesDeprecated
Creates an iterator of Message from a Read outputting a stream of JSON messages. For usage information, look at the top-level documentation.

Type Definitions

Custom result type for cargo_metadata::Error