Debian Codegraph @

src/check/string.rs

raw
1 /// Functions for types that can be turned into strings.
2 ///
3 /// Example:
4 /// ```rust
5 /// use icann_rdap_common::check::*;
6 ///
7 /// let s = " ";
8 /// assert!(s.is_whitespace_or_empty());
9 /// ```
10 pub trait StringCheck {
11 /// Tests if the string is empty, including for if the string only has whitespace.
12 fn is_whitespace_or_empty(&self) -> bool;
13
14 /// Tests if the string contains only letters, digits, or hyphens and is not empty.
15 fn is_ldh_string(&self) -> bool;
16
17 /// Tests if a string is an LDH domain name. This is not to be confused with [StringCheck::is_ldh_string],
18 /// which checks individual domain labels.
19 fn is_ldh_domain_name(&self) -> bool;
20
21 /// Tests if a string is a Unicode domain name.
22 fn is_unicode_domain_name(&self) -> bool;
23
24 /// Tests if a string begins with a period and only has one label.
25 fn is_tld(&self) -> bool;
26
27 /// Tests if a string looks like an ASCII table line (pipe-delimited or border).
28 fn is_ascii_table_line(&self) -> bool;
29
30 /// Tests if a string is an ldh host name (i.e., at least to labels)
31 fn is_ldh_hostname(&self) -> bool;
32 }
33
34 impl<T: ToString> StringCheck for T {
35 fn is_whitespace_or_empty(&self) -> bool {
36 let s = self.to_string();
37 s.is_empty() || s.chars().all(char::is_whitespace)
38 }
39
40 fn is_ldh_string(&self) -> bool {
41 let s = self.to_string();
42 !s.is_empty() && s.chars().all(char::is_ldh)
43 }
44
45 fn is_ldh_domain_name(&self) -> bool {
46 let s = self.to_string();
47 s == "." || (!s.is_empty() && s.split_terminator('.').all(|s| s.is_ldh_string()))
48 }
49
50 fn is_unicode_domain_name(&self) -> bool {
51 let s = self.to_string();
52 s == "."
53 || (!s.is_empty()
54 && s.split_terminator('.').all(|s| {
55 s.chars()
56 .all(|c| c == '-' || (!c.is_ascii_punctuation() && !c.is_whitespace()))
57 }))
58 }
59
60 fn is_tld(&self) -> bool {
61 let s = self.to_string();
62 s.starts_with('.')
63 && s.len() > 2
64 && s.matches('.').count() == 1
65 && s.split_terminator('.').all(|s| {
66 s.chars()
67 .all(|c| !c.is_ascii_punctuation() && !c.is_whitespace())
68 })
69 }
70
71 fn is_ascii_table_line(&self) -> bool {
72 let s = self.to_string();
73 let trimmed = s.trim();
74 if trimmed.is_empty() {
75 return false;
76 }
77 // Pipe-delimited table row: starts or ends with |
78 if trimmed.starts_with('|') || trimmed.ends_with('|') {
79 return true;
80 }
81 // Table border: only +, -, =, and spaces
82 if trimmed.chars().all(|c| matches!(c, '+' | '-' | '=' | ' ')) {
83 return true;
84 }
85 false
86 }
87
88 fn is_ldh_hostname(&self) -> bool {
89 let s = self.to_string();
90 let count = s.split_terminator('.').try_fold(0, |acc, s| {
91 if s.is_ldh_string() {
92 Ok(acc + 1)
93 } else {
94 Err(acc)
95 }
96 });
97 match count {
98 Ok(count) => count > 1,
99 Err(_) => false,
100 }
101 }
102 }
103
104 /// Functions for types that can be turned into arrays of strings.
105 ///
106 /// Example:
107 /// ```rust
108 /// use icann_rdap_common::check::*;
109 ///
110 /// let a: &[&str] = &["foo",""];
111 /// assert!(a.is_empty_or_any_empty_or_whitespace());
112 /// ```
113 pub trait StringListCheck {
114 /// Tests if a list of strings is empty, or if any of the
115 /// elements of the list are empty or whitespace.
116 fn is_empty_or_any_empty_or_whitespace(&self) -> bool;
117
118 /// Tests if a list of strings are LDH strings. See [CharCheck::is_ldh].
119 fn is_ldh_string_list(&self) -> bool;
120 }
121
122 impl<T: ToString> StringListCheck for &[T] {
123 fn is_empty_or_any_empty_or_whitespace(&self) -> bool {
124 self.is_empty() || self.iter().any(|s| s.to_string().is_whitespace_or_empty())
125 }
126
127 fn is_ldh_string_list(&self) -> bool {
128 !self.is_empty() && self.iter().all(|s| s.to_string().is_ldh_string())
129 }
130 }
131
132 impl<T: ToString> StringListCheck for Vec<T> {
133 fn is_empty_or_any_empty_or_whitespace(&self) -> bool {
134 self.is_empty() || self.iter().any(|s| s.to_string().is_whitespace_or_empty())
135 }
136
137 fn is_ldh_string_list(&self) -> bool {
138 !self.is_empty() && self.iter().all(|s| s.to_string().is_ldh_string())
139 }
140 }
141
142 /// Functions for chars.
143 ///
144 /// Example:
145 /// ```rust
146 /// use icann_rdap_common::check::*;
147 ///
148 /// let c = 'a';
149 /// assert!(c.is_ldh());
150 /// ```
151 pub trait CharCheck {
152 /// Checks if the character is a letter, digit or a hyphen
153 #[allow(clippy::wrong_self_convention)]
154 fn is_ldh(self) -> bool;
155 }
156
157 impl CharCheck for char {
158 fn is_ldh(self) -> bool {
159 matches!(self, 'A'..='Z' | 'a'..='z' | '0'..='9' | '-')
160 }
161 }
162
163 #[cfg(test)]
164 mod tests {
165 use rstest::rstest;
166
167 use crate::check::string::{CharCheck, StringListCheck};
168
169 use super::StringCheck;
170
171 #[rstest]
172 #[case("foo", false)]
173 #[case("", true)]
174 #[case(" ", true)]
175 #[case("foo bar", false)]
176 fn test_is_whitespace_or_empty(#[case] test_string: &str, #[case] expected: bool) {
177 // GIVEN in parameters
178
179 // WHEN
180 let actual = test_string.is_whitespace_or_empty();
181
182 // THEN
183 assert_eq!(actual, expected);
184 }
185
186 #[rstest]
187 #[case(&[], true)]
188 #[case(&["foo"], false)]
189 #[case(&["foo",""], true)]
190 #[case(&["foo","bar"], false)]
191 #[case(&["foo","bar baz"], false)]
192 #[case(&[""], true)]
193 #[case(&[" "], true)]
194 fn test_is_whitespace_or_any_empty_or_whitespace(
195 #[case] test_list: &[&str],
196 #[case] expected: bool,
197 ) {
198 // GIVEN in parameters
199
200 // WHEN
201 let actual = test_list.is_empty_or_any_empty_or_whitespace();
202
203 // THEN
204 assert_eq!(actual, expected);
205 }
206
207 #[rstest]
208 #[case('a', true)]
209 #[case('l', true)]
210 #[case('z', true)]
211 #[case('A', true)]
212 #[case('L', true)]
213 #[case('Z', true)]
214 #[case('0', true)]
215 #[case('3', true)]
216 #[case('9', true)]
217 #[case('-', true)]
218 #[case('_', false)]
219 #[case('.', false)]
220 fn test_is_ldh(#[case] test_char: char, #[case] expected: bool) {
221 // GIVEN in parameters
222
223 // WHEN
224 let actual = test_char.is_ldh();
225
226 // THEN
227 assert_eq!(actual, expected);
228 }
229
230 #[rstest]
231 #[case("foo", true)]
232 #[case("", false)]
233 #[case("foo-bar", true)]
234 #[case("foo bar", false)]
235 fn test_is_ldh_string(#[case] test_string: &str, #[case] expected: bool) {
236 // GIVEN in parameters
237
238 // WHEN
239 let actual = test_string.is_ldh_string();
240
241 // THEN
242 assert_eq!(actual, expected);
243 }
244
245 #[rstest]
246 #[case("foo", false)]
247 #[case("", false)]
248 #[case("foo-bar", false)]
249 #[case("foo bar", false)]
250 #[case(".", false)]
251 #[case(".foo.bar", false)]
252 #[case(".foo", true)]
253 fn test_is_tld(#[case] test_string: &str, #[case] expected: bool) {
254 // GIVEN in parameters
255
256 // WHEN
257 let actual = test_string.is_tld();
258
259 // THEN
260 assert_eq!(actual, expected);
261 }
262
263 #[rstest]
264 #[case(&[], false)]
265 #[case(&["foo"], true)]
266 #[case(&["foo",""], false)]
267 #[case(&["foo","bar"], true)]
268 #[case(&["foo","bar baz"], false)]
269 #[case(&[""], false)]
270 #[case(&[" "], false)]
271 fn test_is_ldh_string_list(#[case] test_list: &[&str], #[case] expected: bool) {
272 // GIVEN in parameters
273
274 // WHEN
275 let actual = test_list.is_ldh_string_list();
276
277 // THEN
278 assert_eq!(actual, expected);
279 }
280
281 #[rstest]
282 #[case("foo", true)]
283 #[case("", false)]
284 #[case(".", true)]
285 #[case("foo.bar", true)]
286 #[case("foo.bar.", true)]
287 fn test_is_ldh_domain_name(#[case] test_string: &str, #[case] expected: bool) {
288 // GIVEN in parameters
289
290 // WHEN
291 let actual = test_string.is_ldh_domain_name();
292
293 // THEN
294 assert_eq!(actual, expected);
295 }
296
297 #[rstest]
298 #[case("foo", true)]
299 #[case("", false)]
300 #[case(".", true)]
301 #[case("foo.bar", true)]
302 #[case("foè.bar", true)]
303 #[case("foo.bar.", true)]
304 #[case("fo_o.bar.", false)]
305 #[case("fo o.bar.", false)]
306 fn test_is_unicode_domain_name(#[case] test_string: &str, #[case] expected: bool) {
307 // GIVEN in parameters
308
309 // WHEN
310 let actual = test_string.is_unicode_domain_name();
311
312 // THEN
313 assert_eq!(actual, expected);
314 }
315
316 #[rstest]
317 #[case("foo", false)]
318 #[case("", false)]
319 #[case(".", false)]
320 #[case("foo.bar", true)]
321 #[case("foè.bar", false)]
322 #[case("foo.bar.", true)]
323 #[case("fo_o.bar.", false)]
324 #[case("fo o.bar.", false)]
325 #[case("bar.foo.bar", true)]
326 #[case("https://foo.bar", false)]
327 #[case("http://foo.bar", false)]
328 fn test_is_ldh_hostname(#[case] test_string: &str, #[case] expected: bool) {
329 // GIVEN in parameters
330
331 // WHEN
332 let actual = test_string.is_ldh_hostname();
333
334 // THEN
335 assert_eq!(actual, expected);
336 }
337
338 #[rstest]
339 #[case("| AS3333 RIPE-NCC-AS |", true)]
340 #[case("| RIPE NCC |", true)]
341 #[case("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++", true)]
342 #[case(
343 "====================================================================",
344 true
345 )]
346 #[case("--------", true)]
347 #[case("+++ === +++", true)]
348 #[case("normal text", false)]
349 #[case("", false)]
350 #[case(" ", false)]
351 #[case("Some text | in middle", false)]
352 #[case("Text with + plus", false)]
353 fn test_is_ascii_table_line(#[case] test_string: &str, #[case] expected: bool) {
354 // GIVEN in parameters
355
356 // WHEN
357 let actual = test_string.is_ascii_table_line();
358
359 // THEN
360 assert_eq!(actual, expected);
361 }
362 }