harper_core/patterns/
invert.rs

1use crate::Token;
2
3use super::Pattern;
4
5/// A struct that matches any pattern __except__ the one provided.
6pub struct Invert {
7    inner: Box<dyn Pattern>,
8}
9
10impl Invert {
11    pub fn new(inner: impl Pattern + 'static) -> Self {
12        Self {
13            inner: Box::new(inner),
14        }
15    }
16}
17
18impl Pattern for Invert {
19    fn matches(&self, tokens: &[Token], source: &[char]) -> Option<usize> {
20        if self.inner.matches(tokens, source).is_some() {
21            None
22        } else {
23            Some(1)
24        }
25    }
26}