rune/ast/
item_enum.rs

1use crate::ast::prelude::*;
2
3#[test]
4#[cfg(not(miri))]
5fn ast_parse() {
6    rt::<ast::ItemEnum>("enum Foo { Bar(a), Baz(b), Empty() }");
7    rt::<ast::ItemEnum>("enum Foo { Bar(a), Baz(b), #[default_value = \"zombie\"] Empty() }");
8    rt::<ast::ItemEnum>(
9        "#[repr(Rune)] enum Foo { Bar(a), Baz(b), #[default_value = \"zombie\"] Empty() }",
10    );
11    rt::<ast::ItemEnum>("pub enum Color { Blue, Red, Green }");
12
13    rt::<ast::Fields>("( a, b, c )");
14    rt::<ast::Fields>("{ a, b, c }");
15    rt::<ast::Fields>("( #[serde(default)] a, b, c )");
16    rt::<ast::Fields>("{ a, #[debug(skip)] b, c }");
17}
18
19/// An enum item.
20#[derive(Debug, TryClone, PartialEq, Eq, Parse, ToTokens, Spanned)]
21#[rune(parse = "meta_only")]
22#[non_exhaustive]
23pub struct ItemEnum {
24    /// The attributes for the enum block
25    #[rune(iter, meta)]
26    pub attributes: Vec<ast::Attribute>,
27    /// The visibility of the `enum` item
28    #[rune(option, meta)]
29    pub visibility: ast::Visibility,
30    /// The `enum` token.
31    pub enum_token: T![enum],
32    /// The name of the enum.
33    pub name: ast::Ident,
34    /// Variants in the enum.
35    pub variants: ast::Braced<ItemVariant, T![,]>,
36}
37
38item_parse!(Enum, ItemEnum, "enum item");
39
40/// An enum variant.
41#[derive(Debug, TryClone, PartialEq, Eq, Parse, ToTokens, Spanned)]
42#[non_exhaustive]
43pub struct ItemVariant {
44    /// The attributes associated with the variant.
45    #[rune(iter)]
46    pub attributes: Vec<ast::Attribute>,
47    /// The name of the variant.
48    pub name: ast::Ident,
49    /// The body of the variant.
50    #[rune(iter)]
51    pub body: ast::Fields,
52    /// Opaque identifier of variant.
53    #[rune(skip)]
54    pub(crate) id: ItemId,
55}