amaters_server/
version.rs1pub const MIN_COMPATIBLE_VERSION: (u64, u64, u64) = (0, 2, 0);
10
11pub const CURRENT_VERSION: (u64, u64, u64) = (0, 2, 2);
13
14pub fn is_compatible(peer_version: (u64, u64, u64)) -> bool {
22 peer_version.0 == CURRENT_VERSION.0 && peer_version.1 >= MIN_COMPATIBLE_VERSION.1
23}
24
25#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, PartialEq, Eq)]
31pub struct VersionHandshake {
32 pub version: (u64, u64, u64),
34 pub min_compatible: (u64, u64, u64),
36 pub build_id: String,
38}
39
40impl VersionHandshake {
41 pub fn current() -> Self {
43 Self {
44 version: CURRENT_VERSION,
45 min_compatible: MIN_COMPATIBLE_VERSION,
46 build_id: env!("CARGO_PKG_VERSION").to_string(),
47 }
48 }
49
50 pub fn is_compatible_with(&self, other: &VersionHandshake) -> bool {
55 is_compatible(other.version)
57 }
58}
59
60#[cfg(test)]
63mod tests {
64 use super::*;
65
66 #[test]
67 fn test_current_version_compatible_with_itself() {
68 assert!(is_compatible(CURRENT_VERSION));
69 }
70
71 #[test]
72 fn test_older_minor_version_compatible() {
73 let old_minor = (0u64, 2u64, 0u64);
75 assert!(is_compatible(old_minor));
76
77 let mid_minor = (0u64, 2u64, 1u64);
79 assert!(is_compatible(mid_minor));
80 }
81
82 #[test]
83 fn test_different_major_version_incompatible() {
84 let v1 = (1u64, 0u64, 0u64);
85 assert!(!is_compatible(v1));
86
87 let v2 = (2u64, 2u64, 0u64);
88 assert!(!is_compatible(v2));
89 }
90
91 #[test]
92 fn test_minor_below_minimum_incompatible() {
93 let too_old = (0u64, 1u64, 99u64);
95 assert!(!is_compatible(too_old));
96 }
97
98 #[test]
99 fn test_version_handshake_serialization() {
100 let hs = VersionHandshake::current();
101 let json = serde_json::to_string(&hs).expect("serialise");
102 let back: VersionHandshake = serde_json::from_str(&json).expect("deserialise");
103 assert_eq!(hs, back);
104 }
105
106 #[test]
107 fn test_version_handshake_incompatible_major() {
108 let current = VersionHandshake::current();
109 let other = VersionHandshake {
110 version: (1u64, 0u64, 0u64),
111 min_compatible: (1u64, 0u64, 0u64),
112 build_id: "old-major".to_string(),
113 };
114 assert!(!current.is_compatible_with(&other));
115 }
116
117 #[test]
118 fn test_version_handshake_compatible_peer() {
119 let current = VersionHandshake::current();
120 let peer = VersionHandshake {
121 version: (0u64, 2u64, 1u64),
122 min_compatible: MIN_COMPATIBLE_VERSION,
123 build_id: "peer".to_string(),
124 };
125 assert!(current.is_compatible_with(&peer));
126 }
127}