metadata!() { /* proc-macro */ }
Expand description
Macro for accessing data from the package.metadata
section of the Cargo manifest
§Arguments
key
- A string slice of a dot-separated path to the TOML key of interest
§Example
Given the following Cargo.toml
:
[package]
name = "MyApp"
version = "0.1.0"
[package.metadata]
copyright = "Copyright (c) 2019 ACME Inc."
And the following main.rs
:
#![feature(proc_macro_hygiene)]
use std::env;
use cargo_meta::package_metadata;
pub fn main() {
println!("{} {}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"));
println!("{}", package_metadata!("copyright"));
}
Invoking cargo run
will produce:
MyApp 0.1.0
Copyright (c) 2019 ACME Inc.
§TOML Support
This macro only supports static data:
- Integers
- Floating-point numbers
- Booleans
- Keys of tables nested below the
package.metadata
section of the manifest (if the value is a supported type) - Entire TOML arrays
§Array Example
Given the following Cargo manifest:
[package.metadata.arrays]
some_array = [ 1, 2, 3 ]
This is legal:
static ARR: [3; i64] = package_metadata!("arrays.some_array");
It does not currently support accessing TOML array elements directly, though this
is possible. TOML tables will not be possible without a statically keyed hashmap such
as the one provided by the phf
crate. Support may be added if/when Rust can
support static hashmaps with compile-time key errors.