4242use chrono:: prelude:: * ;
4343use rand:: distributions:: Alphanumeric ;
4444use rand:: { thread_rng, Rng } ;
45- use reqwest:: IntoUrl ;
4645use serde:: { Deserialize , Deserializer } ;
4746use std:: { error:: Error , fmt:: Display } ;
4847
48+ const API_URL : & str = "https://www.1secmail.com/api/v1/" ;
49+
4950/// Represents an attachment associated with an email message.
50- #[ derive( Debug , Clone , Deserialize ) ]
51+ #[ derive( Debug , Clone , Deserialize , PartialEq , Eq , PartialOrd , Ord ) ]
5152pub struct TempmailAttachment {
5253 /// The filename of the attachment.
5354 pub filename : String ,
@@ -59,7 +60,7 @@ pub struct TempmailAttachment {
5960}
6061
6162/// Represents an email message received in the temporary email inbox.
62- #[ derive( Debug , Clone ) ]
63+ #[ derive( Debug , Clone , PartialEq , Eq , PartialOrd , Ord ) ]
6364pub struct TempmailMessage {
6465 /// The unique identifier of the message.
6566 pub id : usize ,
@@ -87,7 +88,7 @@ struct TempmailMessageRaw {
8788}
8889
8990/// Enum representing different temporary email domains.
90- #[ derive( Debug , Clone ) ]
91+ #[ derive( Debug , Clone , PartialEq , Eq , PartialOrd , Ord ) ]
9192pub enum Domain {
9293 /// Domain "1secmail.com"
9394 SecMailCom ,
@@ -106,7 +107,7 @@ pub enum Domain {
106107}
107108
108109/// Represents a temporary email address with associated domain for receiving emails.
109- #[ derive( Debug , Clone ) ]
110+ #[ derive( Debug , Clone , PartialEq , Eq , PartialOrd , Ord ) ]
110111pub struct Tempmail {
111112 /// The username part of the email address.
112113 pub username : String ,
@@ -197,6 +198,12 @@ impl Display for Domain {
197198 }
198199}
199200
201+ impl Default for Domain {
202+ fn default ( ) -> Self {
203+ Self :: SecMailCom
204+ }
205+ }
206+
200207impl Display for TempmailError {
201208 fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
202209 match self {
@@ -208,13 +215,13 @@ impl Display for TempmailError {
208215
209216impl Error for TempmailError { }
210217
211- /// A helper function to perform a JSON GET request and deserialize the response.
212- async fn reqjson < U , T > ( url : U ) -> TempmailResult < T >
218+ // A helper function to perform a JSON GET request and deserialize the response.
219+ async fn reqjson < T , R > ( query : T ) -> TempmailResult < R >
213220where
214- U : IntoUrl ,
215- T : for < ' de > serde:: Deserialize < ' de > ,
221+ T : AsRef < str > ,
222+ R : for < ' de > serde:: Deserialize < ' de > ,
216223{
217- match reqwest:: get ( url ) . await {
224+ match reqwest:: get ( format ! ( "{}?{}" , API_URL , query . as_ref ( ) ) ) . await {
218225 Ok ( response) => {
219226 let text = response
220227 . text ( )
@@ -226,7 +233,7 @@ where
226233 }
227234}
228235
229- /// A helper functon for generating a random string of the specified length.
236+ // A helper functon for generating a random string of the specified length.
230237fn generate_random_string ( length : usize ) -> String {
231238 let rng = thread_rng ( ) ;
232239 let random_string: String = rng
@@ -245,7 +252,7 @@ impl Tempmail {
245252 {
246253 Self {
247254 username : username. into ( ) ,
248- domain : domain. unwrap_or ( Domain :: SecMailCom ) ,
255+ domain : domain. unwrap_or_default ( ) ,
249256 }
250257 }
251258
@@ -262,7 +269,7 @@ impl Tempmail {
262269 /// Fetches the messages in the inbox.
263270 pub async fn get_messages ( & self ) -> TempmailResult < Vec < TempmailMessage > > {
264271 let raw_messages: Vec < TempmailMessageRaw > = reqjson ( format ! (
265- "https://www.1secmail.com/api/v1/? action=getMessages&login={}&domain={}" ,
272+ "action=getMessages&login={}&domain={}" ,
266273 self . username, self . domain
267274 ) )
268275 . await ?;
@@ -271,7 +278,7 @@ impl Tempmail {
271278
272279 for raw_message in raw_messages {
273280 let mut message: TempmailMessage = reqjson ( format ! (
274- "https://www.1secmail.com/api/v1/? action=readMessage&login={}&domain={}&id={}" ,
281+ "action=readMessage&login={}&domain={}&id={}" ,
275282 self . username, self . domain, raw_message. id
276283 ) )
277284 . await ?;
@@ -294,7 +301,7 @@ impl Tempmail {
294301 T : AsRef < str > ,
295302 {
296303 reqwest:: get ( format ! (
297- "https://www.1secmail.com/api/v1/? action=download&login={}&domain={}&id={}&file={}" ,
304+ "action=download&login={}&domain={}&id={}&file={}" ,
298305 self . username,
299306 self . domain,
300307 message_id,
@@ -308,3 +315,16 @@ impl Tempmail {
308315 . map ( |bytes| bytes. to_vec ( ) )
309316 }
310317}
318+
319+ // `Send` and `Sync` trait implementations for public structs
320+
321+ unsafe impl Send for Domain { }
322+ unsafe impl Sync for Domain { }
323+ unsafe impl Send for Tempmail { }
324+ unsafe impl Sync for Tempmail { }
325+ unsafe impl Send for TempmailError { }
326+ unsafe impl Sync for TempmailError { }
327+ unsafe impl Send for TempmailMessage { }
328+ unsafe impl Sync for TempmailMessage { }
329+ unsafe impl Send for TempmailAttachment { }
330+ unsafe impl Sync for TempmailAttachment { }
0 commit comments