sub_strs/glob_set.rs
1/*
2==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--
3
4sub-strs
5
6Copyright (C) 2019-2023, 2026 Anonymous
7
8There are several releases over multiple years,
9they are listed as ranges, such as: "2019-2023".
10
11This program is free software: you can redistribute it and/or modify
12it under the terms of the GNU Lesser General Public License as published by
13the Free Software Foundation, either version 3 of the License, or
14(at your option) any later version.
15
16This program is distributed in the hope that it will be useful,
17but WITHOUT ANY WARRANTY; without even the implied warranty of
18MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19GNU Lesser General Public License for more details.
20
21You should have received a copy of the GNU Lesser General Public License
22along with this program. If not, see <https://www.gnu.org/licenses/>.
23
24::--::--::--::--::--::--::--::--::--::--::--::--::--::--::--::--
25*/
26
27//! # Glob set
28
29use {
30 alloc::borrow::Cow,
31 crate::Glob,
32};
33
34#[cfg(not(feature="std"))]
35use alloc::collections::BTreeSet as Set;
36
37#[cfg(feature="std")]
38use std::collections::HashSet as Set;
39
40/// # Glob set
41///
42/// This struct contains a set of [`Glob`][struct:Glob]'s.
43///
44/// [struct:Glob]: struct.Glob.html
45#[derive(Debug, Clone, Eq, PartialEq)]
46pub struct GlobSet<'a> {
47 globs: Set<Glob<'a>>,
48}
49
50impl<'a> GlobSet<'a> {
51
52 /// # Makes new instance from an iterator of [`Cow<'a, str>`][r::Cow]
53 ///
54 /// ## Notes
55 ///
56 /// - Empty strings will be ignored.
57 /// - If the iterator contains no globs, `None` is returned.
58 ///
59 /// ## Examples
60 ///
61 /// ```
62 /// use sub_strs::GlobSet;
63 ///
64 /// let glob_set = GlobSet::from_iter("*.rs|*.md".split('|')).unwrap();
65 /// assert!(glob_set.any("this.rs"));
66 /// assert!(glob_set.any("that.md"));
67 /// assert!(glob_set.any("not-this") == false);
68 /// ```
69 ///
70 /// [r::Cow]: https://doc.rust-lang.org/std/borrow/enum.Cow.html
71 pub fn from_iter<I0, I1>(iter: I0) -> Option<Self> where I0: IntoIterator<Item=I1>, I1: Into<Cow<'a, str>> {
72 let globs = iter.into_iter().filter_map(|s| {
73 let s = s.into();
74 (s.is_empty() == false).then(|| Glob::from(s))
75 }).collect::<Set<_>>();
76 (globs.is_empty() == false).then_some(Self { globs })
77 }
78
79 /// # Merges an iterator of [`GlobSet<'a>`][::GlobSet] into one
80 ///
81 /// ## Notes
82 ///
83 /// - If there are no inner [`Glob`][::Glob]'s, `None` is returned.
84 /// - For `#![no_std]` users: this function takes little effort to remove duplicates. You should help with that yourself (by not providing
85 /// duplicates).
86 ///
87 /// ## Examples
88 ///
89 /// ```
90 /// use sub_strs::GlobSet;
91 ///
92 /// let program_args = &["*.svg,*.OGG", "*.md"];
93 /// let glob_set = GlobSet::merge(
94 /// program_args.iter().filter_map(|s|
95 /// GlobSet::from_iter(s.to_lowercase().split(',').map(|s| s.to_string()))
96 /// )
97 /// );
98 /// assert!(glob_set.map(|g| g.any("some.ogg")).unwrap_or(false));
99 /// ```
100 ///
101 /// [::Glob]: struct.Glob.html
102 /// [::GlobSet]: struct.GlobSet.html
103 pub fn merge<I>(iter: I) -> Option<Self> where I: IntoIterator<Item=Self> {
104 let result = iter.into_iter().fold(Self { globs: Set::new() }, |mut result, item| {
105 result.globs.extend(item.globs);
106 result
107 });
108 (result.globs.is_empty() == false).then_some(result)
109 }
110
111 /// # Checks if _any_ glob inside matches the input string
112 pub fn any<S>(&self, s: S) -> bool where S: AsRef<str> {
113 self.globs.iter().any(|g| g.matches(&s))
114 }
115
116 /// Returns `true` if self has no elements in common with other. This is equivalent to checking for an empty intersection.
117 #[inline]
118 pub fn is_disjoint(&self, other: &Self) -> bool {
119 self.globs.is_disjoint(&other.globs)
120 }
121
122 /// Returns `true` if the set is a subset of another, i.e., other contains at least all the elements in self.
123 #[inline]
124 pub fn is_subset(&self, other: &Self) -> bool {
125 self.globs.is_subset(&other.globs)
126 }
127
128 /// Returns `true` if the set is a superset of another, i.e., self contains at least all the elements in other.
129 #[inline]
130 pub fn is_superset(&self, other: &Self) -> bool {
131 self.globs.is_superset(&other.globs)
132 }
133
134}