Skip to content

Commit

Permalink
Add common trait implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
a-rustacean committed Aug 23, 2023
1 parent 89e2d74 commit c349ddc
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tempmail"
version = "0.2.0"
version = "0.2.1"
description = "Simplify temporary email management and interaction, including message retrieval and attachment downloads."
authors = ["Dilshad <dilshadplayingminecraft@outlook.com>"]
edition = "2021"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ To use this library in your project, simply add the following to your `Cargo.tom

```toml
[dependencies]
tempmail = "0.2"
tempmail = "0.2.1"
```

## License
Expand Down
50 changes: 35 additions & 15 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,13 @@
use chrono::prelude::*;
use rand::distributions::Alphanumeric;
use rand::{thread_rng, Rng};
use reqwest::IntoUrl;
use serde::{Deserialize, Deserializer};
use std::{error::Error, fmt::Display};

const API_URL: &str = "https://www.1secmail.com/api/v1/";

/// Represents an attachment associated with an email message.
#[derive(Debug, Clone, Deserialize)]
#[derive(Debug, Clone, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
pub struct TempmailAttachment {
/// The filename of the attachment.
pub filename: String,
Expand All @@ -59,7 +60,7 @@ pub struct TempmailAttachment {
}

/// Represents an email message received in the temporary email inbox.
#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct TempmailMessage {
/// The unique identifier of the message.
pub id: usize,
Expand Down Expand Up @@ -87,7 +88,7 @@ struct TempmailMessageRaw {
}

/// Enum representing different temporary email domains.
#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum Domain {
/// Domain "1secmail.com"
SecMailCom,
Expand All @@ -106,7 +107,7 @@ pub enum Domain {
}

/// Represents a temporary email address with associated domain for receiving emails.
#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct Tempmail {
/// The username part of the email address.
pub username: String,
Expand Down Expand Up @@ -197,6 +198,12 @@ impl Display for Domain {
}
}

impl Default for Domain {
fn default() -> Self {
Self::SecMailCom
}
}

impl Display for TempmailError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Expand All @@ -208,13 +215,13 @@ impl Display for TempmailError {

impl Error for TempmailError {}

/// A helper function to perform a JSON GET request and deserialize the response.
async fn reqjson<U, T>(url: U) -> TempmailResult<T>
// A helper function to perform a JSON GET request and deserialize the response.
async fn reqjson<T, R>(query: T) -> TempmailResult<R>
where
U: IntoUrl,
T: for<'de> serde::Deserialize<'de>,
T: AsRef<str>,
R: for<'de> serde::Deserialize<'de>,
{
match reqwest::get(url).await {
match reqwest::get(format!("{}?{}", API_URL, query.as_ref())).await {
Ok(response) => {
let text = response
.text()
Expand All @@ -226,7 +233,7 @@ where
}
}

/// A helper functon for generating a random string of the specified length.
// A helper functon for generating a random string of the specified length.
fn generate_random_string(length: usize) -> String {
let rng = thread_rng();
let random_string: String = rng
Expand All @@ -245,7 +252,7 @@ impl Tempmail {
{
Self {
username: username.into(),
domain: domain.unwrap_or(Domain::SecMailCom),
domain: domain.unwrap_or_default(),
}
}

Expand All @@ -262,7 +269,7 @@ impl Tempmail {
/// Fetches the messages in the inbox.
pub async fn get_messages(&self) -> TempmailResult<Vec<TempmailMessage>> {
let raw_messages: Vec<TempmailMessageRaw> = reqjson(format!(
"https://www.1secmail.com/api/v1/?action=getMessages&login={}&domain={}",
"action=getMessages&login={}&domain={}",
self.username, self.domain
))
.await?;
Expand All @@ -271,7 +278,7 @@ impl Tempmail {

for raw_message in raw_messages {
let mut message: TempmailMessage = reqjson(format!(
"https://www.1secmail.com/api/v1/?action=readMessage&login={}&domain={}&id={}",
"action=readMessage&login={}&domain={}&id={}",
self.username, self.domain, raw_message.id
))
.await?;
Expand All @@ -294,7 +301,7 @@ impl Tempmail {
T: AsRef<str>,
{
reqwest::get(format!(
"https://www.1secmail.com/api/v1/?action=download&login={}&domain={}&id={}&file={}",
"action=download&login={}&domain={}&id={}&file={}",
self.username,
self.domain,
message_id,
Expand All @@ -308,3 +315,16 @@ impl Tempmail {
.map(|bytes| bytes.to_vec())
}
}

// `Send` and `Sync` trait implementations for public structs

unsafe impl Send for Domain {}
unsafe impl Sync for Domain {}
unsafe impl Send for Tempmail {}
unsafe impl Sync for Tempmail {}
unsafe impl Send for TempmailError {}
unsafe impl Sync for TempmailError {}
unsafe impl Send for TempmailMessage {}
unsafe impl Sync for TempmailMessage {}
unsafe impl Send for TempmailAttachment {}
unsafe impl Sync for TempmailAttachment {}

0 comments on commit c349ddc

Please sign in to comment.