1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
use crate::configuration::EmailConfiguration;
use async_trait::async_trait;
use std::sync::mpsc;
use std::sync::mpsc::SyncSender;
use crate::email::{EmailAddress, EmailObject};
use crate::errors::EmailError;
use crate::traits::EmailTrait;
#[derive(Debug, Clone, serde::Deserialize, serde::Serialize, Default, PartialOrd, PartialEq)]
pub struct MemoryConfig {
pub sender: EmailAddress,
}
impl MemoryConfig {
/// Generates a new `MemoryConfig`.
///
/// # Parameters
/// - `sender`: A `String` that will be used as the sender of the `MemoryConfig`.
///
/// # Returns
/// A new instance of `MemoryConfig` with the `sender` property set to the provided `String`.
///
/// # Examples
/// ```rust
/// use email_clients::clients::memory::MemoryConfig;
///
/// let config = MemoryConfig::new("sender@example.com");
/// assert_eq!(config.sender.to_string(), "sender@example.com");
/// ```
pub fn new(sender: impl Into<EmailAddress>) -> Self {
Self {
sender: sender.into(),
}
}
}
impl From<String> for MemoryConfig {
/// Converts a `String` into a `MemoryConfig`.
///
/// # Parameters
/// - `value`: A `String` value which will be used to initialize a `MemoryConfig` instance.
///
/// # Returns
/// A new instance of `MemoryConfig` with the `sender` property set to the provided `String`.
///
/// # Examples
/// ```rust
/// use email_clients::clients::memory::MemoryConfig;
/// let value = String::from("sender@example.com");
/// let config = MemoryConfig::from(value);
/// assert_eq!(config.sender.to_string(), "sender@example.com");
/// ```
fn from(value: String) -> Self {
Self::new(value.as_str())
}
}
impl From<MemoryConfig> for EmailConfiguration {
/// Implementation of From trait to convert MemoryConfig into EmailConfiguration
///
/// # Example
///
/// ```rust
/// # use email_clients::configuration::EmailConfiguration;
/// # use email_clients::clients::memory::MemoryConfig;
/// #
/// let memory_config = MemoryConfig::new("sender@example.com");
/// let email_config: EmailConfiguration = memory_config.into();
/// #
/// # match email_config {
/// # EmailConfiguration::Memory(mc) => {
/// # assert_eq!(mc.sender.to_string(), "sender@example.com");
/// # },
/// # _ => panic!("Invalid conversion"),
/// # }
/// ```
fn from(value: MemoryConfig) -> Self {
EmailConfiguration::Memory(value)
}
}
#[derive(Clone, Debug)]
pub struct MemoryClient {
sender: EmailAddress,
tx: SyncSender<EmailObject>,
}
impl Default for MemoryClient {
/// `Default` implementation for `MemoryClient`.
///
/// This method will return a `MemoryClient` instance with an empty sender and a `SyncSender<EmailObject>`
/// with a channel buffer size of 5.
///
/// # Returns
/// A `MemoryClient` instance with the default configuration.
///
/// # Examples
///
/// ```rust
/// use email_clients::clients::memory::MemoryClient;
/// use email_clients::traits::EmailTrait;
///
/// let default_client = MemoryClient::default();
/// // Gets the default sender which is an empty string
/// assert_eq!(default_client.get_sender().to_string(), "");
/// ```
fn default() -> Self {
let (tx, _) = mpsc::sync_channel(5 /* usize */);
Self {
sender: "".into(),
tx,
}
}
}
impl MemoryClient {
/// Initializes a new `MemoryClient` with the provided `MemoryConfig`.
///
/// # Parameters
/// - `config`: A `MemoryConfig` instance that will be used to initialize the `MemoryClient`.
///
/// # Returns
/// A new instance of `MemoryClient` with the `sender` and `SyncSender<EmailObject>` set per the provided `MemoryConfig`.
///
/// # Examples
/// ```rust
/// # use email_clients::clients::memory::{MemoryConfig, MemoryClient};
/// # use email_clients::traits::EmailTrait;
///
/// let config = MemoryConfig::new("sender@example.com");
/// let client = MemoryClient::new(config);
/// assert_eq!(client.get_sender().to_string(), "sender@example.com");
/// ```
pub fn new(config: MemoryConfig) -> Self {
let (tx, _) = mpsc::sync_channel(5 /* usize */);
Self {
sender: config.sender,
tx,
}
}
/// Initializes a new `MemoryClient` with the provided `MemoryConfig` and `SyncSender<EmailObject>`.
///
/// # Parameters
/// - `config`: A `MemoryConfig` instance that will be used to initialize the `MemoryClient`.
/// - `tx`: A `SyncSender<EmailObject>` instance that will be used for sending emails.
///
/// # Returns
/// A new instance of `MemoryClient` with the `sender` and `SyncSender<EmailObject>` set per the provided parameters.
///
/// # Examples
/// ```rust
/// # use std::sync::mpsc::sync_channel;
/// # use email_clients::clients::memory::{MemoryConfig, MemoryClient};
/// # use email_clients::email::EmailObject;
/// # use email_clients::traits::EmailTrait;
///
/// let config = MemoryConfig::new("sender@example.com");
/// let (tx, rx) = sync_channel(2);
/// let client = MemoryClient::with_tx(config, tx.clone());
/// assert_eq!(client.get_sender().to_string(), "sender@example.com");
/// ```
pub fn with_tx(config: MemoryConfig, tx: SyncSender<EmailObject>) -> Self {
Self {
sender: config.sender,
tx,
}
}
}
#[async_trait]
impl EmailTrait for MemoryClient {
/// Returns the sender email address used for the `MemoryClient`.
///
/// # Returns
/// An `EmailAddress` that is used as the sender's email in the `MemoryClient`.
///
/// # Examples
///
/// ```rust
/// # use email_clients::clients::memory::{MemoryConfig, MemoryClient};
/// # use email_clients::traits::EmailTrait;
///
/// let config = MemoryConfig::new("sender@example.com");
/// let client = MemoryClient::new(config);
/// assert_eq!(client.get_sender().to_string(), "sender@example.com");
/// ```
fn get_sender(&self) -> EmailAddress {
self.sender.clone()
}
/// Sends email from memory client.
async fn send_emails(&self, email: EmailObject) -> crate::Result<()> {
self.tx
.send(email)
.map_err(|_| EmailError::UnexpectedError("Cannot send email in memory".to_string()))?;
Ok(())
}
}