File: string.rs

package info (click to toggle)
icann-rdap-common 0.0.25-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,480 kB
  • sloc: makefile: 2
file content (295 lines) | stat: -rw-r--r-- 7,800 bytes parent folder | download
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/// Functions for types that can be turned into strings.
///
/// Example:
/// ```rust
/// use icann_rdap_common::check::*;
///
/// let s = "  ";
/// assert!(s.is_whitespace_or_empty());
/// ```
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;

    /// Tests if a string is a Unicode domain name.
    fn is_unicode_domain_name(&self) -> bool;

    /// Tests if a string is begins with a period and only has one label.
    fn is_tld(&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()))
    }

    fn is_unicode_domain_name(&self) -> bool {
        let s = self.to_string();
        s == "."
            || (!s.is_empty()
                && s.split_terminator('.').all(|s| {
                    s.chars()
                        .all(|c| c == '-' || (!c.is_ascii_punctuation() && !c.is_whitespace()))
                }))
    }

    fn is_tld(&self) -> bool {
        let s = self.to_string();
        s.starts_with('.')
            && s.len() > 2
            && s.matches('.').count() == 1
            && s.split_terminator('.').all(|s| {
                s.chars()
                    .all(|c| !c.is_ascii_punctuation() && !c.is_whitespace())
            })
    }
}

/// Functions for types that can be turned into arrays of strings.
///
/// Example:
/// ```rust
/// use icann_rdap_common::check::*;
///
/// let a: &[&str] = &["foo",""];
/// assert!(a.is_empty_or_any_empty_or_whitespace());
/// ```
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())
    }
}

impl<T: ToString> StringListCheck for Vec<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())
    }
}

/// Functions for chars.
///
/// Example:
/// ```rust
/// use icann_rdap_common::check::*;
///
/// let c = 'a';
/// assert!(c.is_ldh());
/// ```
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, 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("foo", false)]
    #[case("", false)]
    #[case("foo-bar", false)]
    #[case("foo bar", false)]
    #[case(".", false)]
    #[case(".foo.bar", false)]
    #[case(".foo", true)]
    fn GIVEN_string_WHEN_is_tld_THEN_correct_result(
        #[case] test_string: &str,
        #[case] expected: bool,
    ) {
        // GIVEN in parameters

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

        // 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);
    }

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

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

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