rumqttc/mqttbytes/v4/
unsuback.rs

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