kerf 0.1.2

Simple tokio-based trace event collector
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
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
#![allow(clippy::mutable_key_type)]

use regex::Regex;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::{LazyLock, Mutex};
use tracing::Level as TracingLevel;

use crate::Event;

// Global regex cache to avoid recompilation
static REGEX_CACHE: LazyLock<Mutex<HashMap<String, Option<Regex>>>> =
    LazyLock::new(|| Mutex::new(HashMap::new()));

// Optimized pattern matcher enum
#[derive(Debug, Clone)]
pub enum PatternMatcher {
    Exact(String),
    Wildcard,         // matches "*"
    Prefix(String),   // matches "prefix*"
    Suffix(String),   // matches "*suffix"
    Contains(String), // matches "*substring*"
    Regex(Regex),
}

impl PatternMatcher {
    pub fn new(pattern: &str) -> Self {
        match pattern {
            "*" => Self::Wildcard,
            p if p.starts_with('*')
                && p.ends_with('*')
                && p.len() > 2
                && !p[1..p.len() - 1].contains('*') =>
            {
                Self::Contains(p[1..p.len() - 1].to_string())
            }
            p if p.starts_with('*') && p.len() > 1 && !p[1..].contains('*') => {
                Self::Suffix(p[1..].to_string())
            }
            p if p.ends_with('*') && p.len() > 1 && !p[..p.len() - 1].contains('*') => {
                Self::Prefix(p[..p.len() - 1].to_string())
            }
            p if !p.contains('*') => Self::Exact(p.to_string()),
            p => {
                // Complex pattern, use cached regex
                let regex_pattern = p.replace("*", ".*");
                let full_pattern = format!("^{regex_pattern}$");

                let mut cache = REGEX_CACHE.lock().unwrap();
                if let Some(cached_regex) = cache.get(&full_pattern) {
                    if let Some(regex) = cached_regex {
                        Self::Regex(regex.clone())
                    } else {
                        // Previously failed to compile, return exact match as fallback
                        Self::Exact(p.to_string())
                    }
                } else {
                    // Not in cache, try to compile
                    match Regex::new(&full_pattern) {
                        Ok(regex) => {
                            let result = Self::Regex(regex.clone());
                            cache.insert(full_pattern, Some(regex));
                            result
                        }
                        Err(_) => {
                            // Failed to compile, cache the failure and use exact match
                            cache.insert(full_pattern, None);
                            Self::Exact(p.to_string())
                        }
                    }
                }
            }
        }
    }

    pub fn matches(&self, value: &str) -> bool {
        match self {
            Self::Exact(pattern) => value == pattern,
            Self::Wildcard => true,
            Self::Prefix(prefix) => value.starts_with(prefix),
            Self::Suffix(suffix) => value.ends_with(suffix),
            Self::Contains(substring) => value.contains(substring),
            Self::Regex(regex) => regex.is_match(value),
        }
    }
}

impl PartialEq for PatternMatcher {
    fn eq(&self, other: &Self) -> bool {
        match (self, other) {
            (Self::Exact(a), Self::Exact(b)) => a == b,
            (Self::Wildcard, Self::Wildcard) => true,
            (Self::Prefix(a), Self::Prefix(b)) => a == b,
            (Self::Suffix(a), Self::Suffix(b)) => a == b,
            (Self::Contains(a), Self::Contains(b)) => a == b,
            (Self::Regex(a), Self::Regex(b)) => a.as_str() == b.as_str(),
            _ => false,
        }
    }
}

impl std::hash::Hash for PatternMatcher {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        match self {
            Self::Exact(s) => {
                0u8.hash(state);
                s.hash(state);
            }
            Self::Wildcard => 1u8.hash(state),
            Self::Prefix(s) => {
                2u8.hash(state);
                s.hash(state);
            }
            Self::Suffix(s) => {
                3u8.hash(state);
                s.hash(state);
            }
            Self::Contains(s) => {
                4u8.hash(state);
                s.hash(state);
            }
            Self::Regex(r) => {
                5u8.hash(state);
                r.as_str().hash(state);
            }
        }
    }
}

// Backward compatibility function
pub fn matches(pattern: &str, value: &str) -> bool {
    PatternMatcher::new(pattern).matches(value)
}

// Define TraceLevel for serialization
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Level(pub TracingLevel);

impl Level {
    /// The "error" level.
    ///
    /// Designates very serious errors.
    pub const ERROR: Level = Level(TracingLevel::ERROR);
    /// The "warn" level.
    ///
    /// Designates hazardous situations.
    pub const WARN: Level = Level(TracingLevel::WARN);
    /// The "info" level.
    ///
    /// Designates useful information.
    pub const INFO: Level = Level(TracingLevel::INFO);
    /// The "debug" level.
    ///
    /// Designates lower priority information.
    pub const DEBUG: Level = Level(TracingLevel::DEBUG);
    /// The "trace" level.
    ///
    /// Designates very low priority, often extremely verbose, information.
    pub const TRACE: Level = Level(TracingLevel::TRACE);
    pub fn is_trace(&self) -> bool {
        self.0 == TracingLevel::TRACE
    }
    pub fn is_error(&self) -> bool {
        self.0 == TracingLevel::ERROR
    }
    pub fn is_warn(&self) -> bool {
        self.0 == TracingLevel::WARN
    }
    pub fn is_info(&self) -> bool {
        self.0 == TracingLevel::INFO
    }
    pub fn is_debug(&self) -> bool {
        self.0 == TracingLevel::DEBUG
    }
}

impl Serialize for Level {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        let s = match self.0 {
            TracingLevel::ERROR => "ERROR",
            TracingLevel::WARN => "WARN",
            TracingLevel::INFO => "INFO",
            TracingLevel::DEBUG => "DEBUG",
            TracingLevel::TRACE => "TRACE",
        };
        serializer.serialize_str(s)
    }
}

impl<'de> Deserialize<'de> for Level {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let s = String::deserialize(deserializer)?;
        let level = match s.to_uppercase().as_str() {
            "ERROR" => TracingLevel::ERROR,
            "WARN" => TracingLevel::WARN,
            "INFO" => TracingLevel::INFO,
            "DEBUG" => TracingLevel::DEBUG,
            "TRACE" => TracingLevel::TRACE,
            _ => {
                return Err(serde::de::Error::custom(format!(
                    "invalid level filter: {s}"
                )));
            }
        };
        Ok(Level(level))
    }
}

impl std::hash::Hash for Level {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        // Using the integer representation of the filter
        let value = match self.0 {
            TracingLevel::ERROR => 1,
            TracingLevel::WARN => 2,
            TracingLevel::INFO => 3,
            TracingLevel::DEBUG => 4,
            TracingLevel::TRACE => 5,
        };
        value.hash(state);
    }
}

impl Eq for Level {}

impl std::fmt::Display for Level {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self.0 {
            TracingLevel::ERROR => write!(f, "ERROR"),
            TracingLevel::WARN => write!(f, "WARN"),
            TracingLevel::INFO => write!(f, "INFO"),
            TracingLevel::DEBUG => write!(f, "DEBUG"),
            TracingLevel::TRACE => write!(f, "TRACE"),
        }
    }
}

impl From<TracingLevel> for Level {
    fn from(filter: TracingLevel) -> Self {
        Level(filter)
    }
}

impl From<Level> for TracingLevel {
    fn from(level: Level) -> Self {
        level.0
    }
}

#[derive(Debug, Clone)]
pub struct Match {
    pub level: Level,
    pub include: bool,
    pub module_patterns: Vec<String>,
    pub file_patterns: Vec<String>,
    pub span_patterns: Vec<String>,
    pub target_patterns: Vec<String>,
    // Precompiled matchers for performance
    module_matchers: Vec<PatternMatcher>,
    file_matchers: Vec<PatternMatcher>,
    span_matchers: Vec<PatternMatcher>,
    target_matchers: Vec<PatternMatcher>,
}

// Separate struct for serialization that doesn't include compiled patterns
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SerializableMatch {
    pub level: Level,
    pub include: bool,
    pub module_patterns: Vec<String>,
    pub file_patterns: Vec<String>,
    pub span_patterns: Vec<String>,
    pub target_patterns: Vec<String>,
}

impl Serialize for Match {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        let serializable = SerializableMatch {
            level: self.level,
            include: self.include,
            module_patterns: self.module_patterns.clone(),
            file_patterns: self.file_patterns.clone(),
            span_patterns: self.span_patterns.clone(),
            target_patterns: self.target_patterns.clone(),
        };
        serializable.serialize(serializer)
    }
}

impl<'de> Deserialize<'de> for Match {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let serializable = SerializableMatch::deserialize(deserializer)?;
        Ok(Match {
            level: serializable.level,
            include: serializable.include,
            module_patterns: serializable.module_patterns.clone(),
            file_patterns: serializable.file_patterns.clone(),
            span_patterns: serializable.span_patterns.clone(),
            target_patterns: serializable.target_patterns.clone(),
            module_matchers: serializable
                .module_patterns
                .iter()
                .map(|p| PatternMatcher::new(p))
                .collect(),
            file_matchers: serializable
                .file_patterns
                .iter()
                .map(|p| PatternMatcher::new(p))
                .collect(),
            span_matchers: serializable
                .span_patterns
                .iter()
                .map(|p| PatternMatcher::new(p))
                .collect(),
            target_matchers: serializable
                .target_patterns
                .iter()
                .map(|p| PatternMatcher::new(p))
                .collect(),
        })
    }
}

impl std::hash::Hash for Match {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        self.include.hash(state);
        self.level.hash(state);
        self.module_patterns.hash(state);
        self.file_patterns.hash(state);
        self.span_patterns.hash(state);
        self.target_patterns.hash(state);
        // Note: We don't hash the compiled matchers since they're derived from the patterns
    }
}

impl Default for Match {
    fn default() -> Self {
        let module_patterns = vec!["*".to_string()];
        Self {
            level: Level(TracingLevel::DEBUG),
            include: true,
            module_matchers: module_patterns
                .iter()
                .map(|p| PatternMatcher::new(p))
                .collect(),
            file_matchers: vec![],
            span_matchers: vec![],
            target_matchers: vec![],
            module_patterns,
            file_patterns: vec![],
            span_patterns: vec![],
            target_patterns: vec![],
        }
    }
}

impl Eq for Match {}

impl PartialEq for Match {
    fn eq(&self, other: &Self) -> bool {
        self.level == other.level
            && self.include == other.include
            && self.module_patterns == other.module_patterns
            && self.file_patterns == other.file_patterns
            && self.span_patterns == other.span_patterns
            && self.target_patterns == other.target_patterns
        // Note: We don't compare compiled matchers since they're derived from the patterns
    }
}

// Builder methods for Matcher
impl Match {
    pub fn new(level: impl Into<Level>) -> Self {
        Self {
            level: level.into(),
            include: true,
            module_patterns: vec![],
            file_patterns: vec![],
            span_patterns: vec![],
            target_patterns: vec![],
            module_matchers: vec![],
            file_matchers: vec![],
            span_matchers: vec![],
            target_matchers: vec![],
        }
    }

    pub fn trace() -> Self {
        Self::new(TracingLevel::TRACE)
    }

    pub fn debug() -> Self {
        Self::new(TracingLevel::DEBUG)
    }

    pub fn info() -> Self {
        Self::new(TracingLevel::INFO)
    }

    pub fn warn() -> Self {
        Self::new(TracingLevel::WARN)
    }

    pub fn error() -> Self {
        Self::new(TracingLevel::ERROR)
    }

    // Set inclusion/exclusion
    pub fn include(mut self) -> Self {
        self.include = true;
        self
    }

    pub fn exclude(mut self) -> Self {
        self.include = false;
        self
    }

    pub fn module_patterns(
        mut self,
        patterns: impl IntoIterator<Item = impl Into<String>>,
    ) -> Self {
        self.module_patterns = patterns.into_iter().map(Into::<String>::into).collect();
        self.module_matchers = self
            .module_patterns
            .iter()
            .map(|p| PatternMatcher::new(p))
            .collect();
        self
    }

    pub fn module_pattern(mut self, pattern: impl Into<String>) -> Self {
        let pattern_str = pattern.into();
        self.module_matchers.push(PatternMatcher::new(&pattern_str));
        self.module_patterns.push(pattern_str);
        self
    }

    pub fn extend_module_patterns(
        mut self,
        patterns: impl IntoIterator<Item = impl Into<String>>,
    ) -> Self {
        let new_patterns: Vec<String> = patterns.into_iter().map(Into::<String>::into).collect();
        for pattern in &new_patterns {
            self.module_matchers.push(PatternMatcher::new(pattern));
        }
        self.module_patterns.extend(new_patterns);
        self
    }

    pub fn file_patterns(mut self, patterns: impl IntoIterator<Item = impl Into<String>>) -> Self {
        self.file_patterns = patterns.into_iter().map(Into::<String>::into).collect();
        self.file_matchers = self
            .file_patterns
            .iter()
            .map(|p| PatternMatcher::new(p))
            .collect();
        self
    }

    pub fn file_pattern(mut self, pattern: impl Into<String>) -> Self {
        let pattern_str = pattern.into();
        self.file_matchers.push(PatternMatcher::new(&pattern_str));
        self.file_patterns.push(pattern_str);
        self
    }

    pub fn extend_file_patterns(
        mut self,
        patterns: impl IntoIterator<Item = impl Into<String>>,
    ) -> Self {
        let new_patterns: Vec<String> = patterns.into_iter().map(Into::<String>::into).collect();
        for pattern in &new_patterns {
            self.file_matchers.push(PatternMatcher::new(pattern));
        }
        self.file_patterns.extend(new_patterns);
        self
    }

    pub fn span_patterns(mut self, patterns: impl IntoIterator<Item = impl Into<String>>) -> Self {
        self.span_patterns = patterns.into_iter().map(Into::<String>::into).collect();
        self.span_matchers = self
            .span_patterns
            .iter()
            .map(|p| PatternMatcher::new(p))
            .collect();
        self
    }

    pub fn span_pattern(mut self, pattern: impl Into<String>) -> Self {
        let pattern_str = pattern.into();
        self.span_matchers.push(PatternMatcher::new(&pattern_str));
        self.span_patterns.push(pattern_str);
        self
    }

    pub fn extend_span_patterns(
        mut self,
        patterns: impl IntoIterator<Item = impl Into<String>>,
    ) -> Self {
        let new_patterns: Vec<String> = patterns.into_iter().map(Into::<String>::into).collect();
        for pattern in &new_patterns {
            self.span_matchers.push(PatternMatcher::new(pattern));
        }
        self.span_patterns.extend(new_patterns);
        self
    }

    pub fn target_patterns(
        mut self,
        patterns: impl IntoIterator<Item = impl Into<String>>,
    ) -> Self {
        self.target_patterns = patterns.into_iter().map(Into::<String>::into).collect();
        self.target_matchers = self
            .target_patterns
            .iter()
            .map(|p| PatternMatcher::new(p))
            .collect();
        self
    }

    pub fn target_pattern(mut self, pattern: impl Into<String>) -> Self {
        let pattern_str = pattern.into();
        self.target_matchers.push(PatternMatcher::new(&pattern_str));
        self.target_patterns.push(pattern_str);
        self
    }

    pub fn extend_target_patterns(
        mut self,
        patterns: impl IntoIterator<Item = impl Into<String>>,
    ) -> Self {
        let new_patterns: Vec<String> = patterns.into_iter().map(Into::<String>::into).collect();
        for pattern in &new_patterns {
            self.target_matchers.push(PatternMatcher::new(pattern));
        }
        self.target_patterns.extend(new_patterns);
        self
    }

    // Shorthand for common patterns
    pub fn all_modules(mut self) -> Self {
        let pattern = "*".to_string();
        self.module_matchers.push(PatternMatcher::new(&pattern));
        self.module_patterns.push(pattern);
        self
    }

    pub fn into_matcher_set(self) -> MatcherSet {
        MatcherSet::from_matcher(self)
    }

    pub fn matches(&self, event: &Event) -> bool {
        // Check level first
        match self.level.0 {
            TracingLevel::ERROR => {
                if event.level.0 != TracingLevel::ERROR {
                    return false;
                }
            }
            TracingLevel::WARN => {
                if !matches!(event.level.0, TracingLevel::ERROR | TracingLevel::WARN) {
                    return false;
                }
            }
            TracingLevel::INFO => {
                if !matches!(
                    event.level.0,
                    TracingLevel::ERROR | TracingLevel::WARN | TracingLevel::INFO
                ) {
                    return false;
                }
            }
            TracingLevel::DEBUG => {
                if !matches!(
                    event.level.0,
                    TracingLevel::ERROR
                        | TracingLevel::WARN
                        | TracingLevel::INFO
                        | TracingLevel::DEBUG
                ) {
                    return false;
                }
            }
            TracingLevel::TRACE => {} // All levels pass
        }

        // Check module path
        if let Some(module_path) = &event.module_path {
            // If we have include patterns, at least one must match
            if !self.module_matchers.is_empty() {
                let mut module_matched = false;
                for matcher in &self.module_matchers {
                    if matcher.matches(module_path) {
                        module_matched = true;
                        break;
                    }
                }
                if !module_matched {
                    return false;
                }
            }
        } else if !self.module_matchers.is_empty() {
            // Special case: if there's a wildcard pattern, allow no-module events
            let has_wildcard = self
                .module_matchers
                .iter()
                .any(|m| matches!(m, PatternMatcher::Wildcard));
            if !has_wildcard {
                // If we require a specific module pattern but there's no module path, exclude
                return false;
            }
        }

        // Check file path
        if !self.file_matchers.is_empty() {
            let mut file_matched = false;
            if let Some(file) = &event.file {
                for matcher in &self.file_matchers {
                    if matcher.matches(file) {
                        file_matched = true;
                        break;
                    }
                }
            }
            if !file_matched {
                return false;
            }
        }

        // Check span name
        if !self.span_matchers.is_empty() {
            let mut span_matched = false;
            if let Some(span_name) = &event.span_name {
                for matcher in &self.span_matchers {
                    if matcher.matches(span_name) {
                        span_matched = true;
                        break;
                    }
                }
            }
            if !span_matched {
                return false;
            }
        }

        // Check target
        if !self.target_matchers.is_empty() {
            let mut target_matched = false;
            for matcher in &self.target_matchers {
                if matcher.matches(&event.target) {
                    target_matched = true;
                    break;
                }
            }
            if !target_matched {
                return false;
            }
        }

        true
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MatcherSet {
    matchers: std::collections::HashSet<Match>,
}

// Trait for ergonomic conversion to MatcherSet
pub trait IntoMatcherSet {
    fn into_matcher_set(self) -> MatcherSet;
}

// Single Matcher implementation
impl IntoMatcherSet for Match {
    fn into_matcher_set(self) -> MatcherSet {
        MatcherSet::from_matcher(self)
    }
}

// MatcherSet already is a MatcherSet
impl IntoMatcherSet for MatcherSet {
    fn into_matcher_set(self) -> MatcherSet {
        self
    }
}

// Array implementations for various sizes (common ones)
impl<const N: usize> IntoMatcherSet for [Match; N] {
    fn into_matcher_set(self) -> MatcherSet {
        MatcherSet::from_matchers(self)
    }
}

// Vec implementation
impl IntoMatcherSet for Vec<Match> {
    fn into_matcher_set(self) -> MatcherSet {
        MatcherSet::from_matchers(self)
    }
}

// Slice reference implementation
impl IntoMatcherSet for &[Match] {
    fn into_matcher_set(self) -> MatcherSet {
        MatcherSet::from_matchers(self.iter().cloned())
    }
}

impl MatcherSet {
    pub fn empty() -> Self {
        Self {
            matchers: std::collections::HashSet::new(),
        }
    }

    pub fn from_matcher(matcher: Match) -> Self {
        let mut filter = Self::empty();
        filter.matchers.insert(matcher);
        filter
    }

    pub fn from_matchers(matchers: impl IntoIterator<Item = Match>) -> Self {
        let mut filter = Self::empty();
        for matcher in matchers {
            filter.matchers.insert(matcher);
        }
        filter
    }

    pub fn with_matcher(mut self, filter: Match) -> Self {
        self.matchers.replace(filter);
        self
    }

    pub fn add_matcher(&mut self, filter: Match) {
        self.matchers.replace(filter);
    }

    pub fn remove_matcher(&mut self, filter: &Match) -> bool {
        self.matchers.remove(filter)
    }

    pub fn clear_matchers(&mut self) {
        self.matchers.clear();
    }

    pub fn is_empty(&self) -> bool {
        self.matchers.is_empty()
    }

    pub fn iter_matchers(&self) -> Vec<&Match> {
        self.matchers.iter().collect()
    }
}

// Make Matcher work directly as a MatcherSet when needed
impl From<Match> for MatcherSet {
    fn from(val: Match) -> Self {
        val.into_matcher_set()
    }
}