calimero_primitives/
version.rs1use serde::{Deserialize, Serialize};
14
15#[derive(Clone, Debug, Default, Serialize, Deserialize)]
20#[cfg_attr(
21 feature = "borsh",
22 derive(borsh::BorshSerialize, borsh::BorshDeserialize)
23)]
24pub struct Version {
25 pub version: String,
27 pub build: String,
29 pub commit: String,
31 pub rustc_version: String,
33}
34
35impl Version {
36 pub fn from_build_env(version: &str, build: &str, commit: &str, rustc_version: &str) -> Self {
38 Self {
39 version: version.to_string(),
40 build: build.to_string(),
41 commit: commit.to_string(),
42 rustc_version: rustc_version.to_string(),
43 }
44 }
45}
46
47impl std::fmt::Display for Version {
48 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
49 write!(
50 f,
51 "(release {}) (build {}) (commit {}) (rustc {})",
52 self.version, self.build, self.commit, self.rustc_version,
53 )
54 }
55}
56
57#[cfg(test)]
58mod tests {
59 use super::Version;
60
61 #[test]
62 fn from_build_env_sets_all_fields() {
63 let version = Version::from_build_env("1.2.3", "v1.2.3-5-gabc123", "abc123", "1.88.0");
64
65 assert_eq!(version.version, "1.2.3");
66 assert_eq!(version.build, "v1.2.3-5-gabc123");
67 assert_eq!(version.commit, "abc123");
68 assert_eq!(version.rustc_version, "1.88.0");
69 }
70
71 #[test]
72 fn display_uses_expected_format() {
73 let version = Version::from_build_env("1.2.3", "v1.2.3-5-gabc123", "abc123", "1.88.0");
74
75 assert_eq!(
76 version.to_string(),
77 "(release 1.2.3) (build v1.2.3-5-gabc123) (commit abc123) (rustc 1.88.0)"
78 );
79 }
80}