Skip to content
This repository was archived by the owner on Jan 14, 2026. It is now read-only.

Commit c349ddc

Browse files
committed
Add common trait implementations
1 parent 89e2d74 commit c349ddc

File tree

4 files changed

+38
-18
lines changed

4 files changed

+38
-18
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "tempmail"
3-
version = "0.2.0"
3+
version = "0.2.1"
44
description = "Simplify temporary email management and interaction, including message retrieval and attachment downloads."
55
authors = ["Dilshad <dilshadplayingminecraft@outlook.com>"]
66
edition = "2021"

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ To use this library in your project, simply add the following to your `Cargo.tom
4444

4545
```toml
4646
[dependencies]
47-
tempmail = "0.2"
47+
tempmail = "0.2.1"
4848
```
4949

5050
## License

src/lib.rs

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,13 @@
4242
use chrono::prelude::*;
4343
use rand::distributions::Alphanumeric;
4444
use rand::{thread_rng, Rng};
45-
use reqwest::IntoUrl;
4645
use serde::{Deserialize, Deserializer};
4746
use 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)]
5152
pub 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)]
6364
pub 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)]
9192
pub 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)]
110111
pub 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+
200207
impl 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

209216
impl 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>
213220
where
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.
230237
fn 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

Comments
 (0)