#cargo-manifest #package #manifest #cargo #fields

编译 package_info

用于将 Cargo 包信息暴露给 Rust 代码的 Crate

1 个不稳定版本

0.1.0 2021年9月11日

#569构建工具

Download history 64/week @ 2024-04-06 146/week @ 2024-04-13 188/week @ 2024-04-20 83/week @ 2024-04-27 65/week @ 2024-05-04 51/week @ 2024-05-11 158/week @ 2024-05-18 87/week @ 2024-05-25 203/week @ 2024-06-01 110/week @ 2024-06-08 71/week @ 2024-06-15 105/week @ 2024-06-22 18/week @ 2024-06-29 73/week @ 2024-07-06 101/week @ 2024-07-13 203/week @ 2024-07-20

每月下载量 398 次
用于 2 crates

MIT 协议

6KB

包信息

包信息提供从你的 Rust 代码中访问 Cargo 清单信息的功能。实际上,Cargo 通过暴露环境变量来提供这一功能。然而,如果你从二进制文件(而不是通过 cargo run)运行应用程序,这些环境变量将不再存在。

包信息通过在编译时捕获环境变量值并将它们嵌入你的代码中通过派生特性 PackageInfo 来解决这个问题。你必须将 两个 package_infopackage_info_derive 都作为依赖项添加到你的项目中。

用法

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

#[derive(PackageInfo)]
struct CargoPackageInfo {}

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

PackageInfo 特性提供了以下函数,所有函数都返回一个 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>;
}

无运行时依赖