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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
pub trait StringCheck {
    /// Tests if the string is empty, including for if the string only has whitespace.
    fn is_whitespace_or_empty(&self) -> bool;

    /// Tests if the string contains only letters, digits, or hyphens and is not empty.
    fn is_ldh_string(&self) -> bool;

    /// Tests if a string is an LDH doamin name. This is not to be confused with [StringCheck::is_ldh_string],
    /// which checks individual domain labels.
    fn is_ldh_domain_name(&self) -> bool;
}

impl<T: ToString> StringCheck for T {
    fn is_whitespace_or_empty(&self) -> bool {
        let s = self.to_string();
        s.is_empty() || s.chars().all(char::is_whitespace)
    }

    fn is_ldh_string(&self) -> bool {
        let s = self.to_string();
        !s.is_empty() && s.chars().all(char::is_ldh)
    }

    fn is_ldh_domain_name(&self) -> bool {
        let s = self.to_string();
        s == "." || (!s.is_empty() && s.split_terminator('.').all(|s| s.is_ldh_string()))
    }
}

pub trait StringListCheck {
    /// Tests if a list of strings is empty, or if any of the
    /// elemeents of the list are empty or whitespace.
    fn is_empty_or_any_empty_or_whitespace(&self) -> bool;

    /// Tests if a list of strings ard LDH strings. See [CharCheck::is_ldh].
    fn is_ldh_string_list(&self) -> bool;
}

impl<T: ToString> StringListCheck for &[T] {
    fn is_empty_or_any_empty_or_whitespace(&self) -> bool {
        self.is_empty() || self.iter().any(|s| s.to_string().is_whitespace_or_empty())
    }

    fn is_ldh_string_list(&self) -> bool {
        !self.is_empty() && self.iter().all(|s| s.to_string().is_ldh_string())
    }
}

pub trait CharCheck {
    /// Checks if the character is a letter, digit or a hyphen
    #[allow(clippy::wrong_self_convention)]
    fn is_ldh(self) -> bool;
}

impl CharCheck for char {
    fn is_ldh(self) -> bool {
        matches!(self, 'A'..='Z' | 'a'..='z' | '0'..='9' | '-')
    }
}

#[cfg(test)]
#[allow(non_snake_case)]
mod tests {
    use rstest::rstest;

    use crate::check::string::CharCheck;
    use crate::check::string::StringListCheck;

    use super::StringCheck;

    #[rstest]
    #[case("foo", false)]
    #[case("", true)]
    #[case(" ", true)]
    #[case("foo bar", false)]
    fn GIVEN_string_WHEN_is_whitespace_or_empty_THEN_correct_result(
        #[case] test_string: &str,
        #[case] expected: bool,
    ) {
        // GIVEN in parameters

        // WHEN
        let actual = test_string.is_whitespace_or_empty();

        // THEN
        assert_eq!(actual, expected);
    }

    #[rstest]
    #[case(&[], true)]
    #[case(&["foo"], false)]
    #[case(&["foo",""], true)]
    #[case(&["foo","bar"], false)]
    #[case(&["foo","bar baz"], false)]
    #[case(&[""], true)]
    #[case(&[" "], true)]
    fn GIVEN_string_list_WHEN_is_whitespace_or_empty_THEN_correct_result(
        #[case] test_list: &[&str],
        #[case] expected: bool,
    ) {
        // GIVEN in parameters

        // WHEN
        let actual = test_list.is_empty_or_any_empty_or_whitespace();

        // THEN
        assert_eq!(actual, expected);
    }

    #[rstest]
    #[case('a', true)]
    #[case('l', true)]
    #[case('z', true)]
    #[case('A', true)]
    #[case('L', true)]
    #[case('Z', true)]
    #[case('0', true)]
    #[case('3', true)]
    #[case('9', true)]
    #[case('-', true)]
    #[case('_', false)]
    #[case('.', false)]
    fn GIVEN_char_WHEN_is_ldh_THEN_correct_result(#[case] test_char: char, #[case] expected: bool) {
        // GIVEN in parameters

        // WHEN
        let actual = test_char.is_ldh();

        // THEN
        assert_eq!(actual, expected);
    }

    #[rstest]
    #[case("foo", true)]
    #[case("", false)]
    #[case("foo-bar", true)]
    #[case("foo bar", false)]
    fn GIVEN_string_WHEN_is_ldh_string_THEN_correct_result(
        #[case] test_string: &str,
        #[case] expected: bool,
    ) {
        // GIVEN in parameters

        // WHEN
        let actual = test_string.is_ldh_string();

        // THEN
        assert_eq!(actual, expected);
    }

    #[rstest]
    #[case(&[], false)]
    #[case(&["foo"], true)]
    #[case(&["foo",""], false)]
    #[case(&["foo","bar"], true)]
    #[case(&["foo","bar baz"], false)]
    #[case(&[""], false)]
    #[case(&[" "], false)]
    fn GIVEN_string_list_WHEN_is_ldh_string_list_THEN_correct_result(
        #[case] test_list: &[&str],
        #[case] expected: bool,
    ) {
        // GIVEN in parameters

        // WHEN
        let actual = test_list.is_ldh_string_list();

        // THEN
        assert_eq!(actual, expected);
    }

    #[rstest]
    #[case("foo", true)]
    #[case("", false)]
    #[case(".", true)]
    #[case("foo.bar", true)]
    #[case("foo.bar.", true)]
    fn GIVEN_string_WHEN_is_ldh_domain_name_THEN_correct_result(
        #[case] test_string: &str,
        #[case] expected: bool,
    ) {
        // GIVEN in parameters

        // WHEN
        let actual = test_string.is_ldh_domain_name();

        // THEN
        assert_eq!(actual, expected);
    }
}