mqtt_tiny/packets/
connack.rs1use crate::coding::encoder::{PacketLenIter, U8Iter, Unit};
4use crate::coding::{Decoder, Encoder};
5use crate::err;
6use crate::error::{Data, DecoderError};
7use crate::packets::TryFromIterator;
8use core::iter::Chain;
9
10#[derive(Debug, Clone, PartialEq, Eq)]
12pub struct Connack {
13 session_present: bool,
15 return_code: u8,
17}
18impl Connack {
19 pub const TYPE: u8 = 2;
21
22 const BODY_LEN: usize = 2;
24
25 pub const fn new(session_present: bool, return_code: u8) -> Self {
27 Self { session_present, return_code }
28 }
29
30 pub const fn session_present(&self) -> bool {
32 self.session_present
33 }
34 pub const fn return_code(&self) -> u8 {
36 self.return_code
37 }
38}
39impl TryFromIterator for Connack {
40 fn try_from_iter<T>(iter: T) -> Result<Self, DecoderError>
41 where
42 T: IntoIterator<Item = u8>,
43 {
44 let mut decoder = Decoder::new(iter);
50 let (Self::TYPE, _flags) = decoder.header()? else {
51 return Err(err!(Data::SpecViolation, "invalid packet type"))?;
52 };
53 let Self::BODY_LEN = decoder.packetlen()? else {
54 return Err(err!(Data::SpecViolation, "invalid packet length"))?;
55 };
56
57 let [_, _, _, _, _, _, _, session_present] = decoder.bitmap()?;
59 let return_code = decoder.u8()?;
60
61 Ok(Self { session_present, return_code })
63 }
64}
65impl IntoIterator for Connack {
66 type Item = u8;
67 #[rustfmt::skip]
68 type IntoIter =
69 Chain<Chain<Chain<Chain<
71 Unit, U8Iter>,
73 PacketLenIter>,
75 U8Iter>,
77 U8Iter>;
79
80 fn into_iter(self) -> Self::IntoIter {
81 Encoder::default()
87 .header(Self::TYPE, [false, false, false, false])
88 .packetlen(Self::BODY_LEN)
89 .bitmap([false, false, false, false, false, false, false, self.session_present])
90 .u8(self.return_code)
91 .into_iter()
92 }
93}