|
| 1 | +//! Glob matching for environment-variable **names** (flat strings, never paths). |
| 2 | +//! |
| 3 | +//! Backed by `globset` with path-separator handling disabled, so `*`, `?`, |
| 4 | +//! `[...]`, and `{a,b}` behave as plain-string wildcards. Matching is |
| 5 | +//! case-sensitive on Unix and case-insensitive on Windows, mirroring how |
| 6 | +//! environment variables are looked up on each platform. |
| 7 | +//! |
| 8 | +//! [`EnvGlobSet`] supports negation (Turbo-style): a `!`-prefixed pattern |
| 9 | +//! *excludes* names, and a name matches the set when it matches an include |
| 10 | +//! pattern and no exclude pattern. A leading `\!` escapes the `!` to match a |
| 11 | +//! literal name that starts with `!`. [`EnvGlob`] matches a single pattern |
| 12 | +//! literally — `!` is an ordinary character there (no negation), since a lone |
| 13 | +//! exclude has nothing to subtract from. |
| 14 | +
|
| 15 | +use globset::{Glob, GlobBuilder, GlobMatcher, GlobSet, GlobSetBuilder}; |
| 16 | + |
| 17 | +/// Error compiling an environment-variable name pattern. |
| 18 | +#[derive(Debug, thiserror::Error)] |
| 19 | +#[error(transparent)] |
| 20 | +pub struct EnvGlobError(#[from] globset::Error); |
| 21 | + |
| 22 | +/// Compiles `pattern` into a `globset::Glob` configured for env-name matching: |
| 23 | +/// separators are not special, and case follows the platform's env semantics. |
| 24 | +fn build(pattern: &str) -> Result<Glob, globset::Error> { |
| 25 | + GlobBuilder::new(pattern) |
| 26 | + // Env names contain no path separators, so disabling separator handling |
| 27 | + // makes `*`/`?` match any character — a pure string match. |
| 28 | + .literal_separator(false) |
| 29 | + // Env lookups are case-insensitive on Windows, case-sensitive elsewhere. |
| 30 | + .case_insensitive(cfg!(windows)) |
| 31 | + .build() |
| 32 | +} |
| 33 | + |
| 34 | +/// Matches a single environment-variable name against one glob pattern. |
| 35 | +#[derive(Debug, Clone)] |
| 36 | +pub struct EnvGlob { |
| 37 | + matcher: GlobMatcher, |
| 38 | +} |
| 39 | + |
| 40 | +impl EnvGlob { |
| 41 | + /// Compiles `pattern` into an env-name matcher. |
| 42 | + /// |
| 43 | + /// # Errors |
| 44 | + /// Returns an error if `pattern` is not a valid glob. |
| 45 | + pub fn new(pattern: &str) -> Result<Self, EnvGlobError> { |
| 46 | + Ok(Self { matcher: build(pattern)?.compile_matcher() }) |
| 47 | + } |
| 48 | + |
| 49 | + /// Returns whether `name` matches the pattern. |
| 50 | + #[must_use] |
| 51 | + pub fn is_match(&self, name: &str) -> bool { |
| 52 | + self.matcher.is_match(name) |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +/// Matches an environment-variable name against a **set** of glob patterns, |
| 57 | +/// with negation. |
| 58 | +/// |
| 59 | +/// Patterns are split into includes and excludes: |
| 60 | +/// - `!FOO` is an **exclude** pattern. |
| 61 | +/// - `\!FOO` is an **include** pattern matching the literal name `!FOO`. |
| 62 | +/// - any other pattern is an **include**. |
| 63 | +/// |
| 64 | +/// A name matches when it matches some include pattern and no exclude pattern. |
| 65 | +/// A set with no include patterns matches nothing (an exclude has nothing to |
| 66 | +/// subtract from), so an empty set — or a set of only excludes — never matches. |
| 67 | +#[derive(Debug, Clone)] |
| 68 | +pub struct EnvGlobSet { |
| 69 | + include: GlobSet, |
| 70 | + exclude: GlobSet, |
| 71 | +} |
| 72 | + |
| 73 | +impl EnvGlobSet { |
| 74 | + /// Compiles `patterns` into a combined env-name matcher. |
| 75 | + /// |
| 76 | + /// # Errors |
| 77 | + /// Returns an error if any pattern is not a valid glob. |
| 78 | + pub fn new<I, S>(patterns: I) -> Result<Self, EnvGlobError> |
| 79 | + where |
| 80 | + I: IntoIterator<Item = S>, |
| 81 | + S: AsRef<str>, |
| 82 | + { |
| 83 | + let mut include = GlobSetBuilder::new(); |
| 84 | + let mut exclude = GlobSetBuilder::new(); |
| 85 | + for pattern in patterns { |
| 86 | + let pattern = pattern.as_ref(); |
| 87 | + if let Some(rest) = pattern.strip_prefix('!') { |
| 88 | + exclude.add(build(rest)?); |
| 89 | + } else if pattern.starts_with("\\!") { |
| 90 | + // Escaped: drop the leading backslash, keep the literal `!FOO`. |
| 91 | + include.add(build(&pattern[1..])?); |
| 92 | + } else { |
| 93 | + include.add(build(pattern)?); |
| 94 | + } |
| 95 | + } |
| 96 | + Ok(Self { include: include.build()?, exclude: exclude.build()? }) |
| 97 | + } |
| 98 | + |
| 99 | + /// Returns whether `name` matches an include pattern and no exclude pattern. |
| 100 | + #[must_use] |
| 101 | + pub fn is_match(&self, name: &str) -> bool { |
| 102 | + self.include.is_match(name) && !self.exclude.is_match(name) |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +#[cfg(test)] |
| 107 | +mod tests { |
| 108 | + use super::{EnvGlob, EnvGlobSet}; |
| 109 | + |
| 110 | + #[test] |
| 111 | + fn matches_star_prefix_and_suffix() { |
| 112 | + let g = EnvGlob::new("VITE_*").unwrap(); |
| 113 | + assert!(g.is_match("VITE_FOO")); |
| 114 | + assert!(g.is_match("VITE_")); // `*` matches the empty string |
| 115 | + assert!(!g.is_match("MYVITE_FOO")); |
| 116 | + |
| 117 | + let g = EnvGlob::new("*_KEY").unwrap(); |
| 118 | + assert!(g.is_match("MY_KEY")); |
| 119 | + assert!(!g.is_match("MY_KEYS")); |
| 120 | + |
| 121 | + let g = EnvGlob::new("*_CREDENTIAL*").unwrap(); |
| 122 | + assert!(g.is_match("AWS_CREDENTIALS")); |
| 123 | + assert!(g.is_match("X_CREDENTIAL_Y")); |
| 124 | + } |
| 125 | + |
| 126 | + #[test] |
| 127 | + fn question_mark_matches_exactly_one_char() { |
| 128 | + let g = EnvGlob::new("APP?_*").unwrap(); |
| 129 | + assert!(g.is_match("APP1_TOKEN")); |
| 130 | + assert!(g.is_match("APP2_NAME")); |
| 131 | + // `?` requires exactly one character, so `APP_X` (nothing before `_`) does not match. |
| 132 | + assert!(!g.is_match("APP_X")); |
| 133 | + } |
| 134 | + |
| 135 | + #[test] |
| 136 | + fn brace_alternation_is_supported() { |
| 137 | + let g = EnvGlob::new("{VITE,NEXT}_*").unwrap(); |
| 138 | + assert!(g.is_match("VITE_FOO")); |
| 139 | + assert!(g.is_match("NEXT_BAR")); |
| 140 | + assert!(!g.is_match("NUXT_BAR")); |
| 141 | + } |
| 142 | + |
| 143 | + #[test] |
| 144 | + fn dot_and_separators_are_literal_not_path_special() { |
| 145 | + // Env names are flat strings: `*` spans `.` and `/` (no path semantics), |
| 146 | + // and a literal `.` in the pattern matches a literal `.`. |
| 147 | + assert!(EnvGlob::new("A*").unwrap().is_match("A.B")); |
| 148 | + assert!(EnvGlob::new("A*").unwrap().is_match("A/B")); |
| 149 | + assert!(EnvGlob::new("*.local").unwrap().is_match("APP.local")); |
| 150 | + assert!(!EnvGlob::new("*.local").unwrap().is_match("APPXlocal")); |
| 151 | + } |
| 152 | + |
| 153 | + #[test] |
| 154 | + fn single_glob_bang_is_a_literal_character() { |
| 155 | + // A single `EnvGlob` has no negation: `!FOO` matches the literal name |
| 156 | + // `!FOO`, not `FOO`. |
| 157 | + let g = EnvGlob::new("!FOO").unwrap(); |
| 158 | + assert!(g.is_match("!FOO")); |
| 159 | + assert!(!g.is_match("FOO")); |
| 160 | + } |
| 161 | + |
| 162 | + #[test] |
| 163 | + fn non_match_default_is_false() { |
| 164 | + assert!(!EnvGlob::new("VITE_*").unwrap().is_match("PATH")); |
| 165 | + } |
| 166 | + |
| 167 | + #[test] |
| 168 | + fn set_matches_any_pattern() { |
| 169 | + let set = EnvGlobSet::new(["VITE_*", "*_KEY", "APP?_*"]).unwrap(); |
| 170 | + assert!(set.is_match("VITE_FOO")); |
| 171 | + assert!(set.is_match("MY_KEY")); |
| 172 | + assert!(set.is_match("APP1_TOKEN")); |
| 173 | + assert!(!set.is_match("PATH")); |
| 174 | + assert!(!set.is_match("APP_X")); |
| 175 | + } |
| 176 | + |
| 177 | + #[test] |
| 178 | + fn empty_set_matches_nothing() { |
| 179 | + let set = EnvGlobSet::new(std::iter::empty::<&str>()).unwrap(); |
| 180 | + assert!(!set.is_match("VITE_FOO")); |
| 181 | + } |
| 182 | + |
| 183 | + #[test] |
| 184 | + fn set_negation_excludes_matching_names() { |
| 185 | + // `!VITE_SECRET` excludes that name from the `VITE_*` include set. |
| 186 | + let set = EnvGlobSet::new(["VITE_*", "!VITE_SECRET"]).unwrap(); |
| 187 | + assert!(set.is_match("VITE_FOO")); |
| 188 | + assert!(set.is_match("VITE_BAR")); |
| 189 | + assert!(!set.is_match("VITE_SECRET")); |
| 190 | + assert!(!set.is_match("PATH")); |
| 191 | + |
| 192 | + // An exclude glob can itself be a wildcard. |
| 193 | + let set = EnvGlobSet::new(["*", "!*_SECRET"]).unwrap(); |
| 194 | + assert!(set.is_match("VITE_FOO")); |
| 195 | + assert!(!set.is_match("API_SECRET")); |
| 196 | + } |
| 197 | + |
| 198 | + #[test] |
| 199 | + fn set_only_excludes_matches_nothing() { |
| 200 | + // With no include patterns there is nothing to subtract from. |
| 201 | + let set = EnvGlobSet::new(["!FOO"]).unwrap(); |
| 202 | + assert!(!set.is_match("FOO")); |
| 203 | + assert!(!set.is_match("BAR")); |
| 204 | + } |
| 205 | + |
| 206 | + #[test] |
| 207 | + fn set_escaped_bang_is_a_literal_include() { |
| 208 | + // `\!FOO` includes the literal name `!FOO` (not a negation). |
| 209 | + let set = EnvGlobSet::new(["\\!FOO"]).unwrap(); |
| 210 | + assert!(set.is_match("!FOO")); |
| 211 | + assert!(!set.is_match("FOO")); |
| 212 | + } |
| 213 | + |
| 214 | + #[test] |
| 215 | + #[cfg(not(windows))] |
| 216 | + fn unix_matching_is_case_sensitive() { |
| 217 | + let g = EnvGlob::new("VITE_*").unwrap(); |
| 218 | + assert!(g.is_match("VITE_FOO")); |
| 219 | + assert!(!g.is_match("vite_foo")); |
| 220 | + let set = EnvGlobSet::new(["VITE_*"]).unwrap(); |
| 221 | + assert!(!set.is_match("vite_foo")); |
| 222 | + } |
| 223 | + |
| 224 | + #[test] |
| 225 | + #[cfg(windows)] |
| 226 | + fn windows_matching_is_case_insensitive() { |
| 227 | + let g = EnvGlob::new("VITE_*").unwrap(); |
| 228 | + assert!(g.is_match("VITE_FOO")); |
| 229 | + assert!(g.is_match("vite_foo")); |
| 230 | + let set = EnvGlobSet::new(["VITE_*"]).unwrap(); |
| 231 | + assert!(set.is_match("vite_foo")); |
| 232 | + } |
| 233 | +} |
0 commit comments