diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2024-04-04 12:10:33 +0200 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2024-04-04 12:29:40 +0200 |
commit | 6195b9f344168fe094a8da8d0f8261a689942461 (patch) | |
tree | 15fea796c4354be7c9e997efb6ca7bddbc4714c1 /src | |
parent | de260daf73add9c28ceedaef1345a250c241b1de (diff) |
Add dedicated type for PacketIdenitifier{,NonZero}
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
Diffstat (limited to 'src')
-rw-r--r-- | src/lib.rs | 1 | ||||
-rw-r--r-- | src/packet_identifier.rs | 41 |
2 files changed, 42 insertions, 0 deletions
@@ -12,6 +12,7 @@ pub mod client_identifier; mod codecs; mod error; pub mod keep_alive; +pub mod packet_identifier; pub mod packets; pub mod payload; mod properties; diff --git a/src/packet_identifier.rs b/src/packet_identifier.rs new file mode 100644 index 0000000..24e5481 --- /dev/null +++ b/src/packet_identifier.rs @@ -0,0 +1,41 @@ +// +// 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)] +pub struct PacketIdentifier(u16); + +impl From<mqtt_format::v5::variable_header::PacketIdentifier> for PacketIdentifier { + fn from(value: mqtt_format::v5::variable_header::PacketIdentifier) -> Self { + Self(value.0) + } +} + +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) + } +} + +#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)] +pub struct PacketIdentifierNonZero(std::num::NonZeroU16); + +impl TryFrom<mqtt_format::v5::variable_header::PacketIdentifier> for PacketIdentifierNonZero { + 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<PacketIdentifierNonZero> for mqtt_format::v5::variable_header::PacketIdentifier { + fn from(value: PacketIdentifierNonZero) -> mqtt_format::v5::variable_header::PacketIdentifier { + mqtt_format::v5::variable_header::PacketIdentifier(value.0.get()) + } +} |