/ node / rest / src / version.rs
version.rs
 1  // Copyright (c) 2025 ADnet Contributors
 2  // This file is part of the AlphaOS library.
 3  
 4  // Licensed under the Apache License, Version 2.0 (the "License");
 5  // you may not use this file except in compliance with the License.
 6  // You may obtain a copy of the License at:
 7  
 8  // http://www.apache.org/licenses/LICENSE-2.0
 9  
10  // Unless required by applicable law or agreed to in writing, software
11  // distributed under the License is distributed on an "AS IS" BASIS,
12  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  // See the License for the specific language governing permissions and
14  // limitations under the License.
15  
16  use serde::Serialize;
17  use std::sync::OnceLock;
18  
19  // Include the generated build information
20  mod built_info {
21      include!(concat!(env!("OUT_DIR"), "/built.rs"));
22  }
23  
24  // Cache for version info to avoid repeated string allocations
25  static VERSION_INFO: OnceLock<VersionInfo> = OnceLock::new();
26  
27  #[derive(Clone, Debug, Serialize)]
28  pub struct VersionInfo {
29      /// The version from Cargo.toml
30      pub version: String,
31      /// Git commit hash
32      pub git_commit: String,
33      /// Git branch name
34      pub git_branch: String,
35  }
36  
37  impl VersionInfo {
38      /// Get the cached version information
39      pub fn get() -> &'static VersionInfo {
40          VERSION_INFO.get_or_init(|| VersionInfo {
41              version: built_info::PKG_VERSION.to_string(),
42              git_commit: built_info::GIT_COMMIT_HASH.unwrap_or("unknown").to_string(),
43              git_branch: built_info::GIT_HEAD_REF.unwrap_or("unknown").to_string(),
44          })
45      }
46  }