1
1
use anyhow:: { bail, Context } ;
2
+ use email_address:: EmailAddress ;
2
3
use unscanny:: Scanner ;
3
4
4
5
/// Validates the format of an author field. It can contain a name and, in angle
@@ -9,12 +10,12 @@ pub fn validate_author(name: &str) -> anyhow::Result<()> {
9
10
s. eat_until ( |c| c == '<' ) ;
10
11
if s. eat_if ( '<' ) {
11
12
let contact = s. eat_until ( '>' ) ;
12
- if contact. starts_with ( '@' ) {
13
- validate_github_handle ( contact ) . context ( "GitHub handle is invalid" ) ?;
13
+ if let Some ( handle ) = contact. strip_prefix ( '@' ) {
14
+ validate_github_handle ( handle ) . context ( "GitHub handle is invalid" ) ?;
14
15
} else if contact. starts_with ( "http" ) {
15
16
validate_url ( contact) . context ( "URL is invalid" ) ?;
16
17
} else {
17
- validate_email ( contact) . context ( "email is invalid" ) ?;
18
+ let _addr : EmailAddress = contact. parse ( ) . context ( "email is invalid" ) ?;
18
19
}
19
20
if !s. eat_if ( '>' ) {
20
21
bail ! ( "expected '>'" ) ;
@@ -56,49 +57,11 @@ fn validate_url(url: &str) -> anyhow::Result<()> {
56
57
Ok ( ( ) )
57
58
}
58
59
59
- /// Validates an email address.
60
- fn validate_email ( email : & str ) -> anyhow:: Result < ( ) > {
61
- if email. len ( ) >= 254 {
62
- bail ! ( "cannot be longer than 254 characters" ) ;
63
- }
64
-
65
- let mut s = Scanner :: new ( email) ;
66
- let local = s. eat_until ( '@' ) ;
67
- let domain = s. eat_until ( '.' ) ;
68
- let tld = s. after ( ) ;
69
-
70
- if local. is_empty ( ) {
71
- bail ! ( "local part must not be empty" ) ;
72
- }
73
-
74
- if domain. is_empty ( ) {
75
- bail ! ( "domain must not be empty" ) ;
76
- }
77
-
78
- if tld. is_empty ( ) {
79
- bail ! ( "TLD must not be empty" ) ;
80
- }
81
-
82
- if !local. chars ( ) . all ( is_legal_in_email_local_part)
83
- || !domain. chars ( ) . all ( is_legal_in_url)
84
- || !tld. chars ( ) . all ( is_legal_in_url)
85
- {
86
- bail ! ( "contains illegal characters" ) ;
87
- }
88
-
89
- Ok ( ( ) )
90
- }
91
-
92
60
/// Whether a character is legal in a URL.
93
61
fn is_legal_in_url ( c : char ) -> bool {
94
62
c. is_ascii_alphanumeric ( ) || "-_.~:/?#[]@!$&'()*+,;=" . contains ( c)
95
63
}
96
64
97
- /// Whether a character is legal in the local part of an email.
98
- fn is_legal_in_email_local_part ( c : char ) -> bool {
99
- c. is_ascii_alphanumeric ( ) || "!#$%&'*+-/=?^_`{|}~." . contains ( c)
100
- }
101
-
102
65
#[ cfg( test) ]
103
66
mod tests {
104
67
use super :: validate_author;
0 commit comments