scribe-patterns 0.5.1

Advanced pattern matching and search algorithms for Scribe
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
//! High-performance glob pattern matching implementation.
//!
//! This module provides efficient glob pattern matching using the `globset` crate
//! with caching, compilation optimization, and comprehensive pattern support.

use crate::utils::normalize_path;
use globset::{Glob, GlobBuilder, GlobSet, GlobSetBuilder};
use scribe_core::Result;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::Path;

/// High-performance glob pattern matcher with compilation caching
#[derive(Debug)]
pub struct GlobMatcher {
    patterns: Vec<GlobPattern>,
    compiled_set: Option<GlobSet>,
    options: GlobOptions,
    cache: HashMap<String, bool>,
    cache_hits: u64,
    cache_misses: u64,
}

/// Individual glob pattern with metadata
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GlobPattern {
    pub pattern: String,
    pub case_sensitive: bool,
    pub literal_separator: bool,
    pub backslash_escape: bool,
    pub require_literal_separator: bool,
    pub require_literal_leading_dot: bool,
}

/// Configuration options for glob matching
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GlobOptions {
    pub case_sensitive: bool,
    pub literal_separator: bool,
    pub backslash_escape: bool,
    pub require_literal_separator: bool,
    pub require_literal_leading_dot: bool,
    pub cache_enabled: bool,
    pub cache_size_limit: usize,
}

/// Result of a glob match operation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GlobMatchResult {
    pub matched: bool,
    pub pattern_index: Option<usize>,
    pub pattern: Option<String>,
    pub match_method: MatchMethod,
}

/// Method used for pattern matching
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum MatchMethod {
    Cached,
    Compiled,
    Individual,
    Literal,
}

impl Default for GlobOptions {
    fn default() -> Self {
        Self {
            case_sensitive: true,
            literal_separator: false,
            backslash_escape: false,
            require_literal_separator: false,
            require_literal_leading_dot: false,
            cache_enabled: true,
            cache_size_limit: 1000,
        }
    }
}

impl GlobPattern {
    /// Create a new glob pattern with default options
    pub fn new(pattern: &str) -> Result<Self> {
        Self::with_options(pattern, &GlobOptions::default())
    }

    /// Create a new glob pattern with specific options
    pub fn with_options(pattern: &str, options: &GlobOptions) -> Result<Self> {
        // Validate the pattern by trying to compile it
        let _glob = Glob::new(pattern)?;

        Ok(Self {
            pattern: pattern.to_string(),
            case_sensitive: options.case_sensitive,
            literal_separator: options.literal_separator,
            backslash_escape: options.backslash_escape,
            require_literal_separator: options.require_literal_separator,
            require_literal_leading_dot: options.require_literal_leading_dot,
        })
    }

    /// Check if this pattern matches a path
    pub fn matches<P: AsRef<Path>>(&self, path: P) -> Result<bool> {
        let normalized_path = normalize_path(path);
        let path_str = normalized_path.to_string_lossy();

        let mut glob_builder = globset::GlobBuilder::new(&self.pattern);
        glob_builder.case_insensitive(!self.case_sensitive);
        glob_builder.literal_separator(self.literal_separator);
        glob_builder.backslash_escape(self.backslash_escape);

        let glob = glob_builder.build()?;
        let matcher = glob.compile_matcher();
        Ok(matcher.is_match(path_str.as_ref()))
    }

    /// Check if this is a literal (non-glob) pattern
    pub fn is_literal(&self) -> bool {
        !self.pattern.contains('*')
            && !self.pattern.contains('?')
            && !self.pattern.contains('[')
            && !self.pattern.contains('{')
    }

    /// Get the pattern string
    pub fn as_str(&self) -> &str {
        &self.pattern
    }
}

impl GlobMatcher {
    /// Create a new glob matcher with default options
    pub fn new() -> Self {
        Self::with_options(GlobOptions::default())
    }

    /// Create a new glob matcher with specific options
    pub fn with_options(options: GlobOptions) -> Self {
        Self {
            patterns: Vec::new(),
            compiled_set: None,
            options,
            cache: HashMap::new(),
            cache_hits: 0,
            cache_misses: 0,
        }
    }

    /// Add a glob pattern to the matcher
    pub fn add_pattern(&mut self, pattern: &str) -> Result<()> {
        let glob_pattern = GlobPattern::with_options(pattern, &self.options)?;
        self.patterns.push(glob_pattern);

        // Invalidate compiled set - will be rebuilt on next match
        self.compiled_set = None;

        Ok(())
    }

    /// Add multiple glob patterns
    pub fn add_patterns<I, S>(&mut self, patterns: I) -> Result<()>
    where
        I: IntoIterator<Item = S>,
        S: AsRef<str>,
    {
        for pattern in patterns {
            self.add_pattern(pattern.as_ref())?;
        }
        Ok(())
    }

    /// Add patterns from comma-separated string
    pub fn add_patterns_csv(&mut self, csv: &str) -> Result<()> {
        let patterns = crate::utils::parse_csv_patterns(csv);
        for pattern in patterns {
            self.add_pattern(&pattern)?;
        }
        Ok(())
    }

    /// Remove all patterns
    pub fn clear(&mut self) {
        self.patterns.clear();
        self.compiled_set = None;
        self.cache.clear();
    }

    /// Check if any pattern matches the given path
    pub fn matches<P: AsRef<Path>>(&mut self, path: P) -> Result<bool> {
        let result = self.match_with_details(path)?;
        Ok(result.matched)
    }

    /// Get detailed match information
    pub fn match_with_details<P: AsRef<Path>>(&mut self, path: P) -> Result<GlobMatchResult> {
        let normalized_path = normalize_path(path);
        let path_str = normalized_path.to_string_lossy().to_string();

        // Check cache first if enabled
        if self.options.cache_enabled {
            if let Some(&cached_result) = self.cache.get(&path_str) {
                self.cache_hits += 1;
                return Ok(GlobMatchResult {
                    matched: cached_result,
                    pattern_index: None, // Cache doesn't store pattern index
                    pattern: None,
                    match_method: MatchMethod::Cached,
                });
            }
            self.cache_misses += 1;
        }

        if self.patterns.is_empty() {
            return Ok(GlobMatchResult {
                matched: false,
                pattern_index: None,
                pattern: None,
                match_method: MatchMethod::Individual,
            });
        }

        // Use compiled set for performance when we have multiple patterns
        let result = if self.patterns.len() > 1 {
            self.match_with_compiled_set(&normalized_path)?
        } else {
            self.match_with_individual_pattern(&normalized_path)?
        };

        // Cache the result if caching is enabled
        if self.options.cache_enabled {
            if self.cache.len() >= self.options.cache_size_limit {
                // Simple cache eviction - remove half the entries
                let keys_to_remove: Vec<String> = self
                    .cache
                    .keys()
                    .take(self.cache.len() / 2)
                    .cloned()
                    .collect();
                for key in keys_to_remove {
                    self.cache.remove(&key);
                }
            }
            self.cache.insert(path_str, result.matched);
        }

        Ok(result)
    }

    /// Match using compiled glob set (efficient for multiple patterns)
    fn match_with_compiled_set(&mut self, path: &Path) -> Result<GlobMatchResult> {
        if self.compiled_set.is_none() {
            self.compiled_set = Some(self.compile_patterns()?);
        }

        let compiled_set = self.compiled_set.as_ref().unwrap();
        let path_str = path.to_string_lossy();

        let matches: Vec<usize> = compiled_set.matches(path_str.as_ref());

        if matches.is_empty() {
            Ok(GlobMatchResult {
                matched: false,
                pattern_index: None,
                pattern: None,
                match_method: MatchMethod::Compiled,
            })
        } else {
            let pattern_index = matches[0];
            let pattern = self.patterns.get(pattern_index).map(|p| p.pattern.clone());

            Ok(GlobMatchResult {
                matched: true,
                pattern_index: Some(pattern_index),
                pattern,
                match_method: MatchMethod::Compiled,
            })
        }
    }

    /// Match using individual pattern (used for single patterns or fallback)
    fn match_with_individual_pattern(&self, path: &Path) -> Result<GlobMatchResult> {
        for (index, pattern) in self.patterns.iter().enumerate() {
            if pattern.matches(path)? {
                return Ok(GlobMatchResult {
                    matched: true,
                    pattern_index: Some(index),
                    pattern: Some(pattern.pattern.clone()),
                    match_method: if pattern.is_literal() {
                        MatchMethod::Literal
                    } else {
                        MatchMethod::Individual
                    },
                });
            }
        }

        Ok(GlobMatchResult {
            matched: false,
            pattern_index: None,
            pattern: None,
            match_method: MatchMethod::Individual,
        })
    }

    /// Compile all patterns into a GlobSet for efficient batch matching
    fn compile_patterns(&self) -> Result<GlobSet> {
        let mut builder = GlobSetBuilder::new();

        for pattern in &self.patterns {
            let mut glob_builder = GlobBuilder::new(&pattern.pattern);
            glob_builder.case_insensitive(!pattern.case_sensitive);
            glob_builder.literal_separator(pattern.literal_separator);
            glob_builder.backslash_escape(pattern.backslash_escape);

            let glob = glob_builder.build()?;
            builder.add(glob);
        }

        Ok(builder.build()?)
    }

    /// Get the number of patterns
    pub fn pattern_count(&self) -> usize {
        self.patterns.len()
    }

    /// Get all patterns
    pub fn patterns(&self) -> &[GlobPattern] {
        &self.patterns
    }

    /// Get cache statistics
    pub fn cache_stats(&self) -> (u64, u64, usize) {
        (self.cache_hits, self.cache_misses, self.cache.len())
    }

    /// Clear the cache
    pub fn clear_cache(&mut self) {
        self.cache.clear();
        self.cache_hits = 0;
        self.cache_misses = 0;
    }

    /// Check if patterns are compiled
    pub fn is_compiled(&self) -> bool {
        self.compiled_set.is_some()
    }

    /// Force recompilation of patterns
    pub fn recompile(&mut self) -> Result<()> {
        if !self.patterns.is_empty() {
            self.compiled_set = Some(self.compile_patterns()?);
        }
        Ok(())
    }

    /// Get cache hit ratio
    pub fn cache_hit_ratio(&self) -> f64 {
        let total = self.cache_hits + self.cache_misses;
        if total == 0 {
            0.0
        } else {
            self.cache_hits as f64 / total as f64
        }
    }

    /// Optimize patterns for better performance
    pub fn optimize(&mut self) {
        // Sort patterns by complexity (literal patterns first)
        self.patterns.sort_by_key(|p| !p.is_literal());

        // Invalidate compiled set to force recompilation with new order
        self.compiled_set = None;
    }

    /// Test all patterns against a path and return all matches
    pub fn match_all<P: AsRef<Path>>(&mut self, path: P) -> Result<Vec<usize>> {
        if self.compiled_set.is_none() && self.patterns.len() > 1 {
            self.compiled_set = Some(self.compile_patterns()?);
        }

        if let Some(ref compiled_set) = self.compiled_set {
            let path_str = path.as_ref().to_string_lossy();
            Ok(compiled_set.matches(path_str.as_ref()))
        } else {
            // Fallback to individual matching
            let mut matches = Vec::new();
            for (index, pattern) in self.patterns.iter().enumerate() {
                if pattern.matches(&path)? {
                    matches.push(index);
                }
            }
            Ok(matches)
        }
    }

    /// Check if matcher contains any patterns
    pub fn is_empty(&self) -> bool {
        self.patterns.is_empty()
    }

    /// Enable or disable caching
    pub fn set_cache_enabled(&mut self, enabled: bool) {
        self.options.cache_enabled = enabled;
        if !enabled {
            self.clear_cache();
        }
    }

    /// Set cache size limit
    pub fn set_cache_size_limit(&mut self, limit: usize) {
        self.options.cache_size_limit = limit;

        // Trim cache if it exceeds new limit
        if self.cache.len() > limit {
            let keys_to_remove: Vec<String> = self.cache.keys().skip(limit).cloned().collect();
            for key in keys_to_remove {
                self.cache.remove(&key);
            }
        }
    }
}

impl Default for GlobMatcher {
    fn default() -> Self {
        Self::new()
    }
}

/// Convenience functions for common glob operations
impl GlobMatcher {
    /// Create a matcher for specific file extensions
    pub fn for_extensions(extensions: &[&str]) -> Result<Self> {
        let mut matcher = Self::new();
        for ext in extensions {
            let pattern = crate::utils::extension_to_glob(ext);
            matcher.add_pattern(&pattern)?;
        }
        Ok(matcher)
    }

    /// Create a matcher for files in specific directories
    pub fn for_directories(directories: &[&str]) -> Result<Self> {
        let mut matcher = Self::new();
        for dir in directories {
            let pattern = format!("{}/**/*", dir.trim_end_matches('/'));
            matcher.add_pattern(&pattern)?;
        }
        Ok(matcher)
    }

    /// Create a case-insensitive matcher
    pub fn case_insensitive() -> Self {
        Self::with_options(GlobOptions {
            case_sensitive: false,
            ..Default::default()
        })
    }
}

// Note: From<globset::Error> for ScribeError is already implemented in scribe-core

#[cfg(test)]
mod tests {
    use super::*;
    // use std::path::PathBuf; // Not used in these tests

    #[test]
    fn test_glob_pattern_creation() {
        let pattern = GlobPattern::new("**/*.rs").unwrap();
        assert_eq!(pattern.pattern, "**/*.rs");
        assert!(pattern.case_sensitive);

        assert!(pattern.matches("src/lib.rs").unwrap());
        assert!(pattern.matches("tests/integration/test.rs").unwrap());
        assert!(!pattern.matches("src/lib.py").unwrap());
    }

    #[test]
    fn test_glob_pattern_literal_detection() {
        let literal = GlobPattern::new("src/lib.rs").unwrap();
        assert!(literal.is_literal());

        let glob = GlobPattern::new("src/**/*.rs").unwrap();
        assert!(!glob.is_literal());

        let question_mark = GlobPattern::new("src/lib?.rs").unwrap();
        assert!(!question_mark.is_literal());

        let bracket = GlobPattern::new("src/lib[123].rs").unwrap();
        assert!(!bracket.is_literal());

        let brace = GlobPattern::new("src/lib.{rs,py}").unwrap();
        assert!(!brace.is_literal());
    }

    #[test]
    fn test_case_insensitive_matching() {
        let options = GlobOptions {
            case_sensitive: false,
            ..Default::default()
        };

        let pattern = GlobPattern::with_options("**/*.RS", &options).unwrap();
        assert!(pattern.matches("src/lib.rs").unwrap());
        assert!(pattern.matches("src/LIB.RS").unwrap());
        assert!(pattern.matches("src/Lib.Rs").unwrap());
    }

    #[test]
    fn test_glob_matcher_single_pattern() {
        let mut matcher = GlobMatcher::new();
        matcher.add_pattern("**/*.rs").unwrap();

        assert!(matcher.matches("src/lib.rs").unwrap());
        assert!(matcher.matches("tests/test.rs").unwrap());
        assert!(!matcher.matches("src/lib.py").unwrap());
    }

    #[test]
    fn test_glob_matcher_multiple_patterns() {
        let mut matcher = GlobMatcher::new();
        matcher.add_pattern("**/*.rs").unwrap();
        matcher.add_pattern("**/*.py").unwrap();
        matcher.add_pattern("**/*.js").unwrap();

        assert!(matcher.matches("src/lib.rs").unwrap());
        assert!(matcher.matches("src/main.py").unwrap());
        assert!(matcher.matches("src/app.js").unwrap());
        assert!(!matcher.matches("src/data.json").unwrap());
    }

    #[test]
    fn test_glob_matcher_csv_patterns() {
        let mut matcher = GlobMatcher::new();
        matcher
            .add_patterns_csv("**/*.rs, **/*.py , **/*.js")
            .unwrap();

        assert!(matcher.matches("src/lib.rs").unwrap());
        assert!(matcher.matches("src/main.py").unwrap());
        assert!(matcher.matches("src/app.js").unwrap());
        assert!(!matcher.matches("src/data.json").unwrap());
        assert_eq!(matcher.pattern_count(), 3);
    }

    #[test]
    fn test_glob_matcher_detailed_results() {
        let mut matcher = GlobMatcher::new();
        matcher.add_pattern("**/*.rs").unwrap();
        matcher.add_pattern("**/*.py").unwrap();

        let result = matcher.match_with_details("src/lib.rs").unwrap();
        assert!(result.matched);
        assert_eq!(result.pattern_index, Some(0));
        assert_eq!(result.pattern, Some("**/*.rs".to_string()));

        let result = matcher.match_with_details("src/main.py").unwrap();
        assert!(result.matched);
        assert_eq!(result.pattern_index, Some(1));
        assert_eq!(result.pattern, Some("**/*.py".to_string()));

        let result = matcher.match_with_details("src/data.json").unwrap();
        assert!(!result.matched);
        assert_eq!(result.pattern_index, None);
    }

    #[test]
    fn test_glob_matcher_cache() {
        let mut matcher = GlobMatcher::with_options(GlobOptions {
            cache_enabled: true,
            cache_size_limit: 10,
            ..Default::default()
        });

        matcher.add_pattern("**/*.rs").unwrap();

        // First match - cache miss
        assert!(matcher.matches("src/lib.rs").unwrap());
        let (hits, misses, size) = matcher.cache_stats();
        assert_eq!(hits, 0);
        assert_eq!(misses, 1);
        assert_eq!(size, 1);

        // Second match - cache hit
        assert!(matcher.matches("src/lib.rs").unwrap());
        let (hits, misses, size) = matcher.cache_stats();
        assert_eq!(hits, 1);
        assert_eq!(misses, 1);
        assert_eq!(size, 1);

        // Cache hit ratio should be 0.5
        assert_eq!(matcher.cache_hit_ratio(), 0.5);
    }

    #[test]
    fn test_glob_matcher_cache_eviction() {
        let mut matcher = GlobMatcher::with_options(GlobOptions {
            cache_enabled: true,
            cache_size_limit: 2,
            ..Default::default()
        });

        matcher.add_pattern("**/*").unwrap();

        // Fill cache to limit
        matcher.matches("file1.rs").unwrap();
        matcher.matches("file2.py").unwrap();
        assert_eq!(matcher.cache_stats().2, 2);

        // Adding another should trigger eviction
        matcher.matches("file3.js").unwrap();
        assert_eq!(matcher.cache_stats().2, 2); // Should still be at limit
    }

    #[test]
    fn test_glob_matcher_optimization() {
        let mut matcher = GlobMatcher::new();
        matcher.add_pattern("**/*.rs").unwrap(); // Glob pattern
        matcher.add_pattern("exact/path.py").unwrap(); // Literal pattern
        matcher.add_pattern("src/**/*.js").unwrap(); // Glob pattern

        // Before optimization, order should be as added
        assert_eq!(matcher.patterns()[0].pattern, "**/*.rs");
        assert_eq!(matcher.patterns()[1].pattern, "exact/path.py");
        assert_eq!(matcher.patterns()[2].pattern, "src/**/*.js");

        matcher.optimize();

        // After optimization, literal patterns should come first
        assert_eq!(matcher.patterns()[0].pattern, "exact/path.py");
        assert!(matcher.patterns()[0].is_literal());
    }

    #[test]
    fn test_glob_matcher_match_all() {
        let mut matcher = GlobMatcher::new();
        matcher.add_pattern("**/*.rs").unwrap();
        matcher.add_pattern("src/**").unwrap();
        matcher.add_pattern("**/*lib*").unwrap();

        let matches = matcher.match_all("src/lib.rs").unwrap();
        assert_eq!(matches.len(), 3); // Should match all patterns
        assert!(matches.contains(&0)); // **/*.rs
        assert!(matches.contains(&1)); // src/**
        assert!(matches.contains(&2)); // **/*lib*

        let matches = matcher.match_all("tests/test.rs").unwrap();
        assert_eq!(matches.len(), 1); // Should only match **/*.rs
        assert!(matches.contains(&0));
    }

    #[test]
    fn test_glob_matcher_convenience_methods() {
        let mut matcher = GlobMatcher::for_extensions(&["rs", "py", "js"]).unwrap();
        assert!(matcher.matches("src/lib.rs").unwrap());
        assert!(matcher.matches("src/main.py").unwrap());
        assert!(matcher.matches("src/app.js").unwrap());
        assert!(!matcher.matches("src/data.json").unwrap());
        assert_eq!(matcher.pattern_count(), 3);

        let mut matcher = GlobMatcher::for_directories(&["src", "tests"]).unwrap();
        assert!(matcher.matches("src/lib.rs").unwrap());
        assert!(matcher.matches("tests/test.rs").unwrap());
        assert!(!matcher.matches("docs/readme.md").unwrap());
        assert_eq!(matcher.pattern_count(), 2);
    }

    #[test]
    fn test_glob_matcher_case_insensitive() {
        let mut matcher = GlobMatcher::case_insensitive();
        matcher.add_pattern("**/*.RS").unwrap();

        assert!(matcher.matches("src/lib.rs").unwrap());
        assert!(matcher.matches("src/LIB.RS").unwrap());
        assert!(matcher.matches("src/Lib.Rs").unwrap());
    }

    #[test]
    fn test_glob_matcher_empty() {
        let mut matcher = GlobMatcher::new();
        assert!(matcher.is_empty());
        assert!(!matcher.matches("any/path").unwrap());

        matcher.add_pattern("**/*.rs").unwrap();
        assert!(!matcher.is_empty());

        matcher.clear();
        assert!(matcher.is_empty());
        assert!(!matcher.matches("any/path.rs").unwrap());
    }

    #[test]
    fn test_glob_matcher_compilation() {
        let mut matcher = GlobMatcher::new();
        assert!(!matcher.is_compiled());

        matcher.add_pattern("**/*.rs").unwrap();
        matcher.add_pattern("**/*.py").unwrap();

        // Should still not be compiled until first match
        assert!(!matcher.is_compiled());

        // First match should trigger compilation
        matcher.matches("src/lib.rs").unwrap();
        assert!(matcher.is_compiled());

        // Adding pattern should invalidate compilation
        matcher.add_pattern("**/*.js").unwrap();
        assert!(!matcher.is_compiled());

        // Manual recompilation
        matcher.recompile().unwrap();
        assert!(matcher.is_compiled());
    }

    #[test]
    fn test_complex_glob_patterns() {
        let mut matcher = GlobMatcher::new();

        // Brace expansion
        matcher.add_pattern("**/*.{rs,py,js}").unwrap();
        assert!(matcher.matches("src/lib.rs").unwrap());
        assert!(matcher.matches("src/main.py").unwrap());
        assert!(matcher.matches("src/app.js").unwrap());
        assert!(!matcher.matches("src/data.json").unwrap());

        matcher.clear();

        // Character classes
        matcher.add_pattern("test[0-9].rs").unwrap();
        assert!(matcher.matches("test1.rs").unwrap());
        assert!(matcher.matches("test9.rs").unwrap());
        assert!(!matcher.matches("testA.rs").unwrap());

        matcher.clear();

        // Question mark
        matcher.add_pattern("test?.rs").unwrap();
        assert!(matcher.matches("test1.rs").unwrap());
        assert!(matcher.matches("testA.rs").unwrap());
        assert!(!matcher.matches("test12.rs").unwrap());
    }

    #[test]
    fn test_path_normalization_in_matching() {
        let mut matcher = GlobMatcher::new();
        matcher.add_pattern("src/**/*.rs").unwrap();

        // Test various path formats
        assert!(matcher.matches("src/lib.rs").unwrap());
        assert!(matcher.matches("src\\lib.rs").unwrap()); // Windows-style
        assert!(matcher.matches("src/subdir/lib.rs").unwrap());
        assert!(matcher.matches("src\\subdir\\lib.rs").unwrap()); // Windows-style
    }
}