mqtt_protocol_core/mqtt/connection/
role.rs

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