| Crates.io | cargo-pkg-info-struct-builder |
| lib.rs | cargo-pkg-info-struct-builder |
| version | 0.1.0-alpha11 |
| created_at | 2025-02-16 06:12:07.851185+00 |
| updated_at | 2025-02-18 02:30:58.977709+00 |
| description | A Rust crate used as a build dependency which provides structured access to Cargo Package Info. |
| homepage | |
| repository | https://github.com/jzombie/rust-cargo-pkg-info-struct-builder |
| max_upload_size | |
| id | 1557392 |
| size | 39,690 |
| OS | Status |
|---|---|
| Ubuntu-latest | |
| macOS-latest | |
| Windows-latest |
A build-time crate that generates a struct (CargoPkgInfo) to provide easy, structured access to your Rust project’s compile-time Cargo environment variables.
Once generated, the CargoPkgInfo struct provides access to the following metadata:
| Method | Description |
|---|---|
CargoPkgInfo::pkg_name() |
Package name -> Option<&'static str> |
CargoPkgInfo::crate_name() |
Crate name -> Option<&'static str> |
CargoPkgInfo::pkg_version() |
Full version -> Option<&'static str> |
CargoPkgInfo::version_major() |
Major version -> Option<&'static str> |
CargoPkgInfo::version_major_numeric() |
Major version -> Option<u32> |
CargoPkgInfo::version_minor() |
Minor version -> Option<&'static str> |
CargoPkgInfo::version_minor_numeric() |
Minor version -> Option<u32> |
CargoPkgInfo::version_patch() |
Patch version -> Option<&'static str> |
CargoPkgInfo::version_patch_numeric() |
Patch version -> Option<u32> |
CargoPkgInfo::version_pre() |
Pre-release version -> Option<&'static str> |
CargoPkgInfo::authors() |
Authors -> Option<&'static str> |
CargoPkgInfo::description() |
Description -> Option<&'static str> |
CargoPkgInfo::homepage() |
Homepage URL -> Option<&'static str> |
CargoPkgInfo::repository() |
Repository URL -> Option<&'static str> |
CargoPkgInfo::license() |
License name -> Option<&'static str> |
CargoPkgInfo::license_content() |
Full license text -> Option<&'static str> |
CargoPkgInfo::rust_version() |
Required Rust version -> Option<&'static str> |
CargoPkgInfo::readme_path() |
Path to README file -> Option<&'static str> |
CargoPkgInfo::build_target() |
Compilation target -> Option<&'static str> |
CargoPkgInfo::build_time_utc() |
Build timestamp UTC -> Option<u64> |
At compile time, Cargo sets various environment variables describing your project (version, authors, license, etc.). cargo-pkg-info-struct-builder gathers those variables from your project (not this library) and creates a file, at the path of your choosing, containing a CargoPkgInfo struct.
This struct provides simple, typed methods to retrieve metadata like:
Because this happens during the project’s build process, you get project-wide metadata embedded in the final binary—no additional steps required.
Add it as a build dependency:
cargo add cargo-pkg-info-struct-builder --build
Then, in your build.rs:
use cargo_pkg_info_struct_builder::inject_build_metadata;
use std::path::Path;
fn main() {
// Generate metadata into a Rust file.
// You can change the path of your choosing, and directories are
// auto-generated if they do not already exist.
let dest_path = Path::new("src").join("cargo_pkg_info.rs");
inject_build_metadata(dest_path.to_path_buf());
}
Now, in your main application:
// Assuming the `dest_path` is used from the above example.
mod cargo_pkg_info;
use cargo_pkg_info::CargoPkgInfo;
fn main() {
println!(
"{} v{} (Built on {})",
CargoPkgInfo::pkg_name(),
CargoPkgInfo::pkg_version(),
CargoPkgInfo::build_time_utc().unwrap_or(0),
);
}
This embeds project metadata into your binary at compile time—runtime environment variables are not used.
Unlike crates that retrieve package metadata at runtime, this crate:
When build.rs runs:
cargo_pkg_info.rs) containing a struct with methods that access this metadata.This is ideal for logging, debugging, and version tracking in Rust applications.
The generated file can be committed to version control, but it will remain unchanged unless the template itself is modified. Metadata updates do not change the file itself.
Licensed under MIT. See LICENSE for details.