harper_core/expr/
unless_step.rs

1use crate::{Token, expr::Expr};
2
3use super::Step;
4
5/// Provides the ability to use an expression as a condition.
6/// If the condition does __not match__, it will return the result of the provided step.
7pub struct UnlessStep<E: Expr, S: Step> {
8    condition: E,
9    step: S,
10}
11
12impl<E, S> UnlessStep<E, S>
13where
14    E: Expr,
15    S: Step,
16{
17    pub fn new(condition: E, step: S) -> Self {
18        Self { condition, step }
19    }
20}
21
22impl<E: Expr, S: Step> Step for UnlessStep<E, S> {
23    fn step(&self, tokens: &[Token], cursor: usize, source: &[char]) -> Option<isize> {
24        if self.condition.run(cursor, tokens, source).is_none() {
25            self.step.step(tokens, cursor, source)
26        } else {
27            None
28        }
29    }
30}