12 releases
new 0.1.0-alpha9 | Feb 17, 2025 |
---|---|
0.1.0-alpha6 | Feb 16, 2025 |
0.1.0-alpha11 | Feb 18, 2025 |
#154 in Build Utils
721 downloads per month
24KB
180 lines
cargo-pkg-info-struct-builder
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.
Available Metadata Methods
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> |
Overview
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:
- Version (major, minor, patch, pre-release)
- Authors, description, homepage, and repository
- License information (including file contents)
- Build target and a compile-time build timestamp
Because this happens during the project’s build process, you get project-wide metadata embedded in the final binary—no additional steps required.
Quick Start
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.
Why Compile-Time Injection?
Unlike crates that retrieve package metadata at runtime, this crate:
- Embeds metadata at compile time to avoid file I/O in production.
- Captures the metadata of your project, not the dependency itself.
- Works with any Rust project (including WASM) without requiring Cargo to be installed at runtime.
How It Works
When build.rs
runs:
- It reads package metadata from Cargo environment variables.
- It generates a Rust source file (
cargo_pkg_info.rs
) containing a struct with methods that access this metadata. - The file is compiled into your project, allowing runtime access to compiled environment info.
This is ideal for logging, debugging, and version tracking in Rust applications.
Notes
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.
License
Licensed under MIT. See LICENSE
for details.
Dependencies
~310–600KB
~13K SLoC