|
1 | 1 | use idna::domain_to_ascii; |
2 | | -use once_cell::sync::Lazy; |
3 | 2 | use regex::Regex; |
4 | | -use std::borrow::Cow; |
| 3 | +use std::{borrow::Cow, sync::LazyLock}; |
5 | 4 |
|
6 | 5 | use crate::ValidateIp; |
7 | 6 |
|
8 | 7 | // Regex from the specs |
9 | 8 | // https://html.spec.whatwg.org/multipage/forms.html#valid-e-mail-address |
10 | 9 | // It will mark esoteric email addresses like quoted string as invalid |
11 | | -static EMAIL_USER_RE: Lazy<Regex> = |
12 | | - Lazy::new(|| Regex::new(r"^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+\z").unwrap()); |
13 | | -static EMAIL_DOMAIN_RE: Lazy<Regex> = Lazy::new(|| { |
| 10 | +static EMAIL_USER_RE: LazyLock<Regex> = |
| 11 | + LazyLock::new(|| Regex::new(r"^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+\z").unwrap()); |
| 12 | +static EMAIL_DOMAIN_RE: LazyLock<Regex> = LazyLock::new(|| { |
14 | 13 | Regex::new( |
15 | 14 | r"^[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$" |
16 | 15 | ).unwrap() |
17 | 16 | }); |
18 | 17 | // literal form, ipv4 or ipv6 address (SMTP 4.1.3) |
19 | | -static EMAIL_LITERAL_RE: Lazy<Regex> = |
20 | | - Lazy::new(|| Regex::new(r"\[([a-fA-F0-9:\.]+)\]\z").unwrap()); |
| 18 | +static EMAIL_LITERAL_RE: LazyLock<Regex> = |
| 19 | + LazyLock::new(|| Regex::new(r"\[([a-fA-F0-9:\.]+)\]\z").unwrap()); |
21 | 20 |
|
22 | 21 | /// Checks if the domain is a valid domain and if not, check whether it's an IP |
23 | 22 | #[must_use] |
@@ -109,7 +108,7 @@ where |
109 | 108 | } |
110 | 109 | } |
111 | 110 |
|
112 | | -impl<'a> ValidateEmail for &'a str { |
| 111 | +impl ValidateEmail for &str { |
113 | 112 | fn as_email_string(&self) -> Option<Cow<'_, str>> { |
114 | 113 | Some(Cow::from(*self)) |
115 | 114 | } |
@@ -209,9 +208,9 @@ mod tests { |
209 | 208 | fn test_validate_email_rfc5321() { |
210 | 209 | // 65 character local part |
211 | 210 | let test = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa@mail.com"; |
212 | | - assert_eq!(test.validate_email(), false); |
| 211 | + assert!(!test.validate_email()); |
213 | 212 | // 256 character domain part |
214 | 213 | let test = "a@aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.com"; |
215 | | - assert_eq!(test.validate_email(), false); |
| 214 | + assert!(!test.validate_email()); |
216 | 215 | } |
217 | 216 | } |
0 commit comments