Skip to main content

ghast_protocol/
versions.rs

1//! The protocol version table: one row per protocol number this crate knows.
2//!
3//! A row is the protocol layer's view of a version: its number, the generated module, the
4//! game versions that speak it (e.g. 775 = 26.1, 26.1.1 and 26.1.2) and the few wire facts
5//! that differ between versions outside the packet definitions. Rows of versions whose
6//! Cargo feature is off stay in the table with `enabled == false`, so a caller can tell
7//! "unknown" from "not compiled in".
8//!
9//! Layers above (data sets, physics rules) keep their own columns keyed by the protocol
10//! number or the game version; [`ProtocolVersion`] is `#[non_exhaustive]`, so columns of
11//! this layer can be added without a breaking change.
12
13use crate::types::PositionLayout;
14
15/// One protocol number.
16#[derive(Debug, Clone, Copy, PartialEq, Eq)]
17#[non_exhaustive]
18pub struct ProtocolVersion {
19    /// Protocol number sent in the handshake.
20    pub protocol: i32,
21    /// Name of the generated module (`"v1_8"`, `"v26_1"`).
22    pub module: &'static str,
23    /// Cargo feature that compiles the module in.
24    pub feature: &'static str,
25    /// The version the module is named after and tested with (`"1.8.9"`).
26    pub name: &'static str,
27    /// Every release that speaks this protocol, oldest first (from minecraft-data's
28    /// `protocolVersions.json`).
29    pub game_versions: &'static [&'static str],
30    /// Bit layout of packed block positions.
31    pub position_layout: PositionLayout,
32    /// Whether the version has the configuration state (1.20.2+).
33    pub configuration: bool,
34    /// Whether the module is compiled in (its feature is on).
35    pub enabled: bool,
36}
37
38include!("generated/versions_table.rs");
39
40/// The row of a protocol number.
41#[must_use]
42pub fn by_protocol(protocol: i32) -> Option<&'static ProtocolVersion> {
43    TABLE.iter().find(|v| v.protocol == protocol)
44}
45
46/// The row of a game version name (`"1.8.8"`, `"26.1.2"`).
47#[must_use]
48pub fn by_game_version(name: &str) -> Option<&'static ProtocolVersion> {
49    TABLE.iter().find(|v| v.game_versions.contains(&name))
50}
51
52/// The rows that are compiled in.
53pub fn enabled() -> impl Iterator<Item = &'static ProtocolVersion> {
54    TABLE.iter().filter(|v| v.enabled)
55}
56
57#[cfg(test)]
58mod tests {
59    use super::*;
60
61    #[test]
62    fn table_is_sorted_and_unique() {
63        for w in TABLE.windows(2) {
64            assert!(w[0].protocol < w[1].protocol);
65        }
66        for v in TABLE {
67            assert!(!v.game_versions.is_empty());
68            assert_eq!(by_protocol(v.protocol), Some(v));
69            for g in v.game_versions {
70                assert_eq!(by_game_version(g), Some(v));
71            }
72        }
73        assert_eq!(by_game_version("1.8.8").map(|v| v.protocol), Some(47));
74        assert_eq!(by_game_version("1.16.4").map(|v| v.protocol), Some(754));
75        assert_eq!(
76            by_protocol(775).map(|v| v.game_versions),
77            Some(&["26.1", "26.1.1", "26.1.2"][..])
78        );
79        assert!(by_protocol(775).is_some_and(|v| v.configuration));
80        assert!(by_protocol(1).is_none());
81    }
82}