harper_core/expr/
all.rs

1use crate::{Span, Token, expr::Expr};
2
3/// A [`Step`] that consumes a list of expressions and only
4/// matches if all the child [`Expr`]s do.
5///
6/// It will return the position of the farthest window.
7#[derive(Default)]
8pub struct All {
9    children: Vec<Box<dyn Expr>>,
10}
11
12impl All {
13    pub fn new(children: Vec<Box<dyn Expr>>) -> Self {
14        Self { children }
15    }
16
17    pub fn add(&mut self, e: impl Expr + 'static) {
18        self.children.push(Box::new(e));
19    }
20}
21
22impl Expr for All {
23    fn run(&self, cursor: usize, tokens: &[Token], source: &[char]) -> Option<Span> {
24        let mut longest: Option<Span> = None;
25
26        for expr in self.children.iter() {
27            let window = expr.run(cursor, tokens, source)?;
28
29            if let Some(longest_window) = longest {
30                if window.len() > longest_window.len() {
31                    longest = Some(window);
32                }
33            } else {
34                longest = Some(window);
35            }
36        }
37
38        longest
39    }
40}