pulse_protocol/
version.rs

1//! Protocol versioning for Pulse.
2//!
3//! This module handles protocol version negotiation and compatibility.
4
5use serde::{Deserialize, Serialize};
6
7/// Current protocol version.
8pub const PROTOCOL_VERSION: Version = Version { major: 1, minor: 0 };
9
10/// Protocol version information.
11#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
12pub struct Version {
13    /// Major version - breaking changes increment this.
14    pub major: u8,
15    /// Minor version - backwards-compatible changes increment this.
16    pub minor: u8,
17}
18
19impl Version {
20    /// Create a new version.
21    #[must_use]
22    pub const fn new(major: u8, minor: u8) -> Self {
23        Self { major, minor }
24    }
25
26    /// Check if this version is compatible with another version.
27    ///
28    /// Versions are compatible if they share the same major version.
29    #[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}