mqtt_protocol_core/mqtt/connection/role.rs
1// MIT License
2//
3// Copyright (c) 2025 Takatoshi Kondo
4//
5// Permission is hereby granted, free of charge, to any person obtaining a copy
6// of this software and associated documentation files (the "Software"), to deal
7// in the Software without restriction, including without limitation the rights
8// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9// copies of the Software, and to permit persons to whom the Software is
10// furnished to do so, subject to the following conditions:
11//
12// The above copyright notice and this permission notice shall be included in all
13// copies or substantial portions of the Software.
14//
15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21// SOFTWARE.
22
23/// Trait defining MQTT connection role types
24///
25/// This trait provides a type-level mechanism to distinguish between different
26/// MQTT connection roles (Client, Server, Any) at compile time. It enables
27/// role-specific behavior and validation throughout the MQTT protocol implementation
28/// without runtime overhead.
29///
30/// Each role type implements this trait with specific constant boolean flags
31/// that indicate which role it represents. This allows for compile-time role
32/// detection and role-specific code paths.
33///
34/// # Role Types
35///
36/// - **Client**: Represents an MQTT client that connects to brokers
37/// - **Server**: Represents an MQTT broker/server that accepts client connections
38/// - **Any**: Represents a generic role that can behave as either client or server
39///
40/// # Design Pattern
41///
42/// This trait uses the "phantom type" pattern where the type itself carries
43/// semantic meaning without runtime data. The constant boolean flags allow
44/// for efficient compile-time branching and role validation.
45///
46/// # Examples
47///
48/// ```ignore
49/// use mqtt_protocol_core::mqtt;
50///
51/// // Check role type at compile time
52/// fn process_connection<R: mqtt::connection::RoleType>() {
53/// if R::IS_CLIENT {
54/// // Client-specific processing
55/// } else if R::IS_SERVER {
56/// // Server-specific processing
57/// }
58/// }
59///
60/// // Use with specific role types
61/// process_connection::<mqtt::connection::Client>();
62/// process_connection::<mqtt::connection::Server>();
63/// ```
64#[rustfmt::skip]
65pub trait RoleType: 'static {
66 /// Indicates if this role type represents an MQTT client
67 ///
68 /// Set to `true` for client roles, `false` for all other roles.
69 /// Clients initiate connections to MQTT brokers and can publish
70 /// messages, subscribe to topics, and receive messages.
71 const IS_CLIENT: bool = false;
72
73 /// Indicates if this role type represents an MQTT server/broker
74 ///
75 /// Set to `true` for server roles, `false` for all other roles.
76 /// Servers accept client connections, route messages between clients,
77 /// manage subscriptions, and handle retained messages.
78 const IS_SERVER: bool = false;
79
80 /// Indicates if this role type can represent any MQTT role
81 ///
82 /// Set to `true` for generic roles that can behave as either client
83 /// or server, `false` for specific role types. This is useful for
84 /// testing and flexible implementations.
85 const IS_ANY: bool = false;
86}
87
88/// MQTT Client role type
89///
90/// Represents an MQTT client that connects to MQTT brokers. Clients can:
91/// - Establish connections to brokers using CONNECT packets
92/// - Publish messages to topics
93/// - Subscribe to topic filters to receive messages
94/// - Send heartbeat PINGREQ packets
95/// - Gracefully disconnect using DISCONNECT packets
96///
97/// This is a zero-sized type used purely for compile-time role identification.
98/// The actual client behavior is implemented in the connection logic that
99/// uses this role type as a generic parameter.
100///
101/// # Protocol Restrictions
102///
103/// When using the Client role, certain MQTT packets are restricted:
104/// - Cannot send CONNACK (connection acknowledgment)
105/// - Cannot send SUBACK (subscription acknowledgment)
106/// - Cannot send UNSUBACK (unsubscription acknowledgment)
107/// - Cannot send PINGRESP (ping response)
108///
109/// # Examples
110///
111/// ```ignore
112/// use mqtt_protocol_core::mqtt;
113///
114/// // Client role is typically used as a generic parameter
115/// type ClientConnection = mqtt::connection::GenericConnection<mqtt::connection::Client, u16>;
116///
117/// // Role constants can be checked at compile time
118/// assert_eq!(mqtt::connection::Client::IS_CLIENT, true);
119/// assert_eq!(mqtt::connection::Client::IS_SERVER, false);
120/// ```
121pub struct Client;
122
123/// MQTT Server/Broker role type
124///
125/// Represents an MQTT broker/server that accepts client connections. Servers can:
126/// - Accept CONNECT packets from clients and respond with CONNACK
127/// - Receive published messages and route them to subscribers
128/// - Handle SUBSCRIBE packets and respond with SUBACK
129/// - Handle UNSUBSCRIBE packets and respond with UNSUBACK
130/// - Respond to PINGREQ packets with PINGRESP
131/// - Manage client sessions and retained messages
132///
133/// This is a zero-sized type used purely for compile-time role identification.
134/// The actual server behavior is implemented in the connection logic that
135/// uses this role type as a generic parameter.
136///
137/// # Protocol Restrictions
138///
139/// When using the Server role, certain MQTT packets are restricted:
140/// - Cannot send CONNECT (connection request)
141/// - Cannot send SUBSCRIBE (subscription request)
142/// - Cannot send UNSUBSCRIBE (unsubscription request)
143/// - Cannot send PINGREQ (ping request)
144///
145/// # Examples
146///
147/// ```ignore
148/// use mqtt_protocol_core::mqtt;
149///
150/// // Server role is typically used as a generic parameter
151/// type ServerConnection = mqtt::connection::GenericConnection<mqtt::connection::Server, u16>;
152///
153/// // Role constants can be checked at compile time
154/// assert_eq!(mqtt::connection::Server::IS_CLIENT, false);
155/// assert_eq!(mqtt::connection::Server::IS_SERVER, true);
156/// ```
157pub struct Server;
158
159/// Generic MQTT role type
160///
161/// Represents a flexible MQTT role that can behave as either a client or server.
162/// This role type is useful for:
163/// - Testing scenarios where both client and server behavior is needed
164/// - Bridge implementations that act as both client and server
165/// - Development tools that need to simulate both roles
166/// - Generic code that works with any MQTT role
167///
168/// This is a zero-sized type used purely for compile-time role identification.
169/// When using the Any role, the implementation typically allows all MQTT
170/// packet types without role-based restrictions.
171///
172/// # Protocol Behavior
173///
174/// The Any role typically allows all MQTT packet types and behaviors,
175/// making it the most permissive role type. This flexibility comes at
176/// the cost of losing compile-time role-specific validations.
177///
178/// # Examples
179///
180/// ```ignore
181/// use mqtt_protocol_core::mqtt;
182///
183/// // Any role can be used for flexible implementations
184/// type FlexibleConnection = mqtt::connection::GenericConnection<mqtt::connection::Any, u16>;
185///
186/// // Role constants can be checked at compile time
187/// assert_eq!(mqtt::connection::Any::IS_CLIENT, false);
188/// assert_eq!(mqtt::connection::Any::IS_SERVER, false);
189/// assert_eq!(mqtt::connection::Any::IS_ANY, true);
190/// ```
191pub struct Any;
192
193/// Implementation of `RoleType` for `Client`
194///
195/// Configures the Client role type with the appropriate role flags.
196/// Only the `IS_CLIENT` flag is set to `true`, clearly identifying
197/// this type as representing an MQTT client role.
198impl RoleType for Client {
199 const IS_CLIENT: bool = true;
200}
201
202/// Implementation of `RoleType` for `Server`
203///
204/// Configures the Server role type with the appropriate role flags.
205/// Only the `IS_SERVER` flag is set to `true`, clearly identifying
206/// this type as representing an MQTT server/broker role.
207impl RoleType for Server {
208 const IS_SERVER: bool = true;
209}
210
211/// Implementation of `RoleType` for `Any`
212///
213/// Configures the Any role type with the appropriate role flags.
214/// Only the `IS_ANY` flag is set to `true`, indicating this type
215/// can represent any MQTT role (client, server, or both).
216impl RoleType for Any {
217 const IS_ANY: bool = true;
218}