syn_solidity/variable/
mod.rs1use crate::{Expr, SolIdent, Spanned, Storage, Type, VariableAttributes};
2use proc_macro2::Span;
3use std::fmt::{self, Write};
4use syn::{
5 ext::IdentExt,
6 parse::{Parse, ParseStream},
7 Attribute, Ident, Result, Token,
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 write!(f, "{}", self.ty)?;
82 if let Some(name) = &self.name {
83 write!(f, " {name}")?;
84 }
85 Ok(())
86 }
87
88 pub fn parse_with_name(input: ParseStream<'_>) -> Result<Self> {
89 Self::_parse(input, true)
90 }
91
92 fn _parse(input: ParseStream<'_>, require_name: bool) -> Result<Self> {
93 Ok(Self {
94 attrs: input.call(Attribute::parse_outer)?,
95 ty: input.parse()?,
96 storage: input.call(Storage::parse_opt)?,
97 name: if require_name || input.peek(Ident::peek_any) {
98 Some(input.parse()?)
99 } else {
100 None
101 },
102 })
103 }
104}
105
106#[derive(Clone)]
107pub struct VariableDefinition {
108 pub attrs: Vec<Attribute>,
109 pub ty: Type,
110 pub attributes: VariableAttributes,
111 pub name: SolIdent,
112 pub initializer: Option<(Token![=], Expr)>,
113 pub semi_token: Token![;],
114}
115
116impl fmt::Display for VariableDefinition {
117 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
118 write!(f, "{} {} {}", self.ty, self.attributes, self.name)?;
119 if let Some((_, _expr)) = &self.initializer {
120 write!(f, " = <expr>")?;
122 }
123 f.write_str(";")
124 }
125}
126
127impl fmt::Debug for VariableDefinition {
128 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
129 f.debug_struct("VariableDefinition")
130 .field("ty", &self.ty)
131 .field("attributes", &self.attributes)
132 .field("name", &self.name)
133 .field("initializer", &self.initializer)
134 .finish()
135 }
136}
137
138impl Parse for VariableDefinition {
139 fn parse(input: ParseStream<'_>) -> Result<Self> {
140 Ok(Self {
141 attrs: Attribute::parse_outer(input)?,
142 ty: input.parse()?,
143 attributes: input.parse()?,
144 name: input.parse()?,
145 initializer: if input.peek(Token![=]) {
146 Some((input.parse()?, input.parse()?))
147 } else {
148 None
149 },
150 semi_token: input.parse()?,
151 })
152 }
153}
154
155impl Spanned for VariableDefinition {
156 fn span(&self) -> Span {
157 let span = self.ty.span();
158 span.join(self.semi_token.span).unwrap_or(span)
159 }
160
161 fn set_span(&mut self, span: Span) {
162 self.ty.set_span(span);
163 self.semi_token.span = span;
164 }
165}
166
167impl VariableDefinition {
168 pub fn as_declaration(&self) -> VariableDeclaration {
169 VariableDeclaration {
170 attrs: Vec::new(),
171 ty: self.ty.clone(),
172 storage: None,
173 name: Some(self.name.clone()),
174 }
175 }
176}