checkmail/
lib.rs

1
2extern crate regex;
3#[macro_use]
4extern crate lazy_static;
5
6use regex::Regex;
7
8lazy_static! {
9    
10    // Perform a simple regex match to validate email
11    static ref EMAIL_REGEX: regex::Regex = Regex::new("^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[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])?)*$").unwrap();
12}
13
14
15/// Validates Email Format using regex.
16/// See [this](https://www.w3.org/TR/html5/forms.html#valid-e-mail-address) for more information
17pub fn validate_email(email: &String) -> bool {
18    EMAIL_REGEX.is_match(email.as_str())
19}
20
21mod tests {
22
23    #[test]
24    fn good_email() {
25        let mut email = Vec::new();
26
27        email.push(String::from("ishanjain28@gmail.com"));
28        email.push(String::from("abc+xyz@google.com"));
29
30        for e in email {
31            assert_eq!(true, super::validate_email(&e));
32        }
33    }
34
35    #[test]
36    fn bad_email() {
37        let email = String::from("ishanjain.gmail.com");
38
39        assert_eq!(false, super::validate_email(&email));
40    }
41}