mail_tm_rs/
user.rs

1use serde::{Deserialize, Serialize};
2use rand::distributions::Alphanumeric;
3use rand::Rng;
4
5/// A global User
6///
7/// This user is the secret sauce for all things to do with this API. There will be raw counterparts
8/// but for now this is what you need.
9///
10/// Implements all the serde types and provides a random implementation to provide quick users.
11///
12/// id: the id of the email
13/// domain: email domain
14/// password: password
15/// email_token: the jwt token returned by mail-tm
16#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
17#[serde(rename_all = "camelCase")]
18pub struct User {
19    pub id: String,
20    pub domain: String,
21    pub password: String,
22    pub email_token: String,
23}
24
25impl User {
26    pub fn new(email: &str, password: &str, domain: &str) -> User {
27        User {
28            id: email.to_string(),
29            domain: domain.to_string(),
30            password: password.to_string(),
31            email_token: "".to_string(),
32        }
33    }
34
35    pub fn with_domain(self, domain: &str) -> User {
36        User {
37            domain: domain.to_string(),
38            ..self
39        }
40    }
41
42    fn get_random_string(len: usize) -> String {
43        rand::thread_rng().sample_iter(&Alphanumeric).take(len).map(char::from).collect()
44    }
45}
46
47impl Default for User {
48    fn default() -> Self {
49        User {
50            id: User::get_random_string(10), // Default could be rand
51            password: User::get_random_string(13),
52            email_token: "".to_string(),
53            domain: "".to_string(),
54        }
55    }
56}