mqtt4bytes/packets/
unsuback.rs

1use crate::*;
2use bytes::{Buf, BufMut, Bytes, BytesMut};
3
4/// Acknowledgement to unsubscribe
5#[derive(Debug, Clone, PartialEq)]
6pub struct UnsubAck {
7    pub pkid: u16,
8}
9
10impl UnsubAck {
11    pub fn new(pkid: u16) -> UnsubAck {
12        UnsubAck { pkid }
13    }
14
15    pub(crate) fn assemble(fixed_header: FixedHeader, mut bytes: Bytes) -> Result<Self, Error> {
16        if fixed_header.remaining_len != 2 {
17            return Err(Error::PayloadSizeIncorrect);
18        }
19
20        let variable_header_index = fixed_header.fixed_len;
21        bytes.advance(variable_header_index);
22        let pkid = bytes.get_u16();
23        let unsuback = UnsubAck { pkid };
24
25        Ok(unsuback)
26    }
27
28    pub fn write(&self, payload: &mut BytesMut) -> Result<usize, Error> {
29        payload.put_slice(&[0xB0, 0x02]);
30        payload.put_u16(self.pkid);
31        Ok(4)
32    }
33}