harper_core/patterns/
all.rs

1use crate::Token;
2
3use super::Pattern;
4
5/// A [`Pattern`] that consumes a list of other patterns and only
6/// matches if all the child patterns do.
7///
8/// It will match the length of the longest pattern.
9#[derive(Default)]
10pub struct All {
11    children: Vec<Box<dyn Pattern>>,
12}
13
14impl All {
15    pub fn new(children: Vec<Box<dyn Pattern>>) -> Self {
16        Self { children }
17    }
18
19    pub fn add(&mut self, p: Box<dyn Pattern>) {
20        self.children.push(p);
21    }
22}
23
24impl Pattern for All {
25    fn matches(&self, tokens: &[Token], source: &[char]) -> Option<usize> {
26        let mut max = 0;
27
28        for pattern in &self.children {
29            let len = pattern.matches(tokens, source)?;
30            max = max.max(len);
31        }
32
33        Some(max)
34    }
35}