poster/codec/
pubrel.rs
1use bytes::{Bytes, BytesMut};
2
3use crate::{
4 codec::ack::{AckRx, AckTx, AckTxBuilder, FixedHeader},
5 core::{
6 error::{ConversionError, InvalidValue},
7 utils::{ByteLen, Encode, PacketID, TryDecode},
8 },
9};
10
11#[allow(missing_docs)]
14#[derive(Copy, Clone, Debug, PartialEq, Eq)]
15pub enum PubrelReason {
16 Success = 0x00,
17 PacketIdentifierNotFound = 0x92,
18}
19
20impl TryFrom<u8> for PubrelReason {
21 type Error = ConversionError;
22
23 fn try_from(val: u8) -> Result<Self, Self::Error> {
24 match val {
25 0x00 => Ok(PubrelReason::Success),
26 0x92 => Ok(PubrelReason::PacketIdentifierNotFound),
27 _ => Err(InvalidValue.into()),
28 }
29 }
30}
31
32impl Default for PubrelReason {
33 fn default() -> Self {
34 Self::Success
35 }
36}
37
38impl ByteLen for PubrelReason {
39 fn byte_len(&self) -> usize {
40 (*self as u8).byte_len()
41 }
42}
43
44impl TryDecode for PubrelReason {
45 type Error = ConversionError;
46
47 fn try_decode(bytes: Bytes) -> Result<Self, Self::Error> {
48 Self::try_from(u8::try_decode(bytes)?)
49 }
50}
51
52impl Encode for PubrelReason {
53 fn encode(&self, buf: &mut BytesMut) {
54 (*self as u8).encode(buf)
55 }
56}
57
58pub(crate) type PubrelRx = AckRx<PubrelReason>;
59
60impl PacketID for PubrelRx {
61 const PACKET_ID: u8 = 6;
62}
63
64impl FixedHeader for PubrelRx {
65 const FIXED_HDR: u8 = (Self::PACKET_ID << 4) | 0b0010;
66}
67
68pub(crate) type PubrelTx<'a> = AckTx<'a, PubrelReason>;
69
70impl<'a> PacketID for PubrelTx<'a> {
71 const PACKET_ID: u8 = 6;
72}
73
74impl<'a> FixedHeader for PubrelTx<'a> {
75 const FIXED_HDR: u8 = (Self::PACKET_ID << 4) | 0b0010;
76}
77
78pub(crate) type PubrelTxBuilder<'a> = AckTxBuilder<'a, PubrelReason>;
79
80#[cfg(test)]
81mod test {
82 use super::*;
83 use crate::codec::ack::test::*;
84
85 #[test]
86 fn from_bytes_0() {
87 from_bytes_impl::<PubrelReason>();
88 }
89
90 #[test]
91 fn from_bytes_1() {
92 from_bytes_short_impl::<PubrelReason>();
93 }
94
95 #[test]
96 fn to_bytes_0() {
97 to_bytes_impl::<PubrelReason>();
98 }
99
100 #[test]
101 fn to_bytes_1() {
102 to_bytes_short_impl::<PubrelReason>();
103 }
104}