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
use crate::ast;
use crate::ast::utils;
use crate::{Parse, ParseError, Parser, Spanned, ToTokens};
#[derive(Debug, Clone, PartialEq, Eq, ToTokens, Spanned)]
pub struct ExprSelect {
#[rune(iter)]
pub attributes: Vec<ast::Attribute>,
pub select: T![select],
pub open: T!['{'],
pub branches: Vec<(ExprSelectBranch, Option<T![,]>)>,
pub close: T!['}'],
}
impl ExprSelect {
pub fn parse_with_attributes(
p: &mut Parser<'_>,
attributes: Vec<ast::Attribute>,
) -> Result<Self, ParseError> {
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 = utils::is_block_end(branch.expr(), comma.as_ref());
branches.push((branch, comma));
if is_end {
break;
}
}
let close = p.parse()?;
Ok(Self {
attributes,
select,
open,
branches,
close,
})
}
}
expr_parse!(Select, ExprSelect, "select expression");
#[derive(Debug, Clone, PartialEq, Eq, ToTokens, Spanned)]
pub enum ExprSelectBranch {
Pat(ExprSelectPatBranch),
Default(ExprDefaultBranch),
}
impl ExprSelectBranch {
pub 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, ParseError> {
Ok(if p.peek::<T![default]>()? {
Self::Default(p.parse()?)
} else {
Self::Pat(p.parse()?)
})
}
}
#[derive(Debug, Clone, PartialEq, Eq, ToTokens, Parse, Spanned)]
pub struct ExprSelectPatBranch {
pub pat: ast::Pat,
pub eq: T![=],
pub expr: ast::Expr,
pub rocket: T![=>],
pub body: ast::Expr,
}
#[derive(Debug, Clone, PartialEq, Eq, ToTokens, Parse, Spanned)]
pub struct ExprDefaultBranch {
pub default: T![default],
pub rocket: T![=>],
pub body: ast::Expr,
}