shadow-rs
shadow-rs copied to clipboard
Consider generating a struct, or otherwise simplify usage
TL;DR: make it easier to write shared code interfacing shadow
's output.
Currently, shadow
only generates pub const VAR: &str
variables - it's great as is, but is also quite a bit unwieldy, leading to a lot of boilerplate in multi-binary projects.
There are two main benefits to this approach:
- easier manipulation and code decoupling - if I want to write display methods, I could have functions accepting a reference to that struct
- API documentation is easier - just look up said structure
This also ties into what @epage wrote in https://github.com/baoyachi/shadow-rs/issues/75#issuecomment-1048035937 - such a struct would work as an accessor. Or rather, structs, as it would be then split per-source (git, rust, etc).
In my project I have a struct (not using all variables), which could easily be made as a part of code generation, like this:
pub struct BuildInfo {
pub version_major: &'static str,
pub version_minor: &'static str,
pub version_patch: &'static str,
pub version_pre: &'static str,
pub git_commit_hash: &'static str,
pub git_short_hash: &'static str,
pub git_clean: bool,
pub build_type: &'static str,
pub cargo_version: &'static str,
pub rust_version: &'static str,
pub rust_channel: &'static str,
pub target: &'static str,
}
which is then initialized with:
pub(crate) const BUILD_INFO: BuildInfo = BuildInfo {
version_major: PKG_VERSION_MAJOR,
version_minor: PKG_VERSION_MINOR,
version_patch: PKG_VERSION_PATCH,
version_pre: PKG_VERSION_PRE,
git_commit_hash: COMMIT_HASH,
git_short_hash: SHORT_COMMIT,
git_clean: GIT_CLEAN,
build_type: BUILD_RUST_CHANNEL,
cargo_version: CARGO_VERSION,
rust_version: RUST_VERSION,
rust_channel: RUST_CHANNEL,
target: BUILD_TARGET,
};
That's a good suggestion. I'll think about it. @jaskij THX.
solution ref : https://github.com/aptos-labs/aptos-core/blob/c7978de51c52a5f6517c88f3ae24d9c241d4beb7/crates/aptos-telemetry/src/build_information.rs