mqute-codec 0.4.2

A full-featured implementation of MQTT protocol serialization in Rust, supporting versions 3.1, 3.1.1 and 5.0.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
//! # MQTT Protocol - Fixed Header and Flags
//!
//! This module provides structures and utilities for handling the fixed header and flags
//! in the MQTT protocol.
//!
//! The MQTT protocol uses a fixed header to describe the type of packet and its properties.
//! The `FixedHeader` struct represents this header, while the `Flags` struct encapsulates
//! the control flags (DUP, QoS, and RETAIN) associated with the packet.

use crate::codec;
use crate::protocol::util;
use crate::protocol::{PacketType, QoS};
use crate::Error;
use bytes::{Buf, BufMut, BytesMut};
use std::cmp::PartialEq;

/// Represents the control flags in an MQTT packet.
///
/// # Examples
///
/// ```rust
/// use mqute_codec::protocol::QoS;
/// use mqute_codec::protocol::Flags;
///
/// // Create default flags
/// let default_flags = Flags::default();
/// assert_eq!(default_flags.dup, false);
/// assert_eq!(default_flags.qos, QoS::AtMostOnce);
/// assert_eq!(default_flags.retain, false);
///
/// // Create custom flags
/// let custom_flags = Flags::new(QoS::AtLeastOnce);
/// assert_eq!(custom_flags.qos, QoS::AtLeastOnce);
/// ```
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct Flags {
    /// Indicates if the packet is a duplicate.
    pub dup: bool,

    /// The Quality of Service level (0, 1, or 2).
    pub qos: QoS,

    /// Indicates if the message should be retained by the broker.
    pub retain: bool,
}

impl Default for Flags {
    /// Creates default `Flags` with:
    /// - `dup`: `false`
    /// - `qos`: `QoS::AtMostOnce`
    /// - `retain`: `false`
    fn default() -> Self {
        Flags {
            dup: false,
            qos: QoS::AtMostOnce,
            retain: false,
        }
    }
}

impl Flags {
    /// Creates new `Flags` with the specified QoS level.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use mqute_codec::protocol::QoS;
    /// use mqute_codec::protocol::Flags;
    ///
    /// let flags = Flags::new(QoS::ExactlyOnce);
    /// assert_eq!(flags.qos, QoS::ExactlyOnce);
    /// ```
    pub fn new(qos: QoS) -> Self {
        Flags {
            dup: false,
            qos,
            retain: false,
        }
    }

    /// Checks if the flags are set to their default values.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use mqute_codec::protocol::Flags;
    ///
    /// let flags = Flags::default();
    /// assert!(flags.is_default());
    /// ```
    pub fn is_default(&self) -> bool {
        *self == Self::default()
    }
}

/// Represents the fixed header of an MQTT packet.
///
/// # Examples
///
/// ```
/// use mqute_codec::protocol::{FixedHeader, PacketType};
///
/// // Create a fixed header for a CONNECT packet
/// let header = FixedHeader::new(PacketType::Connect, 10);
/// assert_eq!(header.packet_type(), PacketType::Connect);
/// assert_eq!(header.remaining_len(), 10);
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct FixedHeader {
    /// The first byte of the packet, encoding the packet type and flags.
    control_byte: u8,

    /// The length of the remaining payload.
    remaining_len: usize,
}

impl FixedHeader {
    /// Creates a new `FixedHeader` with the specified packet type and remaining length.
    ///
    /// # Examples
    ///
    /// ```
    /// use mqute_codec::protocol::{FixedHeader, PacketType};
    ///
    /// let header = FixedHeader::new(PacketType::Publish, 20);
    /// assert_eq!(header.packet_type(), PacketType::Publish);
    /// ```
    pub fn new(packet: PacketType, remaining_len: usize) -> Self {
        let control_byte = build_control_byte(packet, Flags::default());

        FixedHeader {
            control_byte,
            remaining_len,
        }
    }

    /// Attempts to create a `FixedHeader` from a control byte and remaining length.
    ///
    /// # Errors
    /// Returns an error if the packet type is invalid.
    ///
    /// # Examples
    ///
    /// ```
    /// use mqute_codec::protocol::{FixedHeader, PacketType};
    /// use mqute_codec::Error;
    ///
    /// let header = FixedHeader::try_from(0x30, 10).unwrap();
    /// assert_eq!(header.packet_type(), PacketType::Publish);
    /// ```
    pub fn try_from(control_byte: u8, remaining_len: usize) -> Result<Self, Error> {
        let _: PacketType = fetch_packet_type(control_byte).try_into()?;

        // Bits 2-1 of the control byte are interpreted as a QoS value by
        // `flags()`. The value 0b11 (3) is reserved by the MQTT spec and must
        // never appear on the wire. Reject it here so that `flags()` never
        // has to deal with an invalid QoS later on.
        let qos_bits = (control_byte >> 1) & 0x03;
        if qos_bits == 0x03 {
            return Err(Error::InvalidQos(qos_bits));
        }

        Ok(FixedHeader {
            control_byte,
            remaining_len,
        })
    }

    /// Creates a `FixedHeader` with custom flags.
    ///
    /// # Examples
    ///
    /// ```
    /// use mqute_codec::protocol::{FixedHeader, PacketType, Flags};
    /// use mqute_codec::protocol::QoS;
    ///
    /// let flags = Flags::new(QoS::AtLeastOnce);
    /// let header = FixedHeader::with_flags(PacketType::Publish, flags, 15);
    /// assert_eq!(header.flags().qos, QoS::AtLeastOnce);
    /// ```
    pub fn with_flags(packet_type: PacketType, flags: Flags, remaining_len: usize) -> Self {
        let control_byte = build_control_byte(packet_type, flags);
        FixedHeader {
            control_byte,
            remaining_len,
        }
    }

    /// Returns the packet type encoded in the control byte.
    ///
    /// # Examples
    ///
    /// ```
    /// use mqute_codec::protocol::{FixedHeader, PacketType};
    ///
    /// let header = FixedHeader::new(PacketType::Subscribe, 5);
    /// assert_eq!(header.packet_type(), PacketType::Subscribe);
    /// ```
    pub fn packet_type(&self) -> PacketType {
        fetch_packet_type(self.control_byte).try_into().unwrap()
    }

    /// Extracts and returns the flags from the control byte.
    ///
    /// # Examples
    ///
    /// ```
    /// use mqute_codec::protocol::{FixedHeader, PacketType, Flags};
    /// use mqute_codec::protocol::QoS;
    ///
    /// let header = FixedHeader::new(PacketType::Publish, 10);
    /// let flags = header.flags();
    /// assert_eq!(flags.qos, QoS::AtMostOnce);
    /// ```
    pub fn flags(&self) -> Flags {
        let flags = self.control_byte & 0x0F;
        let dup: bool = (flags & 0x08) != 0;
        // Safe: `try_from`/`new`/`with_flags` guarantee the QoS bits are
        // never the reserved value 3, so this conversion cannot fail.
        let qos = ((flags >> 1) & 0x03)
            .try_into()
            .expect("QoS bits are validated when the FixedHeader is constructed");
        let retain = flags & 0x01 != 0;

        Flags { dup, qos, retain }
    }

    /// Returns the remaining length of the payload.
    ///
    /// # Examples
    ///
    /// ```
    /// use mqute_codec::protocol::{FixedHeader, PacketType};
    ///
    /// let header = FixedHeader::new(PacketType::Publish, 25);
    /// assert_eq!(header.remaining_len(), 25);
    /// ```
    pub fn remaining_len(&self) -> usize {
        self.remaining_len
    }

    /// Returns the length of the fixed header in bytes.
    ///
    /// # Examples
    ///
    /// ```
    /// use mqute_codec::protocol::{FixedHeader, PacketType};
    ///
    /// let header = FixedHeader::new(PacketType::Publish, 10);
    /// assert_eq!(header.fixed_len(), 2); // 1 byte for control byte, 1 byte for remaining length
    /// ```
    pub fn fixed_len(&self) -> usize {
        util::len_bytes(self.remaining_len) + 1
    }

    /// Returns the total length of the packet (fixed header + payload).
    ///
    /// # Examples
    ///
    /// ```
    /// use mqute_codec::protocol::{FixedHeader, PacketType};
    ///
    /// let header = FixedHeader::new(PacketType::Publish, 10);
    /// assert_eq!(header.packet_len(), 12); // 2 bytes for fixed header, 10 bytes for payload
    /// ```
    pub fn packet_len(&self) -> usize {
        self.remaining_len + self.fixed_len()
    }

    /// Encodes the fixed header into a buffer.
    ///
    /// # Errors
    /// Returns an error if encoding fails.
    ///
    /// # Examples
    ///
    /// ```
    /// use mqute_codec::protocol::{FixedHeader, PacketType};
    /// use bytes::BytesMut;
    ///
    /// let mut buf = BytesMut::new();
    /// let header = FixedHeader::new(PacketType::Publish, 10);
    /// header.encode(&mut buf).unwrap();
    /// ```
    pub fn encode(&self, buf: &mut BytesMut) -> Result<(), Error> {
        buf.put_u8(self.control_byte);
        codec::util::encode_variable_integer(buf, self.remaining_len as u32)
    }

    /// Decodes a fixed header from a buffer.
    ///
    /// # Errors
    /// Returns an error if decoding fails or the payload size exceeds the limit.
    ///
    /// # Examples
    ///
    /// ```
    /// use mqute_codec::protocol::{FixedHeader, PacketType};
    /// use bytes::BytesMut;
    ///
    /// let mut buf = BytesMut::from(&[0x30, 0x04,
    ///                                0x00, 0x00,
    ///                                0x00, 0x00][..]); // Publish packet with remaining length 4
    /// let header = FixedHeader::decode(&buf, None).unwrap();
    /// assert_eq!(header.packet_type(), PacketType::Publish);
    /// assert_eq!(header.remaining_len(), 4);
    /// ```
    pub fn decode(buf: &[u8], inbound_max_size: Option<usize>) -> Result<Self, Error> {
        let buf_len = buf.len();
        if buf_len < 2 {
            return Err(Error::NotEnoughBytes(2 - buf_len));
        }

        let mut buf = buf;
        let control_byte = buf.get_u8();
        let remaining_len = codec::util::decode_variable_integer(buf)? as usize;

        let header = FixedHeader::try_from(control_byte, remaining_len)?;

        if let Some(max_size) = inbound_max_size
            && header.remaining_len > max_size
        {
            return Err(Error::PayloadSizeLimitExceeded(header.remaining_len));
        }

        let packet_len = header.packet_len();
        if buf_len < packet_len {
            return Err(Error::NotEnoughBytes(packet_len - buf_len));
        }

        Ok(header)
    }
}

/// Extracts the packet type from the control byte.
#[inline]
fn fetch_packet_type(control_byte: u8) -> u8 {
    control_byte >> 4
}

/// Builds the control byte from the packet type and flags.
const fn build_control_byte(packet_type: PacketType, flags: Flags) -> u8 {
    let byte = (packet_type as u8) << 4;
    let flags = (flags.dup as u8) << 3 | (flags.qos as u8) << 1 | (flags.retain as u8);
    byte | flags
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn try_from_rejects_reserved_qos_for_publish() {
        // Publish packet type (0x30) with QoS bits set to the reserved value 3
        // (0b11 at bits 2-1): dup=0, qos=3, retain=0 -> 0b0000_0110.
        let control_byte = 0x30 | 0x06;
        let result = FixedHeader::try_from(control_byte, 0);
        assert!(matches!(result, Err(Error::InvalidQos(3))));
    }

    #[test]
    fn try_from_rejects_reserved_qos_regardless_of_packet_type() {
        // The reserved QoS bit pattern is invalid on the wire for any packet
        // type, not just Publish (e.g. PubAck: 0x40 | 0b0110).
        let control_byte = 0x40 | 0x06;
        let result = FixedHeader::try_from(control_byte, 0);
        assert!(matches!(result, Err(Error::InvalidQos(3))));
    }

    #[test]
    fn try_from_accepts_all_valid_qos_values() {
        for (qos_bits, expected) in [
            (0u8, QoS::AtMostOnce),
            (1u8, QoS::AtLeastOnce),
            (2u8, QoS::ExactlyOnce),
        ] {
            let control_byte = 0x30 | (qos_bits << 1);
            let header = FixedHeader::try_from(control_byte, 0).unwrap();
            // Must not panic and must report the expected QoS.
            assert_eq!(header.flags().qos, expected);
        }
    }

    #[test]
    fn decode_rejects_reserved_qos_without_panicking() {
        // A full wire-format buffer for a Publish packet with reserved QoS
        // bits must be rejected gracefully rather than panicking.
        let buf = [0x30 | 0x06, 0x00];
        let result = FixedHeader::decode(&buf, None);
        assert!(matches!(result, Err(Error::InvalidQos(3))));
    }

    #[test]
    fn new_and_with_flags_never_produce_invalid_qos() {
        // Constructors driven by the type-safe `Flags`/`QoS` API can never
        // produce the reserved bit pattern, so `flags()` must not panic.
        let header = FixedHeader::new(PacketType::Publish, 0);
        assert_eq!(header.flags(), Flags::default());

        for qos in [QoS::AtMostOnce, QoS::AtLeastOnce, QoS::ExactlyOnce] {
            let header = FixedHeader::with_flags(PacketType::Publish, Flags::new(qos), 0);
            assert_eq!(header.flags().qos, qos);
        }
    }
}