1 个不稳定版本
0.1.0 | 2021年9月11日 |
---|
#569 在 构建工具
每月下载量 398 次
用于 2 crates
6KB
包信息
包信息提供从你的 Rust 代码中访问 Cargo 清单信息的功能。实际上,Cargo 通过暴露环境变量来提供这一功能。然而,如果你从二进制文件(而不是通过 cargo run
)运行应用程序,这些环境变量将不再存在。
包信息通过在编译时捕获环境变量值并将它们嵌入你的代码中通过派生特性 PackageInfo
来解决这个问题。你必须将 两个 package_info
和 package_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>;
}