Skip to main content

memlink_protocol/
version.rs

1//! Protocol versioning and compatibility.
2//!
3//! Defines ProtocolVersion struct with major/minor/features fields
4//! and constants for V1_0, V1_1, V1_2 supported versions.
5
6use crate::features::{BATCHING, STREAMING, FEATURE_NONE};
7
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
9pub struct ProtocolVersion {
10    pub major: u16,
11    pub minor: u16,
12    pub features: u32,
13}
14
15impl ProtocolVersion {
16    pub const fn new(major: u16, minor: u16, features: u32) -> Self {
17        Self {
18            major,
19            minor,
20            features,
21        }
22    }
23
24    pub const fn major(&self) -> u16 {
25        self.major
26    }
27
28    pub const fn minor(&self) -> u16 {
29        self.minor
30    }
31
32    pub const fn features(&self) -> u32 {
33        self.features
34    }
35
36    pub fn has_feature(&self, feature: u32) -> bool {
37        self.features & feature != 0
38    }
39
40    pub fn enable_feature(&mut self, feature: u32) {
41        self.features |= feature;
42    }
43
44    pub fn disable_feature(&mut self, feature: u32) {
45        self.features &= !feature;
46    }
47
48    pub fn is_v1_0(&self) -> bool {
49        self.major == 1 && self.minor == 0
50    }
51
52    pub fn is_compatible_with(&self, other: &ProtocolVersion) -> bool {
53        self.major == other.major
54    }
55
56    pub fn compare(&self, other: &ProtocolVersion) -> i32 {
57        if self.major != other.major {
58            return self.major as i32 - other.major as i32;
59        }
60        if self.minor != other.minor {
61            return self.minor as i32 - other.minor as i32;
62        }
63        0
64    }
65}
66
67impl core::fmt::Display for ProtocolVersion {
68    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
69        write!(f, "{}.{}", self.major, self.minor)
70    }
71}
72
73pub const V1_0: ProtocolVersion = ProtocolVersion::new(1, 0, FEATURE_NONE);
74
75pub const V1_1: ProtocolVersion = ProtocolVersion::new(1, 1, STREAMING);
76
77pub const V1_2: ProtocolVersion = ProtocolVersion::new(1, 2, STREAMING | BATCHING);
78
79pub const SUPPORTED_VERSIONS: &[ProtocolVersion] = &[V1_0, V1_1, V1_2];
80
81pub const CURRENT_VERSION: ProtocolVersion = V1_2;
82
83pub const MIN_VERSION: ProtocolVersion = V1_0;
84
85pub const MAX_VERSION: ProtocolVersion = V1_2;