rune/ast/
condition.rs

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
use crate::ast::prelude::*;

#[test]
#[cfg(not(miri))]
fn ast_parse() {
    rt::<ast::Condition>("true");
    rt::<ast::Condition>("let [a, ..] = v");
}

/// The condition in an if statement.
///
/// * `true`.
/// * `let Some(<pat>) = <expr>`.
#[derive(Debug, TryClone, PartialEq, Eq, ToTokens, Spanned)]
#[non_exhaustive]
pub enum Condition {
    /// A regular expression.
    Expr(ast::Expr),
    /// A pattern match.
    ExprLet(ast::ExprLet),
}

impl Parse for Condition {
    fn parse(p: &mut Parser<'_>) -> Result<Self> {
        Ok(match p.nth(0)? {
            K![let] => Self::ExprLet(ast::ExprLet::parse_without_eager_brace(p)?),
            _ => Self::Expr(ast::Expr::parse_without_eager_brace(p)?),
        })
    }
}