syn_solidity/variable/
mod.rs1use crate::{Expr, SolIdent, Spanned, Storage, Type, VariableAttributes};
2use proc_macro2::Span;
3use std::fmt::{self, Write};
4use syn::{
5 Attribute, Ident, Result, Token,
6 ext::IdentExt,
7 parse::{Parse, ParseStream},
8};
9
10mod list;
11pub use list::{FieldList, ParameterList, Parameters};
12
13#[derive(Clone, Debug, PartialEq, Eq, Hash)]
15pub struct VariableDeclaration {
16 pub attrs: Vec<Attribute>,
18 pub ty: Type,
20 pub storage: Option<Storage>,
22 pub name: Option<SolIdent>,
25}
26
27impl fmt::Display for VariableDeclaration {
28 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
29 self.ty.fmt(f)?;
30 if let Some(storage) = &self.storage {
31 f.write_char(' ')?;
32 storage.fmt(f)?;
33 }
34 if let Some(name) = &self.name {
35 f.write_char(' ')?;
36 name.fmt(f)?;
37 }
38 Ok(())
39 }
40}
41
42impl Parse for VariableDeclaration {
43 fn parse(input: ParseStream<'_>) -> Result<Self> {
44 Self::_parse(input, false)
45 }
46}
47
48impl Spanned for VariableDeclaration {
49 fn span(&self) -> Span {
50 let span = self.ty.span();
51 match (&self.storage, &self.name) {
52 (Some(storage), None) => span.join(storage.span()),
53 (_, Some(name)) => span.join(name.span()),
54 (None, None) => Some(span),
55 }
56 .unwrap_or(span)
57 }
58
59 fn set_span(&mut self, span: Span) {
60 self.ty.set_span(span);
61 if let Some(storage) = &mut self.storage {
62 storage.set_span(span);
63 }
64 if let Some(name) = &mut self.name {
65 name.set_span(span);
66 }
67 }
68}
69
70impl VariableDeclaration {
71 pub const fn new(ty: Type) -> Self {
72 Self::new_with(ty, None, None)
73 }
74
75 pub const fn new_with(ty: Type, storage: Option<Storage>, name: Option<SolIdent>) -> Self {
76 Self { attrs: Vec::new(), ty, storage, name }
77 }
78
79 pub fn fmt_eip712(&self, f: &mut impl Write) -> fmt::Result {
81 match &self.ty {
83 crate::Type::Custom(path) => {
84 write!(f, "{}", path.last())?;
85 }
86 _ => {
87 write!(f, "{}", self.ty)?;
88 }
89 }
90 if let Some(name) = &self.name {
91 write!(f, " {name}")?;
92 }
93 Ok(())
94 }
95
96 pub fn parse_with_name(input: ParseStream<'_>) -> Result<Self> {
97 Self::_parse(input, true)
98 }
99
100 fn _parse(input: ParseStream<'_>, require_name: bool) -> Result<Self> {
101 Ok(Self {
102 attrs: input.call(Attribute::parse_outer)?,
103 ty: input.parse()?,
104 storage: input.call(Storage::parse_opt)?,
105 name: if require_name || input.peek(Ident::peek_any) {
106 Some(input.parse()?)
107 } else {
108 None
109 },
110 })
111 }
112}
113
114#[derive(Clone)]
115pub struct VariableDefinition {
116 pub attrs: Vec<Attribute>,
117 pub ty: Type,
118 pub attributes: VariableAttributes,
119 pub name: SolIdent,
120 pub initializer: Option<(Token![=], Expr)>,
121 pub semi_token: Token![;],
122}
123
124impl fmt::Display for VariableDefinition {
125 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
126 write!(f, "{} {} {}", self.ty, self.attributes, self.name)?;
127 if let Some((_, _expr)) = &self.initializer {
128 write!(f, " = <expr>")?;
130 }
131 f.write_str(";")
132 }
133}
134
135impl fmt::Debug for VariableDefinition {
136 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
137 f.debug_struct("VariableDefinition")
138 .field("ty", &self.ty)
139 .field("attributes", &self.attributes)
140 .field("name", &self.name)
141 .field("initializer", &self.initializer)
142 .finish()
143 }
144}
145
146impl Parse for VariableDefinition {
147 fn parse(input: ParseStream<'_>) -> Result<Self> {
148 Ok(Self {
149 attrs: Attribute::parse_outer(input)?,
150 ty: input.parse()?,
151 attributes: input.parse()?,
152 name: input.parse()?,
153 initializer: if input.peek(Token![=]) {
154 Some((input.parse()?, input.parse()?))
155 } else {
156 None
157 },
158 semi_token: input.parse()?,
159 })
160 }
161}
162
163impl Spanned for VariableDefinition {
164 fn span(&self) -> Span {
165 let span = self.ty.span();
166 span.join(self.semi_token.span).unwrap_or(span)
167 }
168
169 fn set_span(&mut self, span: Span) {
170 self.ty.set_span(span);
171 self.semi_token.span = span;
172 }
173}
174
175impl VariableDefinition {
176 pub fn as_declaration(&self) -> VariableDeclaration {
177 VariableDeclaration {
178 attrs: Vec::new(),
179 ty: self.ty.clone(),
180 storage: None,
181 name: Some(self.name.clone()),
182 }
183 }
184}