summaryrefslogtreecommitdiffstats
path: root/src/packet_identifier.rs
blob: ff7127be5601b948fe444af8b59421a29549e97b (plain)
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
//
//   This Source Code Form is subject to the terms of the Mozilla Public
//   License, v. 2.0. If a copy of the MPL was not distributed with this
//   file, You can obtain one at http://mozilla.org/MPL/2.0/.
//

#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct PacketIdentifier(std::num::NonZeroU16);

impl PacketIdentifier {
    #[inline]
    pub fn get(&self) -> u16 {
        self.0.get()
    }
}

impl std::fmt::Display for PacketIdentifier {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.0.fmt(f)
    }
}

impl TryFrom<mqtt_format::v5::variable_header::PacketIdentifier> for PacketIdentifier {
    type Error = (); // TODO

    fn try_from(
        value: mqtt_format::v5::variable_header::PacketIdentifier,
    ) -> Result<Self, Self::Error> {
        std::num::NonZeroU16::try_from(value.0)
            .map(Self)
            .map_err(drop) // TODO
    }
}

impl From<PacketIdentifier> for mqtt_format::v5::variable_header::PacketIdentifier {
    fn from(value: PacketIdentifier) -> mqtt_format::v5::variable_header::PacketIdentifier {
        mqtt_format::v5::variable_header::PacketIdentifier(value.0)
    }
}

impl From<std::num::NonZeroU16> for PacketIdentifier {
    fn from(value: std::num::NonZeroU16) -> Self {
        Self(value)
    }
}