neo_email/
message.rs

1use super::status_code::StatusCodes;
2
3/// # Message
4/// 
5/// This struct represents a message that the SMTP server can return to the client.
6///
7/// ```rust
8/// use neo_email::status_code::StatusCodes;
9/// use neo_email::message::Message;
10/// 
11/// Message::builder()
12///     .status(StatusCodes::AuthenticationSuccessful)
13///     .message("Authenticated".to_string())
14///     .build();
15/// ```
16#[derive(Debug, Clone, PartialEq, Eq)]
17pub struct Message {
18    /// # Status
19    /// 
20    /// The status code of the message.
21    /// 
22    /// ## Example
23    /// 
24    /// `StatusCodes::AuthenticationSuccessful`
25    pub status: StatusCodes,
26    /// # Message
27    /// 
28    /// The message to be sent.
29    pub message: String,
30}
31
32/// # Message Builder
33///
34/// This struct is a builder for the Message struct.
35/// 
36/// ```rust
37/// use neo_email::status_code::StatusCodes;
38/// use neo_email::message::Message;
39/// 
40/// Message::builder()
41///     .status(StatusCodes::AuthenticationSuccessful)
42///     .message("Authenticated".to_string())
43///     .build();
44/// ```
45#[derive(Debug, Clone, Default)]
46pub struct MessageBuilder {
47    status: Option<StatusCodes>,
48    message: Option<String>,
49}
50
51impl Message {
52    /// # New
53    /// 
54    /// This function creates a new message.
55    pub fn new(status: StatusCodes, message: String) -> Self {
56        Self { status, message }
57    }
58
59    /// # Builder
60    /// 
61    /// This function returns a MessageBuilder.
62    pub fn builder() -> MessageBuilder {
63        MessageBuilder::default()
64    }
65
66    /// # To String
67    ///
68    /// This function converts the message to a string.
69    pub fn to_string(&self, is_last: bool) -> String {
70        // If it is the last message, return the status code and message with a space
71        // If it is not the last message, return the status code and message with a dash
72        if is_last {
73            format!("{} {}\r\n", self.status.to_string(), self.message)
74        } else {
75            format!("{}-{}\r\n", self.status.to_string(), self.message)
76        }
77    }
78
79    /// # As Bytes
80    ///
81    /// This function converts the message to bytes.
82    pub fn as_bytes(&self, is_last: bool) -> Vec<u8> {
83        self.to_string(is_last).as_bytes().to_vec()
84    }
85}
86
87impl MessageBuilder {
88    /// # Set Status
89    /// 
90    /// This function sets the status of the message.
91    pub fn status(mut self, status: StatusCodes) -> Self {
92        self.status = Some(status);
93        self
94    }
95
96    /// # Set Message
97    /// 
98    /// This function sets the message of the message.
99    pub fn message(mut self, message: String) -> Self {
100        self.message = Some(message);
101        self
102    }
103
104    /// # Build
105    /// 
106    /// This function builds the message.
107    pub fn build(self) -> Message {
108        Message {
109            status: self.status.unwrap(),
110            message: self.message.unwrap(),
111        }
112    }
113}