[go: up one dir, main page]

Crate cargo_metadata

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

Get the current crate’s metadata without default features but with all dependency information.

let _metadata = MetadataCommand::new().exec().unwrap();

If you have a program that takes --manifest-path as an argument, you can forward that to MetadataCommand:

let mut args = std::env::args().skip_while(|val| !val.starts_with("--manifest-path"));
let mut cmd = 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, e.g. --all-features.

let _metadata = MetadataCommand::new()
    .manifest_path("./Cargo.toml")
    .features(CargoOpt::AllFeatures)
    .exec()
    .unwrap();

Parse message-format output produced by other cargo commands. It is recommended to use crates like escargot to produce the Command.

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 libtest::TestMessage;unstable
pub use camino;
pub use cargo_platform;
pub use semver;

Modules§

diagnostic
This module contains Diagnostic and the types/functions it uses for deserialization.
libtestunstable
Parses output of libtest.

Structs§

Artifact
A compiler-generated file.
ArtifactBuilderbuilder
Builder for Artifact.
ArtifactProfile
Profile settings used to determine which compiler flags to use for a target.
ArtifactProfileBuilderbuilder
Builder for ArtifactProfile.
BuildFinished
Final result of a build.
BuildFinishedBuilderbuilder
Builder for BuildFinished.
BuildScript
Output of a build script execution.
BuildScriptBuilderbuilder
Builder for BuildScript.
CompilerMessage
Message left by the compiler
CompilerMessageBuilderbuilder
Builder for CompilerMessage.
DepKindInfo
Information about a dependency kind.
DepKindInfoBuilder
Builder for DepKindInfo.
Dependency
A dependency of the main crate
DependencyBuilderbuilder
Builder for Dependency.
FeatureName
Feature name newtype
MessageIter
An iterator of Messages.
Metadata
Starting point for metadata returned by cargo metadata
MetadataBuilder
Builder for Metadata.
MetadataCommand
A builder for configuring cargo metadata invocation.
Node
A node in a dependencies graph
NodeBuilder
Builder for Node.
NodeDep
A dependency in a node
NodeDepBuilder
Builder for NodeDep.
Package
One or more crates described by a single Cargo.toml
PackageBuilder
Builder for Package.
PackageId
An “opaque” identifier for a package.
PackageName
Package name newtype
Resolve
A dependency graph
ResolveBuilder
Builder for Resolve.
Source
The source of a package such as crates.io.
Target
A single target (lib, bin, example, …) provided by a crate
TargetBuilder
Builder for Target.
WorkspaceDefaultMembers
A list of default workspace members.

Enums§

ArtifactDebuginfo
The kind of debug information included in the artifact.
CargoOpt
Cargo features flags
CrateType
Similar to kind, but only reports the Cargo crate types:
DepKindInfoBuilderError
Error type for DepKindInfoBuilder
DependencyKind
Dependencies can come in three kinds
Edition
The Rust edition
Error
Error returned when executing/parsing cargo metadata fails.
Message
A cargo message
MetadataBuilderError
Error type for MetadataBuilder
NodeBuilderError
Error type for NodeBuilder
NodeDepBuilderError
Error type for NodeDepBuilder
PackageBuilderError
Error type for PackageBuilder
ResolveBuilderError
Error type for ResolveBuilder
TargetBuilderError
Error type for TargetBuilder
TargetKind
Kind of target.

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 Aliases§

Result
Custom result type for cargo_metadata::Error