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
use crate::ast;
use crate::{Id, OptionSpanned, Parse, ParseError, Parser, Spanned, ToTokens};
#[derive(Debug, Clone, PartialEq, Eq, Parse, ToTokens, Spanned)]
#[rune(parse = "meta_only")]
pub struct ItemEnum {
#[rune(iter, meta)]
pub attributes: Vec<ast::Attribute>,
#[rune(optional, meta)]
pub visibility: ast::Visibility,
pub enum_token: T![enum],
pub name: ast::Ident,
pub variants: ast::Braced<ItemVariant, T![,]>,
}
item_parse!(Enum, ItemEnum, "enum item");
#[derive(Debug, Clone, PartialEq, Eq, Parse, ToTokens, Spanned)]
pub struct ItemVariant {
#[rune(id)]
pub id: Option<Id>,
#[rune(iter)]
pub attributes: Vec<ast::Attribute>,
pub name: ast::Ident,
#[rune(optional)]
pub body: ItemVariantBody,
}
#[derive(Debug, Clone, PartialEq, Eq, ToTokens, OptionSpanned)]
pub enum ItemVariantBody {
UnitBody,
TupleBody(ast::Parenthesized<ast::Field, T![,]>),
StructBody(ast::Braced<ast::Field, T![,]>),
}
impl ItemVariantBody {
pub fn fields(&self) -> impl Iterator<Item = &'_ (ast::Field, Option<T![,]>)> {
match self {
ItemVariantBody::UnitBody => IntoIterator::into_iter(&[]),
ItemVariantBody::TupleBody(body) => body.iter(),
ItemVariantBody::StructBody(body) => body.iter(),
}
}
}
impl Parse for ItemVariantBody {
fn parse(p: &mut Parser<'_>) -> Result<Self, ParseError> {
Ok(match p.nth(0)? {
K!['('] => Self::TupleBody(p.parse()?),
K!['{'] => Self::StructBody(p.parse()?),
_ => Self::UnitBody,
})
}
}