1use lazy_static::lazy_static;
10use std::sync::RwLock;
11
12lazy_static! {
13 pub static ref NETWORK_ID: RwLock<u8> = RwLock::new(1);
16
17 pub static ref IDENTIFY_NODE_VERSION_STR: RwLock<String> =
19 RwLock::new(format!(
20 "ant/node/{}/{}",
21 get_truncate_version_str(),
22 *NETWORK_ID.read().expect("Failed to obtain read lock for NETWORK_ID"),
23 ));
24
25 pub static ref IDENTIFY_CLIENT_VERSION_STR: RwLock<String> =
27 RwLock::new(format!(
28 "ant/client/{}/{}",
29 get_truncate_version_str(),
30 *NETWORK_ID.read().expect("Failed to obtain read lock for NETWORK_ID"),
31 ));
32
33 pub static ref REQ_RESPONSE_VERSION_STR: RwLock<String> =
35 RwLock::new(format!(
36 "/ant/{}/{}",
37 get_truncate_version_str(),
38 *NETWORK_ID.read().expect("Failed to obtain read lock for NETWORK_ID"),
39 ));
40
41 pub static ref IDENTIFY_PROTOCOL_STR: RwLock<String> =
43 RwLock::new(format!(
44 "ant/{}/{}",
45 get_truncate_version_str(),
46 *NETWORK_ID.read().expect("Failed to obtain read lock for NETWORK_ID"),
47 ));
48}
49
50pub fn set_network_id(id: u8) {
56 info!("Setting network id to: {id}");
57 let mut network_id = NETWORK_ID
58 .write()
59 .expect("Failed to obtain write lock for NETWORK_ID");
60 *network_id = id;
61 info!("Network id set to: {id}");
62}
63
64pub fn get_network_id() -> String {
66 format!(
67 "{}",
68 *NETWORK_ID
69 .read()
70 .expect("Failed to obtain read lock for NETWORK_ID")
71 )
72}
73
74pub fn get_truncate_version_str() -> String {
77 let version_str = env!("CARGO_PKG_VERSION");
78 let parts = version_str.split('.').collect::<Vec<_>>();
79 if parts.len() >= 2 {
80 format!("{}.{}", parts[0], parts[1])
81 } else {
82 panic!("Cannot obtain truncated version str for {version_str:?}: {parts:?}");
83 }
84}
85
86#[cfg(test)]
87mod tests {
88 use super::*;
89
90 #[test]
91 fn test_print_version_strings() -> Result<(), Box<dyn std::error::Error>> {
92 set_network_id(3);
93 println!(
94 "\nIDENTIFY_NODE_VERSION_STR: {}",
95 *IDENTIFY_NODE_VERSION_STR
96 .read()
97 .expect("Failed to obtain read lock for IDENTIFY_NODE_VERSION_STR")
98 );
99 println!(
100 "IDENTIFY_CLIENT_VERSION_STR: {}",
101 *IDENTIFY_CLIENT_VERSION_STR
102 .read()
103 .expect("Failed to obtain read lock for IDENTIFY_CLIENT_VERSION_STR")
104 );
105 println!(
106 "REQ_RESPONSE_VERSION_STR: {}",
107 *REQ_RESPONSE_VERSION_STR
108 .read()
109 .expect("Failed to obtain read lock for REQ_RESPONSE_VERSION_STR")
110 );
111 println!(
112 "IDENTIFY_PROTOCOL_STR: {}",
113 *IDENTIFY_PROTOCOL_STR
114 .read()
115 .expect("Failed to obtain read lock for IDENTIFY_PROTOCOL_STR")
116 );
117
118 let truncated = get_truncate_version_str();
120 println!("\nTruncated version: {truncated}");
121
122 let network_id = get_network_id();
124 println!("Network ID string: {network_id}");
125
126 Ok(())
127 }
128}