#cargo-manifest #package #cargo #manifest #derive #fields #macro-derive

macro package_info_derive

Derive macro for exposing Cargo Package Information to Rust code

1 unstable release

0.1.0 Sep 11, 2021

#27 in #cargo-manifest

Download history 47/week @ 2023-12-18 5/week @ 2023-12-25 87/week @ 2024-01-01 42/week @ 2024-01-08 17/week @ 2024-01-15 31/week @ 2024-01-22 61/week @ 2024-01-29 215/week @ 2024-02-05 103/week @ 2024-02-12 97/week @ 2024-02-19 108/week @ 2024-02-26 108/week @ 2024-03-04 157/week @ 2024-03-11 77/week @ 2024-03-18 49/week @ 2024-03-25 224/week @ 2024-04-01

511 downloads per month
Used in 3 crates

MIT license

7KB
129 lines

Package Info

Package Info offers access to Cargo manifest information from within your Rust code. Actually, Cargo offers that by exposing it via environment variables. However, if you are running your application from binary (as opposed to via cargo run), those environment variables are no longer present.

Package Info works around this issue by capturing the environment variable values during compile and embedding them in your code via the derived trait PackageInfo. You must add both package_info and package_info_derive as dependencies to your project.

Usage

use package_info::PackageInfo;        // the trait
use package_info_derive::PackageInfo; // the derive macro

#[derive(PackageInfo)]
struct CargoPackageInfo {}

fn main() {
    println!("{}", CargoPackageInfo::name().unwrap());
}

The PackageInfo trait provides the following functions, all of which return a Option<String>:

/// Accessor functions to common Cargo package information as specified in Cargo.toml
pub trait PackageInfo {
    /// Colon separated list of authors from the manifest of the package
    fn authors() -> Option<String>;
    /// The description from the manifest of the package
    fn description() -> Option<String>;
    /// The home page from the manifest of the package
    fn homepage() -> Option<String>;
    /// The license from the manifest of the package
    fn license() -> Option<String>;
    /// The license file from the manifest of the package
    fn license_file() -> Option<String>;
    /// The name of the package
    fn name() -> Option<String>;
    /// The repository from the manifest of the package
    fn repository() -> Option<String>;
    /// The full version of the package
    fn version() -> Option<String>;
    /// The major version of the package
    fn version_major() -> Option<String>;
    /// The minor version of the package
    fn version_minor() -> Option<String>;
    /// The patch version of the package
    fn version_patch() -> Option<String>;
    /// The pre-release version of the package
    fn version_pre() -> Option<String>;
}

lib.rs:

Derive macros for exposing Cargo Package information in Rust apps run as binaries

Values are taken from environment variables exported by Cargo @see https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-crates

Dependencies

~1.5MB
~34K SLoC