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
// 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.
/// Trait defining MQTT connection role types
///
/// This trait provides a type-level mechanism to distinguish between different
/// MQTT connection roles (Client, Server, Any) at compile time. It enables
/// role-specific behavior and validation throughout the MQTT protocol implementation
/// without runtime overhead.
///
/// Each role type implements this trait with specific constant boolean flags
/// that indicate which role it represents. This allows for compile-time role
/// detection and role-specific code paths.
///
/// # Role Types
///
/// - **Client**: Represents an MQTT client that connects to brokers
/// - **Server**: Represents an MQTT broker/server that accepts client connections
/// - **Any**: Represents a generic role that can behave as either client or server
///
/// # Design Pattern
///
/// This trait uses the "phantom type" pattern where the type itself carries
/// semantic meaning without runtime data. The constant boolean flags allow
/// for efficient compile-time branching and role validation.
///
/// # Examples
///
/// ```ignore
/// use mqtt_protocol_core::mqtt;
///
/// // Check role type at compile time
/// fn process_connection<R: mqtt::connection::RoleType>() {
/// if R::IS_CLIENT {
/// // Client-specific processing
/// } else if R::IS_SERVER {
/// // Server-specific processing
/// }
/// }
///
/// // Use with specific role types
/// process_connection::<mqtt::connection::Client>();
/// process_connection::<mqtt::connection::Server>();
/// ```
/// MQTT Client role type
///
/// Represents an MQTT client that connects to MQTT brokers. Clients can:
/// - Establish connections to brokers using CONNECT packets
/// - Publish messages to topics
/// - Subscribe to topic filters to receive messages
/// - Send heartbeat PINGREQ packets
/// - Gracefully disconnect using DISCONNECT packets
///
/// This is a zero-sized type used purely for compile-time role identification.
/// The actual client behavior is implemented in the connection logic that
/// uses this role type as a generic parameter.
///
/// # Protocol Restrictions
///
/// When using the Client role, certain MQTT packets are restricted:
/// - Cannot send CONNACK (connection acknowledgment)
/// - Cannot send SUBACK (subscription acknowledgment)
/// - Cannot send UNSUBACK (unsubscription acknowledgment)
/// - Cannot send PINGRESP (ping response)
///
/// # Examples
///
/// ```ignore
/// use mqtt_protocol_core::mqtt;
///
/// // Client role is typically used as a generic parameter
/// type ClientConnection = mqtt::connection::GenericConnection<mqtt::connection::Client, u16>;
///
/// // Role constants can be checked at compile time
/// assert_eq!(mqtt::connection::Client::IS_CLIENT, true);
/// assert_eq!(mqtt::connection::Client::IS_SERVER, false);
/// ```
;
/// MQTT Server/Broker role type
///
/// Represents an MQTT broker/server that accepts client connections. Servers can:
/// - Accept CONNECT packets from clients and respond with CONNACK
/// - Receive published messages and route them to subscribers
/// - Handle SUBSCRIBE packets and respond with SUBACK
/// - Handle UNSUBSCRIBE packets and respond with UNSUBACK
/// - Respond to PINGREQ packets with PINGRESP
/// - Manage client sessions and retained messages
///
/// This is a zero-sized type used purely for compile-time role identification.
/// The actual server behavior is implemented in the connection logic that
/// uses this role type as a generic parameter.
///
/// # Protocol Restrictions
///
/// When using the Server role, certain MQTT packets are restricted:
/// - Cannot send CONNECT (connection request)
/// - Cannot send SUBSCRIBE (subscription request)
/// - Cannot send UNSUBSCRIBE (unsubscription request)
/// - Cannot send PINGREQ (ping request)
///
/// # Examples
///
/// ```ignore
/// use mqtt_protocol_core::mqtt;
///
/// // Server role is typically used as a generic parameter
/// type ServerConnection = mqtt::connection::GenericConnection<mqtt::connection::Server, u16>;
///
/// // Role constants can be checked at compile time
/// assert_eq!(mqtt::connection::Server::IS_CLIENT, false);
/// assert_eq!(mqtt::connection::Server::IS_SERVER, true);
/// ```
;
/// Generic MQTT role type
///
/// Represents a flexible MQTT role that can behave as either a client or server.
/// This role type is useful for:
/// - Testing scenarios where both client and server behavior is needed
/// - Bridge implementations that act as both client and server
/// - Development tools that need to simulate both roles
/// - Generic code that works with any MQTT role
///
/// This is a zero-sized type used purely for compile-time role identification.
/// When using the Any role, the implementation typically allows all MQTT
/// packet types without role-based restrictions.
///
/// # Protocol Behavior
///
/// The Any role typically allows all MQTT packet types and behaviors,
/// making it the most permissive role type. This flexibility comes at
/// the cost of losing compile-time role-specific validations.
///
/// # Examples
///
/// ```ignore
/// use mqtt_protocol_core::mqtt;
///
/// // Any role can be used for flexible implementations
/// type FlexibleConnection = mqtt::connection::GenericConnection<mqtt::connection::Any, u16>;
///
/// // Role constants can be checked at compile time
/// assert_eq!(mqtt::connection::Any::IS_CLIENT, false);
/// assert_eq!(mqtt::connection::Any::IS_SERVER, false);
/// assert_eq!(mqtt::connection::Any::IS_ANY, true);
/// ```
;
/// Implementation of `RoleType` for `Client`
///
/// Configures the Client role type with the appropriate role flags.
/// Only the `IS_CLIENT` flag is set to `true`, clearly identifying
/// this type as representing an MQTT client role.
/// Implementation of `RoleType` for `Server`
///
/// Configures the Server role type with the appropriate role flags.
/// Only the `IS_SERVER` flag is set to `true`, clearly identifying
/// this type as representing an MQTT server/broker role.
/// Implementation of `RoleType` for `Any`
///
/// Configures the Any role type with the appropriate role flags.
/// Only the `IS_ANY` flag is set to `true`, indicating this type
/// can represent any MQTT role (client, server, or both).