34
34
/// message processing scenarios.
35
35
pub mod interface;
36
36
37
+ pub mod data;
37
38
39
+
40
+ /// The `error` module defines the error types used in the Message Transfer Protocol (MTP).
41
+ ///
42
+ /// This module includes:
43
+ ///
44
+ /// - `ProtocolError`: An enum that represents various client and server-side errors, each associated with
45
+ /// a specific numeric code and description. This enum is used to identify and handle errors in the protocol.
46
+ /// - `Error`: A struct that encapsulates an error message, which is used by `ProtocolError` to provide detailed
47
+ /// information about the error condition.
48
+ ///
49
+ /// ## Example
50
+ ///
51
+ /// ```rust
52
+ /// use crate::error::{ProtocolError, Error};
53
+ ///
54
+ /// // Creating an error for a bad request
55
+ /// let error = ProtocolError::BadRequest100(Error { message: "Invalid request".to_string() });
56
+ ///
57
+ /// // Retrieving the error code and description
58
+ /// assert_eq!(error.code(), 100);
59
+ /// assert_eq!(error.description(), "100 - Bad Request: The request could not be understood or was missing required parameters.");
60
+ /// ```
61
+ ///
62
+ /// The `ProtocolError` enum includes a range of error codes:
63
+ /// - **Client Errors (100-115)**: Errors that indicate issues on the client side.
64
+ /// - **Server Errors (120-128)**: Errors that indicate issues on the server side.
65
+ ///
66
+ /// Each variant of `ProtocolError` has a specific code and message associated with it, which helps in
67
+ /// diagnosing and responding to different error conditions in the protocol.
38
68
pub mod error;
39
69
40
70
use std:: time:: SystemTime ;
41
71
42
72
use interface:: {
43
- MessagePublish ,
44
- MessageCategory ,
45
- MessagePriority ,
46
- ContentType ,
47
- MTPManagerAction ,
48
- MTPHeaderUnit ,
49
- MTPStatusCode ,
50
- MessageTransferProtocolResponse
73
+ ContentType ,
74
+ MTPHeaderUnit ,
75
+ MTPManagerAction ,
76
+ MTPRequestType ,
77
+ MTPStatusCode ,
78
+ MessageCategory ,
79
+ MessagePriority ,
80
+ MessagePublish ,
81
+ MessageTransferProtocolPayload ,
82
+ MessageTransferProtocolResponse
51
83
} ;
52
84
53
85
/// [`MTPResponse`] represents the response returned from operations performed in the message transfer protocol.
@@ -121,6 +153,82 @@ impl MessageTransferProtocolResponse for MTPResponse {
121
153
}
122
154
}
123
155
156
+ /// [`MTPPayload`] type represents the payload sent from the client to the server
157
+ /// Contains all the information pertaining to request action and source information
158
+ /// passed in the header
159
+ struct MTPPayload {
160
+
161
+ /// Headers from the client
162
+ ///
163
+ /// # See Also
164
+ ///
165
+ /// - [`MTPHeaders`] for headers containing request info and [MTPStorage]
166
+ headers : MTPHeaders ,
167
+
168
+ /// Body of the payload (optional) only if the request type is [MTPRequestType::Publish] to publish the message
169
+ ///
170
+ /// # See also
171
+ /// - [`MTPMessage`] for the messsage type
172
+ message : Option < MTPMessage > ,
173
+
174
+ /// [MTPRequestType] of the request.
175
+ ///
176
+ /// # See also
177
+ /// - [`MTPRequestType`] for the type
178
+ request : MTPRequestType ,
179
+ }
180
+
181
+ impl MTPPayload {
182
+ fn construct ( headers : MTPHeaders , message : Option < MTPMessage > , request : MTPRequestType ) ->Self {
183
+ Self {
184
+ headers,
185
+ message,
186
+ request
187
+ }
188
+ }
189
+
190
+ fn subscribe ( headers : MTPHeaders , message : Option < MTPMessage > ) ->Self {
191
+ Self :: construct ( headers, message, MTPRequestType :: Subscribe )
192
+ }
193
+
194
+ fn unsubscribe ( headers : MTPHeaders , message : Option < MTPMessage > ) ->Self {
195
+ Self :: construct ( headers, message, MTPRequestType :: Unsubscribe )
196
+ }
197
+
198
+ fn publish ( headers : MTPHeaders , message : Option < MTPMessage > ) ->Self {
199
+ Self :: construct ( headers, message, MTPRequestType :: Publish )
200
+ }
201
+
202
+ fn pull ( headers : MTPHeaders , message : Option < MTPMessage > ) ->Self {
203
+ Self :: construct ( headers, message, MTPRequestType :: Pull )
204
+ }
205
+
206
+ fn ping ( headers : MTPHeaders , message : Option < MTPMessage > ) ->Self {
207
+ Self :: construct ( headers, message, MTPRequestType :: Ping )
208
+ }
209
+
210
+ fn manage ( headers : MTPHeaders , message : Option < MTPMessage > ) ->Self {
211
+ Self :: construct ( headers, message, MTPRequestType :: Manage )
212
+ }
213
+ }
214
+
215
+ impl MessageTransferProtocolPayload for MTPPayload {
216
+ fn get_headers ( & self ) -> Option < MTPHeaders > {
217
+ Some ( self . headers . clone ( ) )
218
+ }
219
+
220
+ fn get_message ( & self ) -> Option < MTPMessage > {
221
+ self . message . clone ( )
222
+ }
223
+
224
+ fn get_request ( & self ) -> MTPRequestType {
225
+ self . request . clone ( )
226
+ }
227
+
228
+ fn get_timestamp ( & self ) -> Option < SystemTime > {
229
+ self . headers . timestamp . clone ( )
230
+ }
231
+ }
124
232
125
233
126
234
/// `MTPManagerActions` represents a collection of management actions that can be performed.
@@ -310,7 +418,7 @@ pub struct MTPManagerActions {
310
418
pub struct MTPHeaders {
311
419
headers : Vec < MTPHeaderUnit > ,
312
420
local : MTPStorage ,
313
- timestamp : SystemTime ,
421
+ timestamp : Option < SystemTime > ,
314
422
}
315
423
316
424
@@ -359,6 +467,7 @@ impl Clone for MTPHeaderUnit {
359
467
Self :: Source { source } => Self :: Source { source : source. clone ( ) } ,
360
468
Self :: Message { id, timestamp, priority, category, content_type } => Self :: Message { id : id. clone ( ) , timestamp : timestamp. clone ( ) , priority : priority. clone ( ) , category : category. clone ( ) , content_type : content_type. clone ( ) } ,
361
469
Self :: MessagePublish { queue, to } => Self :: MessagePublish { queue : queue. clone ( ) , to : to. clone ( ) } ,
470
+ Self :: QueueCreation { name, access } => Self :: QueueCreation { name : name. clone ( ) , access : access. clone ( ) }
362
471
}
363
472
}
364
473
}
@@ -367,16 +476,16 @@ impl Clone for MTPHeaderUnit {
367
476
impl Clone for MTPManagerAction {
368
477
fn clone ( & self ) -> Self {
369
478
match self {
370
- Self :: Rename => Self :: Rename ,
371
- Self :: Authorize => Self :: Authorize ,
479
+ Self :: Rename ( s ) => Self :: Rename ( s . clone ( ) ) ,
480
+ Self :: Authorize ( s ) => Self :: Authorize ( s . clone ( ) ) ,
372
481
Self :: Reject => Self :: Reject ,
373
- Self :: Dispose => Self :: Dispose ,
374
- Self :: AccessorModify => Self :: AccessorModify ,
482
+ Self :: Dispose ( s ) => Self :: Dispose ( s . clone ( ) ) ,
483
+ Self :: AccessorModify ( s ) => Self :: AccessorModify ( s . clone ( ) ) ,
375
484
}
376
485
}
377
486
}
378
487
379
- /// CLone implementation for [MTPStatusCode]
488
+ /// Clone implementation for [MTPStatusCode]
380
489
impl Clone for MTPStatusCode {
381
490
fn clone ( & self ) -> Self {
382
491
match self {
@@ -385,4 +494,11 @@ impl Clone for MTPStatusCode{
385
494
}
386
495
}
387
496
}
388
-
497
+
498
+
499
+ /// Clone implementation for [MTPMessage]
500
+ impl Clone for MTPMessage {
501
+ fn clone ( & self ) -> Self {
502
+ Self { content_type : self . content_type . clone ( ) , priority : self . priority . clone ( ) , category : self . category . clone ( ) , publish : self . publish . clone ( ) , message : self . message . clone ( ) }
503
+ }
504
+ }
0 commit comments