aldrin_core/
protocol_version.rs

1use crate::error::{ProtocolVersionError, ProtocolVersionErrorKind};
2use std::fmt;
3use std::str::FromStr;
4
5#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
6pub struct ProtocolVersion {
7    minor: Minor,
8}
9
10impl ProtocolVersion {
11    pub const MAJOR: u32 = 1;
12    pub const V1_14: Self = Self { minor: Minor::V14 };
13    pub const V1_15: Self = Self { minor: Minor::V15 };
14    pub const MIN: Self = Self::V1_14;
15    pub const MAX: Self = Self::V1_15;
16
17    pub const fn new(major: u32, minor: u32) -> Result<Self, ProtocolVersionError> {
18        if major != Self::MAJOR {
19            return Err(ProtocolVersionError {
20                kind: ProtocolVersionErrorKind::InvalidMajor,
21            });
22        }
23
24        match minor {
25            14 => Ok(Self { minor: Minor::V14 }),
26            15 => Ok(Self { minor: Minor::V15 }),
27
28            _ => Err(ProtocolVersionError {
29                kind: ProtocolVersionErrorKind::InvalidMinor,
30            }),
31        }
32    }
33
34    pub const fn major(&self) -> u32 {
35        Self::MAJOR
36    }
37
38    pub const fn minor(&self) -> u32 {
39        self.minor as u32
40    }
41}
42
43#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
44enum Minor {
45    V14 = 14,
46    V15 = 15,
47}
48
49impl fmt::Display for ProtocolVersion {
50    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
51        write!(f, "{}.{}", self.major(), self.minor())
52    }
53}
54
55impl FromStr for ProtocolVersion {
56    type Err = ProtocolVersionError;
57
58    fn from_str(s: &str) -> Result<Self, Self::Err> {
59        let (major, minor) = s.split_once('.').ok_or(ProtocolVersionErrorKind::Parse)?;
60        let major = major.parse().map_err(|_| ProtocolVersionErrorKind::Parse)?;
61        let minor = minor.parse().map_err(|_| ProtocolVersionErrorKind::Parse)?;
62        Self::new(major, minor)
63    }
64}
65
66#[cfg(test)]
67mod test {
68    use super::ProtocolVersion;
69    use crate::error::ProtocolVersionErrorKind;
70
71    #[test]
72    fn parse_protocol_version() {
73        assert_eq!("1.14".parse(), Ok(ProtocolVersion::V1_14));
74        assert_eq!("1.15".parse(), Ok(ProtocolVersion::V1_15));
75
76        assert_eq!(
77            "0.14".parse::<ProtocolVersion>(),
78            Err(ProtocolVersionErrorKind::InvalidMajor.into())
79        );
80        assert_eq!(
81            "1.0".parse::<ProtocolVersion>(),
82            Err(ProtocolVersionErrorKind::InvalidMinor.into())
83        );
84        assert_eq!(
85            "1.4294967295".parse::<ProtocolVersion>(),
86            Err(ProtocolVersionErrorKind::InvalidMinor.into())
87        );
88
89        assert_eq!(
90            "1.".parse::<ProtocolVersion>(),
91            Err(ProtocolVersionErrorKind::Parse.into())
92        );
93        assert_eq!(
94            ".14".parse::<ProtocolVersion>(),
95            Err(ProtocolVersionErrorKind::Parse.into())
96        );
97        assert_eq!(
98            "".parse::<ProtocolVersion>(),
99            Err(ProtocolVersionErrorKind::Parse.into())
100        );
101    }
102}