1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
use crate::error::{ProtocolVersionError, ProtocolVersionErrorKind};
use std::fmt;
use std::str::FromStr;

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct ProtocolVersion {
    minor: Minor,
}

impl ProtocolVersion {
    pub const MAJOR: u32 = 1;
    pub const V1_14: Self = Self { minor: Minor::V14 };
    pub const V1_15: Self = Self { minor: Minor::V15 };
    pub const V1_16: Self = Self { minor: Minor::V16 };
    pub const V1_17: Self = Self { minor: Minor::V17 };
    pub const V1_18: Self = Self { minor: Minor::V18 };
    pub const MIN: Self = Self::V1_14;
    pub const MAX: Self = Self::V1_18;

    pub const fn new(major: u32, minor: u32) -> Result<Self, ProtocolVersionError> {
        if major != Self::MAJOR {
            return Err(ProtocolVersionError {
                kind: ProtocolVersionErrorKind::InvalidMajor,
            });
        }

        match minor {
            14 => Ok(Self { minor: Minor::V14 }),
            15 => Ok(Self { minor: Minor::V15 }),
            16 => Ok(Self { minor: Minor::V16 }),
            17 => Ok(Self { minor: Minor::V17 }),
            18 => Ok(Self { minor: Minor::V18 }),

            _ => Err(ProtocolVersionError {
                kind: ProtocolVersionErrorKind::InvalidMinor,
            }),
        }
    }

    pub const fn major(&self) -> u32 {
        Self::MAJOR
    }

    pub const fn minor(&self) -> u32 {
        self.minor as u32
    }
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
enum Minor {
    V14 = 14,
    V15 = 15,
    V16 = 16,
    V17 = 17,
    V18 = 18,
}

impl fmt::Display for ProtocolVersion {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}.{}", self.major(), self.minor())
    }
}

impl FromStr for ProtocolVersion {
    type Err = ProtocolVersionError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let (major, minor) = s.split_once('.').ok_or(ProtocolVersionErrorKind::Parse)?;
        let major = major.parse().map_err(|_| ProtocolVersionErrorKind::Parse)?;
        let minor = minor.parse().map_err(|_| ProtocolVersionErrorKind::Parse)?;
        Self::new(major, minor)
    }
}

#[cfg(test)]
mod test {
    use super::ProtocolVersion;
    use crate::error::ProtocolVersionErrorKind;

    #[test]
    fn parse_protocol_version() {
        assert_eq!("1.14".parse(), Ok(ProtocolVersion::V1_14));
        assert_eq!("1.15".parse(), Ok(ProtocolVersion::V1_15));
        assert_eq!("1.16".parse(), Ok(ProtocolVersion::V1_16));
        assert_eq!("1.17".parse(), Ok(ProtocolVersion::V1_17));
        assert_eq!("1.18".parse(), Ok(ProtocolVersion::V1_18));

        assert_eq!(
            "1.13".parse::<ProtocolVersion>(),
            Err(ProtocolVersionErrorKind::InvalidMinor.into())
        );
        assert_eq!(
            "1.19".parse::<ProtocolVersion>(),
            Err(ProtocolVersionErrorKind::InvalidMinor.into())
        );

        assert_eq!(
            "0.14".parse::<ProtocolVersion>(),
            Err(ProtocolVersionErrorKind::InvalidMajor.into())
        );
        assert_eq!(
            "1.0".parse::<ProtocolVersion>(),
            Err(ProtocolVersionErrorKind::InvalidMinor.into())
        );
        assert_eq!(
            "1.4294967295".parse::<ProtocolVersion>(),
            Err(ProtocolVersionErrorKind::InvalidMinor.into())
        );

        assert_eq!(
            "1.".parse::<ProtocolVersion>(),
            Err(ProtocolVersionErrorKind::Parse.into())
        );
        assert_eq!(
            ".14".parse::<ProtocolVersion>(),
            Err(ProtocolVersionErrorKind::Parse.into())
        );
        assert_eq!(
            "".parse::<ProtocolVersion>(),
            Err(ProtocolVersionErrorKind::Parse.into())
        );
    }
}