#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum ProtocolVersion {
V1_21,
V1_21_2,
V1_21_4,
V1_21_5,
V1_21_6,
V1_21_7,
V1_21_9,
V1_21_11,
}
impl ProtocolVersion {
pub const MINIMUM: Self = Self::V1_21;
pub const MAXIMUM: Self = Self::V1_21_11;
pub const MINIMUM_PROTOCOL: i32 = 767;
pub const MAXIMUM_PROTOCOL: i32 = 774;
#[must_use]
pub const fn from_protocol(protocol: i32) -> Option<Self> {
match protocol {
767 => Some(Self::V1_21),
768 => Some(Self::V1_21_2),
769 => Some(Self::V1_21_4),
770 => Some(Self::V1_21_5),
771 => Some(Self::V1_21_6),
772 => Some(Self::V1_21_7),
773 => Some(Self::V1_21_9),
774 => Some(Self::V1_21_11),
_ => None,
}
}
#[must_use]
pub const fn protocol(self) -> i32 {
match self {
Self::V1_21 => 767,
Self::V1_21_2 => 768,
Self::V1_21_4 => 769,
Self::V1_21_5 => 770,
Self::V1_21_6 => 771,
Self::V1_21_7 => 772,
Self::V1_21_9 => 773,
Self::V1_21_11 => 774,
}
}
#[must_use]
pub const fn name(self) -> &'static str {
match self {
Self::V1_21 => "1.21-1.21.1",
Self::V1_21_2 => "1.21.2-1.21.3",
Self::V1_21_4 => "1.21.4",
Self::V1_21_5 => "1.21.5",
Self::V1_21_6 => "1.21.6",
Self::V1_21_7 => "1.21.7-1.21.8",
Self::V1_21_9 => "1.21.9-1.21.10",
Self::V1_21_11 => "1.21.11",
}
}
#[must_use]
pub const fn at_least(self, other: Self) -> bool {
self.protocol() >= other.protocol()
}
pub const SUPPORTED_VERSIONS: &'static str = "1.21-1.21.11";
}
impl std::fmt::Display for ProtocolVersion {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{} ({})", self.name(), self.protocol())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_from_protocol() {
assert_eq!(
ProtocolVersion::from_protocol(767),
Some(ProtocolVersion::V1_21)
);
assert_eq!(
ProtocolVersion::from_protocol(774),
Some(ProtocolVersion::V1_21_11)
);
assert_eq!(ProtocolVersion::from_protocol(0), None);
assert_eq!(ProtocolVersion::from_protocol(766), None);
assert_eq!(ProtocolVersion::from_protocol(775), None);
}
#[test]
fn test_roundtrip() {
for proto in [767, 768, 769, 770, 771, 772, 773, 774] {
let version = ProtocolVersion::from_protocol(proto).unwrap();
assert_eq!(version.protocol(), proto);
}
}
}