blob: 60e26246e9abb7857b9cc99b96f7f2769e6c9f76 (
plain)
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
|
//! Provides e-mail parsing functions.
use super::{Result, Error};
/// Stores the local_part and domain of an email address.
pub(crate) struct EmailAddress {
pub(crate) local_part: String,
pub(crate) domain: String,
}
impl EmailAddress {
/// Returns an EmailAddress from an email address string.
///
/// From [draft-koch]:
///
///```text
/// To help with the common pattern of using capitalized names
/// (e.g. "Joe.Doe@example.org") for mail addresses, and under the
/// premise that almost all MTAs treat the local-part case-insensitive
/// and that the domain-part is required to be compared
/// case-insensitive anyway, all upper-case ASCII characters in a User
/// ID are mapped to lowercase. Non-ASCII characters are not changed.
///```
pub(crate) fn from<S: AsRef<str>>(email_address: S) -> Result<Self> {
// Ensure that is a valid email address by parsing it and return the
// errors that it returns.
// This is also done in hagrid.
let email_address = email_address.as_ref();
let v: Vec<&str> = email_address.split('@').collect();
if v.len() != 2 {
return Err(Error::MalformedEmail(email_address.into()).into())
};
// Convert domain to lowercase without tailoring, i.e. without taking any
// locale into account. See:
// https://doc.rust-lang.org/std/primitive.str.html#method.to_lowercase
//
// Keep the local part as-is as we'll need that to generate WKD URLs.
let email = EmailAddress {
local_part: v[0].to_string(),
domain: v[1].to_lowercase()
};
Ok(email)
}
}
|