mqute_codec/protocol/v3/
suback.rs1use crate::Error;
8use crate::protocol::common::suback;
9use crate::protocol::{QoS, traits};
10
11#[derive(Debug, Clone, Copy, PartialEq, Eq)]
22pub struct ReturnCode(QoS);
23
24impl ReturnCode {
25 pub fn new(qos: QoS) -> Self {
26 ReturnCode { 0: qos }
27 }
28}
29
30impl TryFrom<u8> for ReturnCode {
31 type Error = Error;
32
33 fn try_from(value: u8) -> Result<Self, Self::Error> {
35 let code = match value {
36 0x0 => ReturnCode(QoS::AtMostOnce),
37 0x1 => ReturnCode(QoS::AtLeastOnce),
38 0x2 => ReturnCode(QoS::ExactlyOnce),
39 _ => return Err(Error::InvalidReasonCode(value)),
40 };
41
42 Ok(code)
43 }
44}
45
46impl From<ReturnCode> for u8 {
47 fn from(value: ReturnCode) -> Self {
49 value.0 as Self
50 }
51}
52
53suback!(ReturnCode);
54
55impl traits::SubAck for SubAck {}
56
57#[cfg(test)]
58mod tests {
59 use super::*;
60 use crate::codec::{Decode, Encode, PacketCodec};
61 use crate::protocol::PacketType;
62 use bytes::BytesMut;
63 use tokio_util::codec::Decoder;
64
65 #[test]
66 fn suback_decode() {
67 let mut codec = PacketCodec::new(None, None);
68
69 let data = &[
70 (PacketType::SubAck as u8) << 4, 0x05, 0x12, 0x34,
74 0x00, 0x01, 0x02, ];
78
79 let mut stream = BytesMut::new();
80
81 stream.extend_from_slice(&data[..]);
82
83 let raw_packet = codec.decode(&mut stream).unwrap().unwrap();
84 let packet = SubAck::decode(raw_packet).unwrap();
85
86 assert_eq!(
87 packet,
88 SubAck::new(
89 0x1234,
90 vec![
91 ReturnCode(QoS::AtMostOnce),
92 ReturnCode(QoS::AtLeastOnce),
93 ReturnCode(QoS::ExactlyOnce)
94 ]
95 )
96 );
97 }
98
99 #[test]
100 fn suback_encode() {
101 let packet = SubAck::new(
102 0x1234,
103 vec![
104 ReturnCode(QoS::AtMostOnce),
105 ReturnCode(QoS::AtLeastOnce),
106 ReturnCode(QoS::ExactlyOnce),
107 ],
108 );
109
110 let mut stream = BytesMut::new();
111 packet.encode(&mut stream).unwrap();
112 assert_eq!(
113 stream,
114 vec![
115 (PacketType::SubAck as u8) << 4,
116 0x05,
117 0x12,
118 0x34,
119 0x00, 0x01, 0x02, ]
123 );
124 }
125}