parity_version/
lib.rs

1// Copyright 2022 Conflux Foundation. All rights reserved.
2// Conflux is free software and distributed under GNU General Public License.
3// See http://www.gnu.org/licenses/
4
5// Copyright 2015-2020 Parity Technologies (UK) Ltd.
6// This file is part of OpenEthereum.
7
8// OpenEthereum is free software: you can redistribute it and/or modify
9// it under the terms of the GNU General Public License as published by
10// the Free Software Foundation, either version 3 of the License, or
11// (at your option) any later version.
12
13// OpenEthereum is distributed in the hope that it will be useful,
14// but WITHOUT ANY WARRANTY; without even the implied warranty of
15// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16// GNU General Public License for more details.
17
18// You should have received a copy of the GNU General Public License
19// along with OpenEthereum.  If not, see <http://www.gnu.org/licenses/>.
20
21//! Parity version specific information.
22
23use target_info::Target;
24
25/// Get the platform identifier.
26pub fn platform() -> String {
27    let env = Target::env();
28    let env_dash = if env.is_empty() { "" } else { "-" };
29    format!("{}-{}{}{}", Target::arch(), Target::os(), env_dash, env)
30}
31
32/// Get the standard version string for this software.
33pub fn version(crate_version: &str) -> String {
34    let sha3 = env!("VERGEN_GIT_SHA");
35    let sha3_dash = if sha3.is_empty() { "" } else { "-" };
36    let commit_date = env!("VERGEN_GIT_COMMIT_DATE").replace("-", "");
37    let date_dash = if commit_date.is_empty() { "" } else { "-" };
38    format!(
39        "conflux-rust/v{}{}{}{}{}/{}/rustc{}",
40        crate_version,
41        sha3_dash,
42        sha3,
43        date_dash,
44        commit_date,
45        platform(),
46        env!("VERGEN_RUSTC_SEMVER"),
47    )
48}
49
50#[macro_export]
51macro_rules! conflux_client_version {
52    () => {
53        parity_version::version(env!("CARGO_PKG_VERSION"))
54    };
55}
56
57#[cfg(test)]
58
59mod tests {
60    use crate::{platform, version};
61
62    use super::Target;
63
64    #[test]
65    fn test_platform() {
66        let platform = platform();
67
68        assert!(!platform.is_empty());
69        assert!(platform.contains(Target::arch()));
70        assert!(platform.contains(Target::os()));
71    }
72
73    #[test]
74    fn test_version() {
75        let test_version = "0.0.0";
76        let version_string = version(test_version);
77        // example:  conflux-rust/v0.0.0-b7cca2a-20250423/x86_64-linux-gnu/
78        // rustc1.77.2
79
80        println!("version_string: {}", version_string);
81        assert!(version_string
82            .starts_with(&format!("conflux-rust/v{}", test_version)));
83        assert!(version_string.contains(&format!("{}", platform())));
84
85        let sha = env!("VERGEN_GIT_SHA");
86
87        assert!(version_string.contains(&format!("-{}", sha)));
88        let commit_date = env!("VERGEN_GIT_COMMIT_DATE").replace("-", "");
89
90        assert_eq!(commit_date.len(), 8);
91        assert!(version_string.contains(&format!("-{}", commit_date)));
92
93        let rust_version = env!("VERGEN_RUSTC_SEMVER");
94        assert!(version_string.contains(&format!("/rustc{}", rust_version)));
95    }
96}