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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
use ::std::convert::AsRef;
use ::std::convert::From;
use ::std::fmt::Debug;
use ::std::fmt::Display;
use ::std::fmt::Formatter;
use ::std::fmt::Result as FmtResult;
use ::std::str::FromStr;

use crate::is_valid_email;
use crate::EmailError;

#[cfg(feature = "serde")]
mod email_visitor;
#[cfg(feature = "serde")]
pub(crate) use self::email_visitor::*;

#[cfg(feature = "serde")]
mod serde_support;

#[cfg(feature = "sea-orm")]
mod sea_orm_support;

/// A validated Email object.
///
/// These can be created from a string using `Email::from_string`,
/// `Email::from_str`, or `String::parse`.
///
/// Once built you can turn this like a `String`, use with Serde, or Sea Orm.
///
/// Note that Email objects _are_ case sensetive.
/// The email addresses `Email::from_str("bob@example.com")` and `Email::from_str("BoB@example.com")`,
/// will not be equal to each other.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Email {
    raw_email: String,
}

impl Email {
    /// Creates a new Email, from the `String` given.
    ///
    /// If the given string doesn't look like a valid email,
    /// then this will return an EmailError.
    pub fn from_string(raw_email: String) -> Result<Self, EmailError> {
        if !is_valid_email(&raw_email) {
            let err = EmailError::Invalid { raw_email };
            return Err(err);
        }

        Ok(Self { raw_email })
    }

    /// Creates a new Email, from the `str` given.
    ///
    /// If the given string doesn't look like a valid email,
    /// then this will return an EmailError.
    pub fn from_str<S>(raw_email: S) -> Result<Self, EmailError>
    where
        S: AsRef<str>,
    {
        Self::from_string(raw_email.as_ref().to_string())
    }

    /// Returns a new Email, where the email has been uppercased.
    pub fn to_lowercase(&self) -> Self {
        Self {
            raw_email: self.raw_email.to_lowercase(),
        }
    }

    /// Returns a new Email, where the internal email has been uppercased.
    pub fn to_uppercase(&self) -> Self {
        Self {
            raw_email: self.raw_email.to_uppercase(),
        }
    }

    pub fn as_str<'a>(&'a self) -> &'a str {
        &self.raw_email
    }
}

/// This is a common default, provided in use for stuff like tests.
///
/// The default email is `default@example.com`.
impl Default for Email {
    fn default() -> Self {
        Self::from_str("default@example.com").expect("Default Email should always be valid")
    }
}

impl Display for Email {
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        write!(f, "{}", self.raw_email)
    }
}

impl From<Email> for String {
    fn from(email: Email) -> Self {
        email.raw_email
    }
}

impl AsRef<str> for Email {
    fn as_ref(&self) -> &str {
        &self.raw_email
    }
}

impl AsRef<String> for Email {
    fn as_ref(&self) -> &String {
        &self.raw_email
    }
}

impl FromStr for Email {
    type Err = EmailError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Email::from_str(s)
    }
}

impl TryFrom<String> for Email {
    type Error = EmailError;

    fn try_from(raw: String) -> Result<Self, Self::Error> {
        Email::from_string(raw)
    }
}

impl<'a> TryFrom<&'a str> for Email {
    type Error = EmailError;

    fn try_from(raw: &'a str) -> Result<Self, Self::Error> {
        Email::from_str(raw)
    }
}

impl<'a> PartialEq<&'a str> for Email {
    fn eq(&self, other: &&'a str) -> bool {
        self.raw_email == *other
    }
}

impl PartialEq<String> for Email {
    fn eq(&self, other: &String) -> bool {
        self.raw_email == *other
    }
}

#[cfg(test)]
mod test_from_string {
    use super::*;

    #[test]
    fn it_should_accept_a_valid_email() {
        let maybe_email = Email::from_string("john@example.com".to_string());

        assert!(maybe_email.is_ok());
    }

    #[test]
    fn it_should_not_accept_a_non_valid_email() {
        let maybe_email = Email::from_string("foxes".to_string());

        assert!(maybe_email.is_err());
    }

    #[test]
    fn it_should_not_accept_a_domain_on_its_own() {
        let maybe_email = Email::from_string("@example.com".to_string());

        assert!(maybe_email.is_err());
    }

    #[test]
    fn it_should_not_accept_a_user_part_on_its_own() {
        let maybe_email = Email::from_string("john@".to_string());

        assert!(maybe_email.is_err());
    }

    #[test]
    fn it_should_not_accept_an_empty_string() {
        let maybe_email = Email::from_string("".to_string());

        assert!(maybe_email.is_err());
    }
}

#[cfg(test)]
mod test_from_str {
    use super::*;

    #[test]
    fn it_should_accept_a_valid_email() {
        let maybe_email = Email::from_str("john@example.com");

        assert!(maybe_email.is_ok());
    }

    #[test]
    fn it_should_not_accept_a_non_valid_email() {
        let maybe_email = Email::from_str("foxes");

        assert!(maybe_email.is_err());
    }
}

#[cfg(test)]
mod test_try_from {
    use super::*;

    #[test]
    fn it_should_parse_valid_email_from_str() {
        let email: Email = "fox@example.com".try_into().unwrap();

        assert_eq!(email, "fox@example.com");
    }

    #[test]
    fn it_should_not_parse_invalid_email_from_str() {
        let maybe_email: Result<Email, EmailError> = "🦊🦊🦊".try_into();

        assert!(maybe_email.is_err());
    }

    #[test]
    fn it_should_parse_valid_email_from_string() {
        let email: Email = "fox@example.com".to_string().try_into().unwrap();

        assert_eq!(email, "fox@example.com");
    }

    #[test]
    fn it_should_not_parse_invalid_email_from_string() {
        let maybe_email: Result<Email, EmailError> = "🦊🦊🦊".to_string().try_into();

        assert!(maybe_email.is_err());
    }
}

#[cfg(test)]
mod test_parse {
    use super::*;

    #[test]
    fn it_should_parse_valid_email_from_string() {
        let email: Email = "fox@example.com".parse().unwrap();

        assert_eq!(email, "fox@example.com");
    }

    #[test]
    fn it_should_not_parse_invalid_email_from_string() {
        let maybe_email: Result<Email, EmailError> = "🦊🦊🦊".parse();

        assert!(maybe_email.is_err());
    }
}

#[cfg(test)]
mod test_display {
    use super::*;

    #[test]
    fn it_should_write_same_email_as_given() {
        let email: Email = "fox@example.com".parse().unwrap();
        let output: String = format!("{}", email);

        assert!(email == output);
        assert_eq!(output, "fox@example.com");
    }
}

#[cfg(test)]
mod test_default {
    use super::*;

    #[test]
    fn it_should_create_a_valid_default() {
        let email = Email::default();

        assert!(is_valid_email(&email));
    }
}

#[cfg(test)]
mod test_to_lowercase {
    use super::*;

    #[test]
    fn it_should_make_it_lowercase() {
        let email: Email = "JoE@eXaMpLe.com".parse().unwrap();

        assert_eq!(
            email.to_lowercase(),
            Email::from_str("joe@example.com").unwrap()
        );
    }

    #[test]
    fn it_should_not_change_already_lowercase() {
        let email: Email = "joe@example.com".parse().unwrap();

        assert_eq!(
            email.to_lowercase(),
            Email::from_str("joe@example.com").unwrap()
        );
    }
}

#[cfg(test)]
mod test_to_uppercase {
    use super::*;

    #[test]
    fn it_should_make_it_uppercase() {
        let email: Email = "JoE@eXaMpLe.com".parse().unwrap();

        assert_eq!(
            email.to_uppercase(),
            Email::from_str("JOE@EXAMPLE.COM").unwrap()
        );
    }

    #[test]
    fn it_should_not_change_already_uppercase() {
        let email: Email = "joe@example.com".parse().unwrap();

        assert_eq!(
            email.to_uppercase(),
            Email::from_str("JOE@EXAMPLE.COM").unwrap()
        );
    }
}

#[cfg(test)]
mod test_partial_eq {
    use super::*;

    #[test]
    fn it_should_be_equal_to_strs() {
        let email: Email = "joe@example.com".parse().unwrap();
        let is_equal = email == "joe@example.com";

        assert!(is_equal);
    }

    #[test]
    fn it_should_not_be_equal_to_different_strs() {
        let email: Email = "joe@example.com".parse().unwrap();
        let is_not_equal = email != "🦊@example.com";

        assert!(is_not_equal);
    }
}