Skip to content

Commit 5bf5d85

Browse files
MTPPayload implementation + Clone implementation
Implemented MTPPayload for MessageTransferProtocolPayload Clone refactors for additional fields
1 parent 1fa2000 commit 5bf5d85

File tree

1 file changed

+131
-15
lines changed

1 file changed

+131
-15
lines changed

net/src/protocol/mod.rs

Lines changed: 131 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,52 @@
3434
/// message processing scenarios.
3535
pub mod interface;
3636

37+
pub mod data;
3738

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.
3868
pub mod error;
3969

4070
use std::time::SystemTime;
4171

4272
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
5183
};
5284

5385
/// [`MTPResponse`] represents the response returned from operations performed in the message transfer protocol.
@@ -121,6 +153,82 @@ impl MessageTransferProtocolResponse for MTPResponse {
121153
}
122154
}
123155

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+
}
124232

125233

126234
/// `MTPManagerActions` represents a collection of management actions that can be performed.
@@ -310,7 +418,7 @@ pub struct MTPManagerActions {
310418
pub struct MTPHeaders {
311419
headers: Vec<MTPHeaderUnit>,
312420
local: MTPStorage,
313-
timestamp: SystemTime,
421+
timestamp: Option<SystemTime>,
314422
}
315423

316424

@@ -359,6 +467,7 @@ impl Clone for MTPHeaderUnit {
359467
Self::Source { source } => Self::Source { source: source.clone() },
360468
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() },
361469
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() }
362471
}
363472
}
364473
}
@@ -367,16 +476,16 @@ impl Clone for MTPHeaderUnit {
367476
impl Clone for MTPManagerAction{
368477
fn clone(&self) -> Self {
369478
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()),
372481
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()),
375484
}
376485
}
377486
}
378487

379-
/// CLone implementation for [MTPStatusCode]
488+
/// Clone implementation for [MTPStatusCode]
380489
impl Clone for MTPStatusCode{
381490
fn clone(&self) -> Self {
382491
match self {
@@ -385,4 +494,11 @@ impl Clone for MTPStatusCode{
385494
}
386495
}
387496
}
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

Comments
 (0)