pulse_protocol/
version.rs1use serde::{Deserialize, Serialize};
6
7pub const PROTOCOL_VERSION: Version = Version { major: 1, minor: 0 };
9
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
12pub struct Version {
13 pub major: u8,
15 pub minor: u8,
17}
18
19impl Version {
20 #[must_use]
22 pub const fn new(major: u8, minor: u8) -> Self {
23 Self { major, minor }
24 }
25
26 #[must_use]
30 pub fn is_compatible_with(&self, other: &Version) -> bool {
31 self.major == other.major
32 }
33}
34
35impl std::fmt::Display for Version {
36 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
37 write!(f, "{}.{}", self.major, self.minor)
38 }
39}
40
41impl Default for Version {
42 fn default() -> Self {
43 PROTOCOL_VERSION
44 }
45}
46
47#[cfg(test)]
48mod tests {
49 use super::*;
50
51 #[test]
52 fn test_version_compatibility() {
53 let v1_0 = Version::new(1, 0);
54 let v1_1 = Version::new(1, 1);
55 let v2_0 = Version::new(2, 0);
56
57 assert!(v1_0.is_compatible_with(&v1_1));
58 assert!(v1_1.is_compatible_with(&v1_0));
59 assert!(!v1_0.is_compatible_with(&v2_0));
60 }
61
62 #[test]
63 fn test_version_display() {
64 let v = Version::new(1, 2);
65 assert_eq!(v.to_string(), "1.2");
66 }
67}