syn_solidity/item/
struct.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
use crate::{FieldList, SolIdent, Spanned, Type};
use proc_macro2::Span;
use std::{
    fmt,
    hash::{Hash, Hasher},
};
use syn::{
    braced,
    parse::{Parse, ParseStream},
    token::Brace,
    Attribute, Result, Token,
};

/// A struct definition: `struct Foo { uint256 bar; }`.
///
/// Solidity reference:
/// <https://docs.soliditylang.org/en/latest/grammar.html#a4.SolidityParser.structDefinition>
#[derive(Clone)]
pub struct ItemStruct {
    pub attrs: Vec<Attribute>,
    pub struct_token: Token![struct],
    pub name: SolIdent,
    pub brace_token: Brace,
    pub fields: FieldList,
}

impl PartialEq for ItemStruct {
    fn eq(&self, other: &Self) -> bool {
        self.name == other.name && self.fields == other.fields
    }
}

impl Eq for ItemStruct {}

impl Hash for ItemStruct {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.name.hash(state);
        self.fields.hash(state);
    }
}

impl fmt::Display for ItemStruct {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "struct {} {{ {} }}", self.name, self.fields)
    }
}

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

impl Parse for ItemStruct {
    fn parse(input: ParseStream<'_>) -> Result<Self> {
        let content;
        Ok(Self {
            attrs: input.call(Attribute::parse_outer)?,
            struct_token: input.parse()?,
            name: input.parse()?,
            brace_token: braced!(content in input),
            fields: content.parse()?,
        })
    }
}

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

    fn set_span(&mut self, span: Span) {
        self.struct_token = Token![struct](span);
        self.name.set_span(span);
        self.brace_token = Brace(span);
    }
}

impl ItemStruct {
    pub fn as_type(&self) -> Type {
        let mut ty = Type::Tuple(self.fields.types().cloned().collect());
        ty.set_span(self.span());
        ty
    }
}