syn_solidity/item/
struct.rs1use crate::{FieldList, SolIdent, Spanned, Type};
2use proc_macro2::Span;
3use std::{
4 fmt,
5 hash::{Hash, Hasher},
6};
7use syn::{
8 braced,
9 parse::{Parse, ParseStream},
10 token::Brace,
11 Attribute, Result, Token,
12};
13
14#[derive(Clone)]
19pub struct ItemStruct {
20 pub attrs: Vec<Attribute>,
21 pub struct_token: Token![struct],
22 pub name: SolIdent,
23 pub brace_token: Brace,
24 pub fields: FieldList,
25}
26
27impl PartialEq for ItemStruct {
28 fn eq(&self, other: &Self) -> bool {
29 self.name == other.name && self.fields == other.fields
30 }
31}
32
33impl Eq for ItemStruct {}
34
35impl Hash for ItemStruct {
36 fn hash<H: Hasher>(&self, state: &mut H) {
37 self.name.hash(state);
38 self.fields.hash(state);
39 }
40}
41
42impl fmt::Display for ItemStruct {
43 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
44 write!(f, "struct {} {{ {} }}", self.name, self.fields)
45 }
46}
47
48impl fmt::Debug for ItemStruct {
49 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
50 f.debug_struct("ItemStruct")
51 .field("attrs", &self.attrs)
52 .field("name", &self.name)
53 .field("fields", &self.fields)
54 .finish()
55 }
56}
57
58impl Parse for ItemStruct {
59 fn parse(input: ParseStream<'_>) -> Result<Self> {
60 let content;
61 Ok(Self {
62 attrs: input.call(Attribute::parse_outer)?,
63 struct_token: input.parse()?,
64 name: input.parse()?,
65 brace_token: braced!(content in input),
66 fields: content.parse()?,
67 })
68 }
69}
70
71impl Spanned for ItemStruct {
72 fn span(&self) -> Span {
73 self.name.span()
74 }
75
76 fn set_span(&mut self, span: Span) {
77 self.struct_token = Token;
78 self.name.set_span(span);
79 self.brace_token = Brace(span);
80 }
81}
82
83impl ItemStruct {
84 pub fn as_type(&self) -> Type {
85 let mut ty = Type::Tuple(self.fields.types().cloned().collect());
86 ty.set_span(self.span());
87 ty
88 }
89}