Skip to main content

shadow_formatted_version/
lib.rs

1#![allow(clippy::needless_doctest_main)] //needed to describe build.rs in README.md
2#![doc = include_str!("../README.md")]
3
4/// Generates a constant containing a preformatted version string.
5///
6/// Use this together with [shadow-rs](https://crates.io/crates/shadow-rs), by placing the following into your `main.rs` or `lib.rs`:
7/// ```text
8/// shadow_rs::shadow!(build);
9/// shadow_formatted_version::from_shadow!(build);
10/// ```
11/// The name "build" is of the module that shadow-rs generates. You can choose a different name, if you prefer.
12#[macro_export]
13macro_rules! from_shadow {
14    ($shadow_module:ident) => {
15        /// Detailed crate version information in a preformatted block.
16        ///
17        /// It looks like this, for example:
18        /// ```text
19        ///   Version:       0.1.0
20        ///   Target:        x86_64-unknown-linux-gnu
21        ///   Commit:        8f29a49b54ca3e94070d244511d8e4a64a69f083
22        ///   Commit-Date:   2023-12-31 23:59:59 +00:00
23        ///   Build-Date:    2024-12-31 23:59:59 +00:00
24        ///   Build-Clean:   No uncommitted changes in build.
25        ///   Rust-Version:  rustc 1.76.0 (07dca489a 2024-02-04)
26        /// ```
27        ///
28        /// See [shadow_formatted_version](https://crates.io/crates/shadow-formatted-version) for more information.
29        pub(crate) const FORMATTED_VERSION: &'static str =
30            shadow_rs::formatcp!(
31"
32  Version:       {crate_version}
33  Target:        {build_target}
34  Commit:        {commit}
35  Commit-Date:   {commit_date}
36  Build-Date:    {build_date}
37  Build-Clean:   {build_clean}
38  Rust-Version:  {rust_version}
39",
40            crate_version = $shadow_module::PKG_VERSION,
41            build_target = $shadow_module::BUILD_TARGET,
42            commit = $shadow_module::COMMIT_HASH,
43            commit_date = $shadow_module::COMMIT_DATE,
44            build_date = $shadow_module::BUILD_TIME,
45            build_clean = if $shadow_module::GIT_CLEAN {
46                "No uncommitted changes in build."
47            } else {
48                "Distribution was built with uncommitted changes! Commit does not reflect actual application code!"
49            },
50            rust_version = $shadow_module::RUST_VERSION,
51
52        );
53    }
54}