• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

baoyachi / shadow-rs / 16680866786

01 Aug 2025 05:04PM UTC coverage: 76.723% (+0.3%) from 76.417%
16680866786

push

github

web-flow
Update Cargo.toml

501 of 653 relevant lines covered (76.72%)

1.12 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.91
/src/gen_const.rs
1
use crate::{Shadow, CARGO_CLIPPY_ALLOW_ALL, CARGO_METADATA};
2

3
macro_rules! gen_const {
4
    ($fn_name:ident, $fn_body:expr) => {
5
        pub fn $fn_name() -> String {
3✔
6
            let (doc, content) = $fn_body;
3✔
7
            format!(
3✔
8
                "{}\n{}\n{}\n{}\n",
9
                doc,
10
                "#[allow(dead_code,missing_docs)]",
11
                $crate::CARGO_CLIPPY_ALLOW_ALL,
12
                content
13
            )
14
        }
15
    };
16
}
17

18
const VERSION_BRANCH_CONST: (&str, &str) = (
19
    r#"/// A long version string describing the project.
20
/// The version string contains the package version, branch, commit hash, build time, and build environment on separate lines.
21
/// This constant is suitable for printing to the user."#,
22
    r##"pub const VERSION:&str = shadow_rs::formatcp!(r#"
23
pkg_version:{}
24
branch:{}
25
commit_hash:{}
26
build_time:{}
27
build_env:{},{}"#,PKG_VERSION, BRANCH, SHORT_COMMIT, BUILD_TIME, RUST_VERSION, RUST_CHANNEL
28
);"##,
29
);
30

31
const VERSION_TAG_CONST: (&str, &str) = (
32
    r#"/// A long version string describing the project.
33
/// The version string contains the package version, current Git tag, commit hash, build time, and build environment on separate lines.
34
/// This constant is suitable for printing to the user."#,
35
    r##"pub const VERSION:&str = shadow_rs::formatcp!(r#"
36
pkg_version:{}
37
tag:{}
38
commit_hash:{}
39
build_time:{}
40
build_env:{},{}"#,PKG_VERSION, TAG, SHORT_COMMIT, BUILD_TIME, RUST_VERSION, RUST_CHANNEL
41
);"##,
42
);
43

44
const CLAP_LONG_VERSION_BRANCH_CONST: (&str, &str) = (
45
    r#"/// A long version string describing the project.
46
/// The version string contains the package version, branch, commit hash, build time, and build environment on separate lines.
47
/// This constant is intended to be used by clap or other CLI tools as a long version string."#,
48
    r##"pub const CLAP_LONG_VERSION:&str = shadow_rs::formatcp!(r#"{}
49
branch:{}
50
commit_hash:{}
51
build_time:{}
52
build_env:{},{}"#,PKG_VERSION, BRANCH, SHORT_COMMIT, BUILD_TIME, RUST_VERSION, RUST_CHANNEL
53
);"##,
54
);
55

56
const CLAP_LONG_VERSION_TAG_CONST: (&str, &str) = (
57
    r#"/// A long version string describing the project.
58
/// The version string contains the package version, current Git tag, commit hash, build time, and build environment on separate lines.
59
/// This constant is intended to be used by clap or other CLI tools as a long version string."#,
60
    r##"pub const CLAP_LONG_VERSION:&str = shadow_rs::formatcp!(r#"{}
61
tag:{}
62
commit_hash:{}
63
build_time:{}
64
build_env:{},{}"#,PKG_VERSION, TAG, SHORT_COMMIT, BUILD_TIME, RUST_VERSION, RUST_CHANNEL
65
);"##,
66
);
67

68
gen_const!(version_branch_const, VERSION_BRANCH_CONST);
×
69
gen_const!(version_tag_const, VERSION_TAG_CONST);
1✔
70
gen_const!(
71
    clap_long_version_branch_const,
72
    CLAP_LONG_VERSION_BRANCH_CONST
1✔
73
);
74
gen_const!(clap_long_version_tag_const, CLAP_LONG_VERSION_TAG_CONST);
1✔
75

76
pub(crate) const BUILD_CONST_VERSION: &str = "VERSION";
77
pub(crate) const BUILD_CONST_CLAP_LONG_VERSION: &str = "CLAP_LONG_VERSION";
78

79
#[allow(dead_code)]
80
pub(crate) fn cargo_metadata_fn(shadow: &Shadow) -> String {
1✔
81
    if !shadow.map.contains_key(CARGO_METADATA) {
1✔
82
        return "".to_string();
1✔
83
    }
84
    format!(
1✔
85
        r#"
86
use shadow_rs::cargo_metadata::Metadata;
87
use shadow_rs::serde_json;
88

89
/// Attempts to parse the Cargo package metadata from the generated constant CARGO_METADATA.
90
///
91
/// Returns a `Metadata` struct containing information about the Cargo workspace,
92
/// such as details about the packages and their dependencies.
93
///
94
/// # Return Values
95
/// - `Ok(Metadata)`: Contains the parsed metadata if successful.
96
/// - `Err(String)`: Returns an error message if converting the environment variable to a UTF-8 string or parsing JSON fails.
97
#[allow(dead_code)]
98
{CARGO_CLIPPY_ALLOW_ALL}
99
pub fn cargo_metadata() -> Result<Metadata, String> {{
100
    let metadata_json = std::str::from_utf8(CARGO_METADATA.as_ref()).map_err(|err| format!("generate 'CARGO_METADATA' value from UTF8 error:{{}}",err))?;
101
    let meta: Metadata = serde_json::from_str(metadata_json).map_err(|err| err.to_string())?;
102
    Ok(meta)
103
}}"#
104
    )
105
}
106

107
#[cfg(test)]
108
mod tests {
109
    use super::*;
110

111
    #[test]
112
    fn test_version_fn() {
113
        assert!(version_tag_const().contains(VERSION_TAG_CONST.0));
114
        assert!(clap_long_version_branch_const().contains(CLAP_LONG_VERSION_BRANCH_CONST.1));
115
    }
116

117
    #[cfg(feature = "metadata")]
118
    #[test]
119
    fn test_metadata() {
120
        let metadata_json = r#"{"metadata":null,"packages":[{"authors":["baoyachi"],"categories":["development-tools","development-tools::build-utils"],"default_run":null,"dependencies":[{"features":[],"kind":"dev","name":"xshell","optional":false,"registry":null,"rename":null,"req":"^0.2.7","source":"registry+https://github.com/rust-lang/crates.io-index","target":null,"uses_default_features":true}],"description":"get build model is debug","documentation":null,"edition":"2018","features":{"default":["std"],"std":[]},"homepage":null,"id":"registry+https://github.com/rust-lang/crates.io-index#is_debug@1.1.0","keywords":["build","build-model","model","debug","release"],"license":"MIT AND Apache-2.0","license_file":null,"links":null,"manifest_path":".cargo/registry/src/index.crates.io-1949cf8c6b5b557f/is_debug-1.1.0/Cargo.toml","metadata":null,"name":"is_debug","publish":null,"readme":"README.md","repository":"https://github.com/baoyachi/rust_is_debug","rust_version":null,"source":"registry+https://github.com/rust-lang/crates.io-index","targets":[{"crate_types":["lib"],"doc":true,"doctest":true,"edition":"2018","kind":["lib"],"name":"is_debug","src_path":".cargo/registry/src/index.crates.io-1949cf8c6b5b557f/is_debug-1.1.0/lib.rs","test":true}],"version":"1.1.0"},{"authors":[],"categories":[],"default_run":null,"dependencies":[{"features":[],"kind":null,"name":"is_debug","optional":false,"registry":null,"rename":null,"req":"^1.1.0","source":"registry+https://github.com/rust-lang/crates.io-index","target":null,"uses_default_features":true}],"description":null,"documentation":null,"edition":"2024","features":{},"homepage":null,"id":"path+file://demo#0.1.0","keywords":[],"license":null,"license_file":null,"links":null,"manifest_path":"demo/Cargo.toml","metadata":null,"name":"rust-demo","publish":null,"readme":null,"repository":null,"rust_version":null,"source":null,"targets":[{"crate_types":["bin"],"doc":true,"doctest":false,"edition":"2024","kind":["bin"],"name":"rust-demo","src_path":"demo/src/main.rs","test":true}],"version":"0.1.0"}],"resolve":{"nodes":[{"dependencies":[],"deps":[],"features":["default","std"],"id":"registry+https://github.com/rust-lang/crates.io-index#is_debug@1.1.0"},{"dependencies":["registry+https://github.com/rust-lang/crates.io-index#is_debug@1.1.0"],"deps":[{"dep_kinds":[{"kind":null,"target":null}],"name":"is_debug","pkg":"registry+https://github.com/rust-lang/crates.io-index#is_debug@1.1.0"}],"features":[],"id":"path+file://demo#0.1.0"}],"root":"path+file://demo#0.1.0"},"target_directory":"demo/target","version":1,"workspace_default_members":["path+file://demo#0.1.0"],"workspace_members":["path+file://demo#0.1.0"],"workspace_root":"demo"}"#;
121

122
        let meta: cargo_metadata::Metadata = serde_json::from_str(metadata_json).unwrap();
123
        assert_eq!(meta.packages.len(), 2);
124
        assert!(meta.workspace_metadata.is_null());
125
    }
126
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc