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
47
48
49
50
51
use lazy_static::lazy_static;
use regex::Regex;
/// Checks if the string is a valid domain name.
///
/// # Arguments
///
/// * `s` - The string to check.
///
/// # Returns
///
/// Returns true if string is a valid domain name, false if not.
///
/// # Examples
///
/// ```
/// use rufl::string;
///
/// assert_eq!(true, string::is_dns("abc.com"));
///
/// assert_eq!(true, string::is_dns("123.cn"));
///
/// assert_eq!(false, string::is_dns("abc@123.com"));
///
/// ```
pub fn is_dns(s: impl AsRef<str>) -> bool {
DNS_REGEX.is_match(s.as_ref())
}
lazy_static! {
static ref DNS_REGEX: Regex =
Regex::new(r"^(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$").unwrap();
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_is_dns() {
assert_eq!(true, is_dns("abc.com"));
assert_eq!(true, is_dns("123.cn"));
assert_eq!(true, is_dns("123.abc.com"));
assert_eq!(false, is_dns(""));
assert_eq!(false, is_dns("abc@com"));
assert_eq!(false, is_dns("@#$.com."));
assert_eq!(false, is_dns("http://abc.com"));
}
}