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
use crate::{utils::DebugPunctuated, SolIdent, Spanned, Type};
use proc_macro2::Span;
use std::{fmt, num::NonZeroU16};
use syn::{
    braced,
    parse::{Parse, ParseStream},
    punctuated::Punctuated,
    token::Brace,
    Attribute, Result, Token,
};

/// An enum definition: `enum Foo { A, B, C }`.
///
/// Solidity reference:
/// <https://docs.soliditylang.org/en/latest/grammar.html#a4.SolidityParser.enumDefinition>
#[derive(Clone)]
pub struct ItemEnum {
    pub attrs: Vec<Attribute>,
    pub enum_token: Token![enum],
    pub name: SolIdent,
    pub brace_token: Brace,
    pub variants: Punctuated<SolIdent, Token![,]>,
}

impl fmt::Debug for ItemEnum {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("ItemEnum")
            .field("attrs", &self.attrs)
            .field("name", &self.name)
            .field("variants", DebugPunctuated::new(&self.variants))
            .finish()
    }
}

impl Parse for ItemEnum {
    fn parse(input: ParseStream<'_>) -> Result<Self> {
        let content;
        Ok(Self {
            attrs: input.call(Attribute::parse_outer)?,
            enum_token: input.parse()?,
            name: input.parse()?,
            brace_token: braced!(content in input),
            variants: content.parse_terminated(SolIdent::parse, Token![,])?,
        })
    }
}

impl Spanned for ItemEnum {
    fn span(&self) -> Span {
        self.name.span()
    }

    fn set_span(&mut self, span: Span) {
        self.name.set_span(span);
    }
}

impl ItemEnum {
    pub fn as_type(&self) -> Type {
        Type::Uint(self.span(), Some(NonZeroU16::new(8).unwrap()))
    }
}