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
With std::env::args():
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:
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:
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:
#[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
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"])
.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");Modules§
- diagnostic
- This module contains
Diagnosticand the types/functions it uses for deserialization.
Structs§
- Artifact
- A compiler-generated file.
- Artifact
Profile - Profile settings used to determine which compiler flags to use for a target.
- Build
Script - Output of a build script execution.
- Compiler
Message - Message left by the compiler
- DepKind
Info - Information about a dependency kind.
- Dependency
- A dependency of the main crate
- Metadata
- Starting point for metadata returned by
cargo metadata - Metadata
Command - A builder for configurating
cargo metadatainvocation. - Node
- A node in a dependencies graph
- NodeDep
- A dependency in a node
- Package
- A crate
- Package
Id - An “opaque” identifier for a package.
It is possible to inspect the
reprfield, if the need arises, but its precise format is an implementation detail and is subject to change. - Resolve
- A dependency graph
- Source
- The source of a package such as crates.io.
- Target
- A single target (lib, bin, example, …) provided by a crate
Enums§
- Cargo
Opt - Cargo features flags
- Dependency
Kind - Dependencies can come in three kinds
- Error
- Error returned when executing/parsing
cargo metadatafails. - Message
- A cargo message
Functions§
- parse_
messages - Creates an iterator of Message from a Read outputting a stream of JSON messages. For usage information, look at the top-level documentation.
Type Aliases§
- Result
- Custom result type for
cargo_metadata::Error