Crate cargo_metadata[−][src]
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");
Modules
diagnostic | This module contains |
Structs
Artifact | A compiler-generated file. |
ArtifactProfile | Profile settings used to determine which compiler flags to use for a target. |
BuildFinished | Final result of a build. |
BuildScript | Output of a build script execution. |
CompilerMessage | Message left by the compiler |
DepKindInfo | Information about a dependency kind. |
Dependency | A dependency of the main crate |
MessageIter | An iterator of Messages. |
Metadata | Starting point for metadata returned by |
MetadataCommand | A builder for configurating |
Node | A node in a dependencies graph |
NodeDep | A dependency in a node |
Package | One or more crates described by a single |
PackageId | An “opaque” identifier for a package.
It is possible to inspect the |
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 |
Version | Represents a version number conforming to the semantic versioning scheme. |
Enums
CargoOpt | Cargo features flags |
DependencyKind | Dependencies can come in three kinds |
Error | Error returned when executing/parsing |
Message | A cargo message |
Functions
parse_messages | Deprecated 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
Result | Custom result type for |