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
// MIT License
//
// Copyright (c) 2025 Takatoshi Kondo
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
use fmt;
use TryFromPrimitive;
use ;
/// Retain Handling Option for MQTT Subscriptions
///
/// This enum defines how retained messages should be handled when establishing
/// a new subscription. It corresponds to bits 4-5 in the Subscription Options
/// byte as specified in the MQTT v5.0 protocol specification.
///
/// Retained messages are messages that the broker stores and delivers to new
/// subscribers immediately upon subscription. The retain handling option
/// controls this behavior, allowing subscribers to specify whether they want
/// to receive existing retained messages.
///
/// # Protocol Specification
///
/// The retain handling option is encoded in bits 4-5 of the Subscription Options:
/// - Bits 4-5: `00` = Send retained messages at subscribe
/// - Bits 4-5: `01` = Send retained messages only for new subscriptions
/// - Bits 4-5: `10` = Do not send retained messages at subscribe
/// - Bits 4-5: `11` = Reserved (invalid)
///
/// # Use Cases
///
/// - **SendRetained**: Useful when a subscriber always wants the latest retained value
/// - **SendRetainedIfNotExists**: Prevents duplicate retained messages on subscription renewal
/// - **DoNotSendRetained**: For subscribers that only want new messages
///
/// # Examples
///
/// ```ignore
/// use mqtt_protocol_core::mqtt;
///
/// // Always receive retained messages when subscribing
/// let always_retained = mqtt::packet::RetainHandling::SendRetained;
///
/// // Only receive retained messages for new subscriptions
/// let new_only = mqtt::packet::RetainHandling::SendRetainedIfNotExists;
///
/// // Never receive retained messages
/// let no_retained = mqtt::packet::RetainHandling::DoNotSendRetained;
///
/// // Convert from byte value
/// let from_byte = mqtt::packet::RetainHandling::try_from(1u8).unwrap();
/// assert_eq!(from_byte, mqtt::packet::RetainHandling::SendRetainedIfNotExists);
/// ```
/// Implementation of `Display` for `RetainHandling`
///
/// Formats the retain handling option as a human-readable string representation.
/// This allows retain handling values to be used with `println!`, `format!`, and
/// other string formatting operations.
///
/// # Output Format
///
/// * `RetainHandling::SendRetained` -> "SendRetained"
/// * `RetainHandling::SendRetainedIfNotExists` -> "SendRetainedIfNotExists"
/// * `RetainHandling::DoNotSendRetained` -> "DoNotSendRetained"
/// Implementation of `Default` for `RetainHandling`
///
/// Returns `SendRetained` as the default retain handling behavior.
/// This matches the MQTT protocol default where retained messages
/// are sent to new subscribers unless explicitly configured otherwise.
///
/// # Returns
///
/// `RetainHandling::SendRetained` - The default retain handling behavior