soar-dl 0.12.1

Downloader for soar package manager
Documentation
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
use fast_glob::glob_match;
use regex::Regex;

#[derive(Debug, Clone, Default)]
pub struct Filter {
    pub regexes: Vec<Regex>,
    pub globs: Vec<String>,
    pub include: Vec<String>,
    pub exclude: Vec<String>,
    pub case_sensitive: bool,
}

impl Filter {
    /// Determines whether a name satisfies this filter's combined criteria.
    ///
    /// The name must match every regex in `self.regexes`, match at least one glob in
    /// `self.globs`, satisfy all include keyword groups in `self.include`, and must
    /// not match any exclude keyword groups in `self.exclude`.
    ///
    /// # Returns
    ///
    /// `true` if the name matches all regexes, at least one glob, all include groups,
    /// and no exclude groups; `false` otherwise.
    ///
    /// # Examples
    ///
    /// ```
    /// use soar_dl::filter::Filter;
    ///
    /// let f = Filter {
    ///     regexes: Vec::new(),
    ///     globs: vec!["*".into()],
    ///     include: Vec::new(),
    ///     exclude: Vec::new(),
    ///     case_sensitive: true,
    /// };
    /// assert!(f.matches("anything"));
    /// ```
    pub fn matches(&self, name: &str) -> bool {
        let matches_regex =
            self.regexes.is_empty() || self.regexes.iter().all(|r| r.is_match(name));
        let matches_glob = self.globs.is_empty()
            || if self.case_sensitive {
                self.globs.iter().any(|g| glob_match(g, name))
            } else {
                self.globs
                    .iter()
                    .any(|g| glob_match(g.to_lowercase(), name.to_lowercase()))
            };
        let matches_include = self.matches_keywords(name, &self.include, true);
        let matches_exclude = self.matches_keywords(name, &self.exclude, false);

        matches_regex && matches_glob && matches_include && matches_exclude
    }

    /// Determines whether every keyword group in `keywords` satisfies the required presence or absence
    /// against `name` according to `must_match`.
    ///
    /// - If `keywords` is empty, returns `true`.
    /// - Splits each keyword string on commas, trims parts, and ignores empty parts.
    /// - Respects `case_sensitive`: comparisons use the original case when `true`, otherwise both
    ///   haystack and needles are lowercased.
    /// - For each keyword (a group of comma-separated alternatives), any one alternative matching
    ///   `name` counts as a match for that keyword.
    /// - If `must_match` is `true`, each keyword group must have at least one matching alternative.
    ///   If `must_match` is `false`, each keyword group must have no matching alternatives.
    ///
    /// # Examples
    ///
    /// ```
    /// use regex::Regex;
    /// use soar_dl::filter::Filter;
    ///
    /// let filter = Filter {
    ///     regexes: vec![],
    ///     globs: vec![],
    ///     include: vec!["foo,bar".to_string()],
    ///     exclude: vec![],
    ///     case_sensitive: false,
    /// };
    ///
    /// // "barbaz" contains "bar", one of the alternatives in the include group.
    /// assert!(filter.matches("barbaz"));
    /// ```
    fn matches_keywords(&self, name: &str, keywords: &[String], must_match: bool) -> bool {
        if keywords.is_empty() {
            return true;
        }

        let haystack = if self.case_sensitive {
            name.to_string()
        } else {
            name.to_lowercase()
        };

        keywords.iter().all(|kw| {
            let parts: Vec<_> = kw
                .split(',')
                .map(str::trim)
                .filter(|s| !s.is_empty())
                .collect();

            let any_match = parts.iter().any(|&part| {
                let needle = if self.case_sensitive {
                    part.to_string()
                } else {
                    part.to_lowercase()
                };
                haystack.contains(&needle)
            });

            if must_match {
                any_match
            } else {
                !any_match
            }
        })
    }
}

#[cfg(test)]
mod tests {
    use regex::Regex;

    use super::*;

    #[test]
    fn test_filter_default() {
        let filter = Filter::default();
        assert!(filter.regexes.is_empty());
        assert!(filter.globs.is_empty());
        assert!(filter.include.is_empty());
        assert!(filter.exclude.is_empty());
        assert!(!filter.case_sensitive);
    }

    #[test]
    fn test_matches_empty_filter() {
        let filter = Filter::default();
        // Empty filter should match everything
        assert!(filter.matches("anything"));
        assert!(filter.matches(""));
        assert!(filter.matches("test.tar.gz"));
    }

    #[test]
    fn test_matches_regex() {
        let filter = Filter {
            regexes: vec![Regex::new(r"\.tar\.gz$").unwrap()],
            globs: vec![],
            include: vec![],
            exclude: vec![],
            case_sensitive: true,
        };

        assert!(filter.matches("archive.tar.gz"));
        assert!(filter.matches("file-v1.0.tar.gz"));
        assert!(!filter.matches("archive.zip"));
        assert!(!filter.matches("file.tar"));
    }

    #[test]
    fn test_matches_multiple_regexes() {
        let filter = Filter {
            regexes: vec![Regex::new(r"^file").unwrap(), Regex::new(r"linux").unwrap()],
            globs: vec![],
            include: vec![],
            exclude: vec![],
            case_sensitive: true,
        };

        assert!(filter.matches("file-linux-x86_64"));
        assert!(!filter.matches("archive-linux-x86_64")); // doesn't start with "file"
        assert!(!filter.matches("file-windows-x86_64")); // doesn't contain "linux"
    }

    #[test]
    fn test_matches_glob_case_sensitive() {
        let filter = Filter {
            regexes: vec![],
            globs: vec!["*.tar.gz".to_string()],
            include: vec![],
            exclude: vec![],
            case_sensitive: true,
        };

        assert!(filter.matches("archive.tar.gz"));
        assert!(filter.matches("file.tar.gz"));
        assert!(!filter.matches("archive.TAR.GZ"));
        assert!(!filter.matches("archive.zip"));
    }

    #[test]
    fn test_matches_glob_case_insensitive() {
        let filter = Filter {
            regexes: vec![],
            globs: vec!["*.tar.gz".to_string()],
            include: vec![],
            exclude: vec![],
            case_sensitive: false,
        };

        assert!(filter.matches("archive.tar.gz"));
        assert!(filter.matches("archive.TAR.GZ"));
        assert!(filter.matches("file.Tar.Gz"));
        assert!(!filter.matches("archive.zip"));
    }

    #[test]
    fn test_matches_multiple_globs() {
        let filter = Filter {
            regexes: vec![],
            globs: vec!["*.tar.gz".to_string(), "*.zip".to_string()],
            include: vec![],
            exclude: vec![],
            case_sensitive: true,
        };

        assert!(filter.matches("archive.tar.gz"));
        assert!(filter.matches("file.zip"));
        assert!(!filter.matches("file.tar"));
        assert!(!filter.matches("file.7z"));
    }

    #[test]
    fn test_matches_include_single_keyword() {
        let filter = Filter {
            regexes: vec![],
            globs: vec![],
            include: vec!["linux".to_string()],
            exclude: vec![],
            case_sensitive: true,
        };

        assert!(filter.matches("file-linux-x86_64"));
        assert!(filter.matches("linux-binary"));
        assert!(!filter.matches("file-windows-x86_64"));
        assert!(!filter.matches("darwin-binary"));
    }

    #[test]
    fn test_matches_include_multiple_keywords() {
        let filter = Filter {
            regexes: vec![],
            globs: vec![],
            include: vec!["linux".to_string(), "x86_64".to_string()],
            exclude: vec![],
            case_sensitive: true,
        };

        assert!(filter.matches("file-linux-x86_64"));
        assert!(!filter.matches("file-linux-arm64")); // missing x86_64
        assert!(!filter.matches("file-darwin-x86_64")); // missing linux
    }

    #[test]
    fn test_matches_include_alternatives() {
        let filter = Filter {
            regexes: vec![],
            globs: vec![],
            include: vec!["linux,darwin".to_string()],
            exclude: vec![],
            case_sensitive: true,
        };

        assert!(filter.matches("file-linux-x86_64"));
        assert!(filter.matches("file-darwin-x86_64"));
        assert!(!filter.matches("file-windows-x86_64"));
    }

    #[test]
    fn test_matches_include_case_insensitive() {
        let filter = Filter {
            regexes: vec![],
            globs: vec![],
            include: vec!["Linux".to_string()],
            exclude: vec![],
            case_sensitive: false,
        };

        assert!(filter.matches("file-linux-x86_64"));
        assert!(filter.matches("file-LINUX-x86_64"));
        assert!(filter.matches("file-Linux-x86_64"));
    }

    #[test]
    fn test_matches_exclude_single_keyword() {
        let filter = Filter {
            regexes: vec![],
            globs: vec![],
            include: vec![],
            exclude: vec!["debug".to_string()],
            case_sensitive: true,
        };

        assert!(filter.matches("file-release"));
        assert!(!filter.matches("file-debug"));
        assert!(!filter.matches("debug-symbols"));
    }

    #[test]
    fn test_matches_exclude_multiple_keywords() {
        let filter = Filter {
            regexes: vec![],
            globs: vec![],
            include: vec![],
            exclude: vec!["debug".to_string(), "test".to_string()],
            case_sensitive: true,
        };

        assert!(filter.matches("file-release"));
        assert!(!filter.matches("file-debug"));
        assert!(!filter.matches("test-binary"));
        assert!(!filter.matches("debug-test-binary"));
    }

    #[test]
    fn test_matches_exclude_alternatives() {
        let filter = Filter {
            regexes: vec![],
            globs: vec![],
            include: vec![],
            exclude: vec!["debug,test".to_string()],
            case_sensitive: true,
        };

        assert!(filter.matches("file-release"));
        assert!(!filter.matches("file-debug"));
        assert!(!filter.matches("file-test"));
    }

    #[test]
    fn test_matches_combined_filters() {
        let filter = Filter {
            regexes: vec![Regex::new(r"^file").unwrap()],
            globs: vec!["*.tar.gz".to_string()],
            include: vec!["linux".to_string(), "x86_64".to_string()],
            exclude: vec!["debug".to_string()],
            case_sensitive: true,
        };

        assert!(filter.matches("file-linux-x86_64-v1.0.tar.gz"));
        assert!(!filter.matches("archive-linux-x86_64-v1.0.tar.gz")); // doesn't start with "file"
        assert!(!filter.matches("file-linux-x86_64-v1.0.zip")); // wrong extension
        assert!(!filter.matches("file-darwin-x86_64-v1.0.tar.gz")); // not linux
        assert!(!filter.matches("file-linux-arm64-v1.0.tar.gz")); // not x86_64
        assert!(!filter.matches("file-linux-x86_64-debug.tar.gz")); // contains "debug"
    }

    #[test]
    fn test_matches_keywords_empty() {
        let filter = Filter::default();
        assert!(filter.matches_keywords("anything", &[], true));
        assert!(filter.matches_keywords("anything", &[], false));
    }

    #[test]
    fn test_matches_keywords_whitespace_handling() {
        let filter = Filter {
            regexes: vec![],
            globs: vec![],
            include: vec!["  linux  ,  darwin  ".to_string()],
            exclude: vec![],
            case_sensitive: true,
        };

        assert!(filter.matches("file-linux-x86_64"));
        assert!(filter.matches("file-darwin-x86_64"));
    }

    #[test]
    fn test_matches_keywords_empty_alternatives() {
        let filter = Filter {
            regexes: vec![],
            globs: vec![],
            include: vec!["linux,,darwin".to_string()],
            exclude: vec![],
            case_sensitive: true,
        };

        // Empty alternatives should be filtered out
        assert!(filter.matches("file-linux-x86_64"));
        assert!(filter.matches("file-darwin-x86_64"));
    }

    #[test]
    fn test_glob_wildcard_patterns() {
        let filter = Filter {
            regexes: vec![],
            globs: vec!["file-*-x86_64".to_string()],
            include: vec![],
            exclude: vec![],
            case_sensitive: true,
        };

        assert!(filter.matches("file-linux-x86_64"));
        assert!(filter.matches("file-darwin-x86_64"));
        assert!(filter.matches("file-windows-x86_64"));
        assert!(!filter.matches("file-linux-arm64"));
    }

    #[test]
    fn test_glob_question_mark() {
        let filter = Filter {
            regexes: vec![],
            globs: vec!["file-?.tar.gz".to_string()],
            include: vec![],
            exclude: vec![],
            case_sensitive: true,
        };

        assert!(filter.matches("file-1.tar.gz"));
        assert!(filter.matches("file-a.tar.gz"));
        assert!(!filter.matches("file-10.tar.gz"));
        assert!(!filter.matches("file-.tar.gz"));
    }
}