mqute_codec/protocol/v5/pubrec.rs
1//! # Publish Received (PubRec) Packet - MQTT v5
2//!
3//! This module implements the MQTT v5 `PubRec` packet, which is the second packet in the
4//! Quality of Service 2 (QoS 2) message delivery flow. The `PubRec` packet is sent by the
5//! receiver to acknowledge receipt of a QoS 2 PUBLISH packet.
6
7use crate::protocol::v5::reason::ReasonCode;
8use crate::protocol::v5::util::{
9 ack, ack_decode_impl, ack_encode_impl, ack_properties, ack_properties_frame_impl,
10};
11use crate::protocol::{Flags, PacketType};
12
13/// Validates reason codes for `PubRec` packets
14///
15/// MQTT v5 specifies the following valid reason codes for `PubRec`:
16/// - 0x00 (Success) - Packet accepted and stored
17/// - 0x10 (No matching subscribers) - No subscribers for the topic
18/// - 0x80 (Unspecified error) - Unspecified error condition
19/// - 0x83 (Implementation specific error) - Implementation-specific error
20/// - 0x87 (Not authorized) - Client not authorized
21/// - 0x90 (Topic Name invalid) - Malformed topic name
22/// - 0x91 (Packet Identifier in use) - Duplicate packet ID
23/// - 0x97 (Quota exceeded) - Message quota exceeded
24/// - 0x99 (Payload format invalid) - Invalid payload format
25fn validate_pubrec_reason_code(code: ReasonCode) -> bool {
26 matches!(
27 code.into(),
28 0 | 16 | 128 | 131 | 135 | 144 | 145 | 151 | 153
29 )
30}
31
32// Defines properties specific to `PubRec` packets
33ack_properties!(PubRecProperties);
34
35// Implements the PropertyFrame trait for PubRecProperties
36ack_properties_frame_impl!(PubRecProperties);
37
38// Represents an MQTT v5 `PubRec` packet
39ack!(PubRec, PubRecProperties, validate_pubrec_reason_code);
40
41// Implements packet decoding for `PubRec`
42ack_decode_impl!(
43 PubRec,
44 PacketType::PubRec,
45 Flags::default(),
46 validate_pubrec_reason_code
47);
48
49// Implements packet encoding for `PubRec`
50ack_encode_impl!(PubRec, PacketType::PubRec, Flags::default());