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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/// The latest version that is currently supported by nix-compat.
static DEFAULT_PROTOCOL_VERSION: ProtocolVersion = ProtocolVersion::from_parts(1, 37);

/// Protocol versions are represented as a u16.
/// The upper 8 bits are the major version, the lower bits the minor.
/// This is not aware of any endianness, use [crate::wire::read_u64] to get an
/// u64 first, and the try_from() impl from here if you're receiving over the
/// Nix Worker protocol.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct ProtocolVersion(u16);

impl ProtocolVersion {
    pub const fn from_parts(major: u8, minor: u8) -> Self {
        Self(((major as u16) << 8) | minor as u16)
    }

    pub fn major(&self) -> u8 {
        ((self.0 & 0xff00) >> 8) as u8
    }

    pub fn minor(&self) -> u8 {
        (self.0 & 0x00ff) as u8
    }
}

impl Default for ProtocolVersion {
    fn default() -> Self {
        DEFAULT_PROTOCOL_VERSION
    }
}

impl PartialOrd for ProtocolVersion {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for ProtocolVersion {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        match self.major().cmp(&other.major()) {
            std::cmp::Ordering::Less => std::cmp::Ordering::Less,
            std::cmp::Ordering::Greater => std::cmp::Ordering::Greater,
            std::cmp::Ordering::Equal => {
                // same major, compare minor
                self.minor().cmp(&other.minor())
            }
        }
    }
}

impl From<u16> for ProtocolVersion {
    fn from(value: u16) -> Self {
        Self::from_parts(((value & 0xff00) >> 8) as u8, (value & 0x00ff) as u8)
    }
}

#[cfg(any(test, feature = "test"))]
impl From<(u8, u8)> for ProtocolVersion {
    fn from((major, minor): (u8, u8)) -> Self {
        Self::from_parts(major, minor)
    }
}

impl TryFrom<u64> for ProtocolVersion {
    type Error = &'static str;

    fn try_from(value: u64) -> Result<Self, Self::Error> {
        if value & !0xffff != 0 {
            return Err("only two least significant bits might be populated");
        }

        Ok((value as u16).into())
    }
}

impl From<ProtocolVersion> for u16 {
    fn from(value: ProtocolVersion) -> Self {
        value.0
    }
}

impl From<ProtocolVersion> for u64 {
    fn from(value: ProtocolVersion) -> Self {
        value.0 as u64
    }
}

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

#[cfg(test)]
mod tests {
    use super::ProtocolVersion;

    #[test]
    fn from_parts() {
        let version = ProtocolVersion::from_parts(1, 37);
        assert_eq!(version.major(), 1, "correct major");
        assert_eq!(version.minor(), 37, "correct minor");
        assert_eq!("1.37", &version.to_string(), "to_string");

        assert_eq!(0x0125, Into::<u16>::into(version));
        assert_eq!(0x0125, Into::<u64>::into(version));
    }

    #[test]
    fn from_u16() {
        let version = ProtocolVersion::from(0x0125_u16);
        assert_eq!("1.37", &version.to_string());
    }

    #[test]
    fn from_u64() {
        let version = ProtocolVersion::try_from(0x0125_u64).expect("must succeed");
        assert_eq!("1.37", &version.to_string());
    }

    /// This contains data in higher bits, which should fail.
    #[test]
    fn from_u64_fail() {
        ProtocolVersion::try_from(0xaa0125_u64).expect_err("must fail");
    }

    #[test]
    fn ord() {
        let v0_37 = ProtocolVersion::from_parts(0, 37);
        let v1_37 = ProtocolVersion::from_parts(1, 37);
        let v1_40 = ProtocolVersion::from_parts(1, 40);

        assert!(v0_37 < v1_37);
        assert!(v1_37 > v0_37);
        assert!(v1_37 < v1_40);
        assert!(v1_40 > v1_37);
        assert!(v1_40 <= v1_40);
    }
}