1use lazy_static::lazy_static;
2use regex::Regex;
3
4pub fn is_dns(s: impl AsRef<str>) -> bool {
28 DNS_REGEX.is_match(s.as_ref())
29}
30
31lazy_static! {
32 static ref DNS_REGEX: Regex =
33 Regex::new(r"^(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$").unwrap();
34}
35
36#[cfg(test)]
37mod tests {
38 use super::*;
39
40 #[test]
41 fn test_is_dns() {
42 assert_eq!(true, is_dns("abc.com"));
43 assert_eq!(true, is_dns("123.cn"));
44 assert_eq!(true, is_dns("123.abc.com"));
45
46 assert_eq!(false, is_dns(""));
47 assert_eq!(false, is_dns("abc@com"));
48 assert_eq!(false, is_dns("@#$.com."));
49 assert_eq!(false, is_dns("http://abc.com"));
50 }
51}