rune/ast/
expr_select.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
use crate::ast::prelude::*;

#[test]
#[cfg(not(miri))]
fn ast_parse() {
    let select = rt::<ast::ExprSelect>(
        r#"
    select {
        _ = a => 0,
        _ = b => {},
        _ = c => {}
        default => ()
    }
    "#,
    );

    assert_eq!(4, select.branches.len());
    assert!(matches!(
        select.branches.get(1),
        Some(&(ast::ExprSelectBranch::Pat(..), Some(..)))
    ));
    assert!(matches!(
        select.branches.get(2),
        Some(&(ast::ExprSelectBranch::Pat(..), None))
    ));
    assert!(matches!(
        select.branches.get(3),
        Some(&(ast::ExprSelectBranch::Default(..), None))
    ));
}

/// A `select` expression that selects over a collection of futures.
///
/// * `select { [arm]* }`.
#[derive(Debug, TryClone, PartialEq, Eq, ToTokens, Spanned)]
#[non_exhaustive]
pub struct ExprSelect {
    /// The attributes of the `select`
    #[rune(iter)]
    pub attributes: Vec<ast::Attribute>,
    /// The `select` keyword.
    pub select: T![select],
    /// The open brace.
    pub open: T!['{'],
    /// The branches of the select.
    #[rune(iter)]
    pub branches: Vec<(ExprSelectBranch, Option<T![,]>)>,
    /// The close brace.
    pub close: T!['}'],
}

impl ExprSelect {
    /// Parse the `select` expression and attach the given attributes
    pub(crate) fn parse_with_attributes(
        p: &mut Parser<'_>,
        attributes: Vec<ast::Attribute>,
    ) -> Result<Self> {
        let select = p.parse()?;
        let open = p.parse()?;

        let mut branches = Vec::new();

        while !p.peek::<T!['}']>()? {
            let branch = ExprSelectBranch::parse(p)?;
            let comma = p.parse::<Option<T![,]>>()?;
            let is_end = ast::utils::is_block_end(branch.expr(), comma.as_ref());
            branches.try_push((branch, comma))?;

            if is_end {
                break;
            }
        }

        let close = p.parse()?;

        Ok(Self {
            attributes,
            select,
            open,
            branches,
            close,
        })
    }
}

expr_parse!(Select, ExprSelect, "select expression");

/// A single selection branch.
#[derive(Debug, TryClone, PartialEq, Eq, ToTokens, Spanned)]
#[non_exhaustive]
#[allow(clippy::large_enum_variant)]
pub enum ExprSelectBranch {
    /// A patterned branch.
    Pat(ExprSelectPatBranch),
    /// A default branch.
    Default(ExprDefaultBranch),
}

impl ExprSelectBranch {
    /// Access the expression body.
    pub(crate) fn expr(&self) -> &ast::Expr {
        match self {
            ExprSelectBranch::Pat(pat) => &pat.body,
            ExprSelectBranch::Default(def) => &def.body,
        }
    }
}

impl Parse for ExprSelectBranch {
    fn parse(p: &mut Parser<'_>) -> Result<Self> {
        Ok(if p.peek::<T![default]>()? {
            Self::Default(p.parse()?)
        } else {
            Self::Pat(p.parse()?)
        })
    }
}

/// A single selection branch.
#[derive(Debug, TryClone, PartialEq, Eq, ToTokens, Parse, Spanned)]
#[non_exhaustive]
pub struct ExprSelectPatBranch {
    /// The identifier to bind the result to.
    pub pat: ast::Pat,
    /// `=`.
    pub eq: T![=],
    /// The expression that should evaluate to a future.
    pub expr: ast::Expr,
    /// `=>`.
    pub rocket: T![=>],
    /// The body of the expression.
    pub body: ast::Expr,
}

/// A single selection branch.
#[derive(Debug, TryClone, PartialEq, Eq, ToTokens, Parse, Spanned)]
#[non_exhaustive]
pub struct ExprDefaultBranch {
    /// The `default` keyword.
    pub default: T![default],
    /// `=>`.
    pub rocket: T![=>],
    /// The body of the expression.
    pub body: ast::Expr,
}