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
use regex::Regex;

#[derive(Debug, Getters, PartialEq, Clone, Eq, Hash)]
pub struct DomainName {
    #[get = "pub with_prefix"]
    pub(crate) domain_name: String,
}

impl DomainName {
    pub fn append_label(&mut self, label: &str) -> bool {
        lazy_static! {
            static ref LABEL_REGEX: Regex = Regex::new(r"[^-.\x00][^.\x00]*").unwrap();
        }

        let label_length = label.len();
        if label_length >= 64 {
            return false;
        }

        let domain_name_length = self.domain_name.len();
        if domain_name_length + label_length >= 256 {
            return false;
        }

        if LABEL_REGEX.is_match(label) {
            if &self.domain_name == "." {
                self.domain_name.insert_str(0, label);
            } else {
                self.domain_name.push_str(label);
                self.domain_name.push('.');
            }
            true
        } else {
            false
        }
    }
}

impl Default for DomainName {
    fn default() -> Self {
        DomainName {
            domain_name: ".".to_string(),
        }
    }
}