mqute_codec/protocol/v5/puback.rs
1//! # Publish Acknowledgement Packet V5
2//!
3//! This module implements the MQTT v5 `PubAck` packet which is sent by the server
4//! to acknowledge receipt of a QoS 1 PUBLISH packet from the client, or by the client
5//! to acknowledge receipt of a QoS 1 PUBLISH packet from the server.
6//!
7
8use crate::protocol::v5::reason::ReasonCode;
9use crate::protocol::v5::util::{
10 ack, ack_decode_impl, ack_encode_impl, ack_properties, ack_properties_frame_impl,
11};
12use crate::protocol::{Flags, PacketType};
13
14/// Validates that a reason code is acceptable for a `PubAck` packet.
15///
16/// MQTT v5 defines specific reason codes that are valid for `PubAck` packets:
17/// - Success (0)
18/// - No matching subscribers (16)
19/// - Unspecified error (128)
20/// - Implementation specific error (131)
21/// - Not authorized (135)
22/// - Topic Name invalid (144)
23/// - Packet Identifier in use (145)
24/// - Quota exceeded (151)
25/// - Payload format invalid (153)
26fn validate_puback_reason_code(code: ReasonCode) -> bool {
27 matches!(
28 code.into(),
29 0 | 16 | 128 | 131 | 135 | 144 | 145 | 151 | 153
30 )
31}
32
33// Defines properties specific to PubAck packets
34ack_properties!(PubAckProperties);
35
36// Represents an MQTT v5 `PubAck` packet
37ack!(PubAck, PubAckProperties, validate_puback_reason_code);
38
39// Implement decoding for `PubAck` packets
40ack_decode_impl!(
41 PubAck,
42 PacketType::PubAck,
43 Flags::default(),
44 validate_puback_reason_code
45);
46
47// Implement encoding for `PubAck` packets
48ack_encode_impl!(PubAck, PacketType::PubAck, Flags::default());
49
50// Implement property frame handling for PubAckProperties
51ack_properties_frame_impl!(PubAckProperties);