deepslate_protocol/
version.rs1#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
8pub enum ProtocolVersion {
9 V1_21,
11 V1_21_2,
13 V1_21_4,
15 V1_21_5,
17 V1_21_6,
19 V1_21_7,
21 V1_21_9,
23 V1_21_11,
25}
26
27impl ProtocolVersion {
28 pub const MINIMUM: Self = Self::V1_21;
30
31 pub const MAXIMUM: Self = Self::V1_21_11;
33
34 pub const MINIMUM_PROTOCOL: i32 = 767;
36
37 pub const MAXIMUM_PROTOCOL: i32 = 774;
39
40 #[must_use]
44 pub const fn from_protocol(protocol: i32) -> Option<Self> {
45 match protocol {
46 767 => Some(Self::V1_21),
47 768 => Some(Self::V1_21_2),
48 769 => Some(Self::V1_21_4),
49 770 => Some(Self::V1_21_5),
50 771 => Some(Self::V1_21_6),
51 772 => Some(Self::V1_21_7),
52 773 => Some(Self::V1_21_9),
53 774 => Some(Self::V1_21_11),
54 _ => None,
55 }
56 }
57
58 #[must_use]
60 pub const fn protocol(self) -> i32 {
61 match self {
62 Self::V1_21 => 767,
63 Self::V1_21_2 => 768,
64 Self::V1_21_4 => 769,
65 Self::V1_21_5 => 770,
66 Self::V1_21_6 => 771,
67 Self::V1_21_7 => 772,
68 Self::V1_21_9 => 773,
69 Self::V1_21_11 => 774,
70 }
71 }
72
73 #[must_use]
75 pub const fn name(self) -> &'static str {
76 match self {
77 Self::V1_21 => "1.21-1.21.1",
78 Self::V1_21_2 => "1.21.2-1.21.3",
79 Self::V1_21_4 => "1.21.4",
80 Self::V1_21_5 => "1.21.5",
81 Self::V1_21_6 => "1.21.6",
82 Self::V1_21_7 => "1.21.7-1.21.8",
83 Self::V1_21_9 => "1.21.9-1.21.10",
84 Self::V1_21_11 => "1.21.11",
85 }
86 }
87
88 #[must_use]
90 pub const fn at_least(self, other: Self) -> bool {
91 self.protocol() >= other.protocol()
92 }
93
94 pub const SUPPORTED_VERSIONS: &'static str = "1.21-1.21.11";
96}
97
98impl std::fmt::Display for ProtocolVersion {
99 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
100 write!(f, "{} ({})", self.name(), self.protocol())
101 }
102}
103
104#[cfg(test)]
105mod tests {
106 use super::*;
107
108 #[test]
109 fn test_from_protocol() {
110 assert_eq!(
111 ProtocolVersion::from_protocol(767),
112 Some(ProtocolVersion::V1_21)
113 );
114 assert_eq!(
115 ProtocolVersion::from_protocol(774),
116 Some(ProtocolVersion::V1_21_11)
117 );
118 assert_eq!(ProtocolVersion::from_protocol(0), None);
119 assert_eq!(ProtocolVersion::from_protocol(766), None);
120 assert_eq!(ProtocolVersion::from_protocol(775), None);
121 }
122
123 #[test]
124 fn test_roundtrip() {
125 for proto in [767, 768, 769, 770, 771, 772, 773, 774] {
126 let version = ProtocolVersion::from_protocol(proto).unwrap();
127 assert_eq!(version.protocol(), proto);
128 }
129 }
130}