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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
use frame_support_procedural_tools_derive::{Parse, ToTokens};
use proc_macro2::{TokenStream, TokenTree};
use quote::ToTokens;
use std::iter::once;
use syn::{
parse::{Parse, ParseStream, Result},
visit::{self, Visit},
Ident,
};
#[derive(Parse, ToTokens, Debug)]
pub struct StopParse {
pub inner: TokenStream,
}
macro_rules! groups_impl {
($name:ident, $tok:ident, $deli:ident, $parse:ident) => {
#[derive(Debug)]
pub struct $name<P> {
pub token: syn::token::$tok,
pub content: P,
}
impl<P: Parse> Parse for $name<P> {
fn parse(input: ParseStream) -> Result<Self> {
let syn::group::$name { token, content } = syn::group::$parse(input)?;
let content = content.parse()?;
Ok($name { token, content })
}
}
impl<P: ToTokens> ToTokens for $name<P> {
fn to_tokens(&self, tokens: &mut TokenStream) {
let mut inner_stream = TokenStream::new();
self.content.to_tokens(&mut inner_stream);
let token_tree: proc_macro2::TokenTree =
proc_macro2::Group::new(proc_macro2::Delimiter::$deli, inner_stream).into();
tokens.extend(once(token_tree));
}
}
impl<P: Clone> Clone for $name<P> {
fn clone(&self) -> Self {
Self { token: self.token.clone(), content: self.content.clone() }
}
}
};
}
groups_impl!(Braces, Brace, Brace, parse_braces);
groups_impl!(Brackets, Bracket, Bracket, parse_brackets);
groups_impl!(Parens, Paren, Parenthesis, parse_parens);
#[derive(Debug)]
pub struct PunctuatedInner<P, T, V> {
pub inner: syn::punctuated::Punctuated<P, T>,
pub variant: V,
}
#[derive(Debug, Clone)]
pub struct NoTrailing;
#[derive(Debug, Clone)]
pub struct Trailing;
pub type Punctuated<P, T> = PunctuatedInner<P, T, NoTrailing>;
pub type PunctuatedTrailing<P, T> = PunctuatedInner<P, T, Trailing>;
impl<P: Parse, T: Parse + syn::token::Token> Parse for PunctuatedInner<P, T, Trailing> {
fn parse(input: ParseStream) -> Result<Self> {
Ok(PunctuatedInner {
inner: syn::punctuated::Punctuated::parse_separated_nonempty(input)?,
variant: Trailing,
})
}
}
impl<P: Parse, T: Parse> Parse for PunctuatedInner<P, T, NoTrailing> {
fn parse(input: ParseStream) -> Result<Self> {
Ok(PunctuatedInner {
inner: syn::punctuated::Punctuated::parse_terminated(input)?,
variant: NoTrailing,
})
}
}
impl<P: ToTokens, T: ToTokens, V> ToTokens for PunctuatedInner<P, T, V> {
fn to_tokens(&self, tokens: &mut TokenStream) {
self.inner.to_tokens(tokens)
}
}
impl<P: Clone, T: Clone, V: Clone> Clone for PunctuatedInner<P, T, V> {
fn clone(&self) -> Self {
Self { inner: self.inner.clone(), variant: self.variant.clone() }
}
}
#[derive(Debug, Clone)]
pub struct Meta {
pub inner: syn::Meta,
}
impl Parse for Meta {
fn parse(input: ParseStream) -> Result<Self> {
Ok(Meta { inner: syn::Meta::parse(input)? })
}
}
impl ToTokens for Meta {
fn to_tokens(&self, tokens: &mut TokenStream) {
match self.inner {
syn::Meta::Path(ref path) => path.to_tokens(tokens),
syn::Meta::List(ref l) => l.to_tokens(tokens),
syn::Meta::NameValue(ref n) => n.to_tokens(tokens),
}
}
}
#[derive(Debug)]
pub struct OuterAttributes {
pub inner: Vec<syn::Attribute>,
}
impl Parse for OuterAttributes {
fn parse(input: ParseStream) -> Result<Self> {
let inner = syn::Attribute::parse_outer(input)?;
Ok(OuterAttributes { inner })
}
}
impl ToTokens for OuterAttributes {
fn to_tokens(&self, tokens: &mut TokenStream) {
for att in self.inner.iter() {
att.to_tokens(tokens);
}
}
}
pub fn extract_type_option(typ: &syn::Type) -> Option<syn::Type> {
if let syn::Type::Path(ref path) = typ {
let v = path.path.segments.last()?;
if v.ident == "Option" {
if let syn::PathArguments::AngleBracketed(a) = &v.arguments {
if let syn::GenericArgument::Type(typ) = a.args.last()? {
return Some(typ.clone())
}
}
}
}
None
}
struct ContainsIdent<'a> {
ident: &'a Ident,
result: bool,
}
impl<'ast> ContainsIdent<'ast> {
fn visit_tokenstream(&mut self, stream: TokenStream) {
stream.into_iter().for_each(|tt| match tt {
TokenTree::Ident(id) => self.visit_ident(&id),
TokenTree::Group(ref group) => self.visit_tokenstream(group.stream()),
_ => {},
})
}
fn visit_ident(&mut self, ident: &Ident) {
if ident == self.ident {
self.result = true;
}
}
}
impl<'ast> Visit<'ast> for ContainsIdent<'ast> {
fn visit_ident(&mut self, input: &'ast Ident) {
self.visit_ident(input);
}
fn visit_macro(&mut self, input: &'ast syn::Macro) {
self.visit_tokenstream(input.tokens.clone());
visit::visit_macro(self, input);
}
}
pub fn type_contains_ident(typ: &syn::Type, ident: &Ident) -> bool {
let mut visit = ContainsIdent { result: false, ident };
visit::visit_type(&mut visit, typ);
visit.result
}
pub fn expr_contains_ident(expr: &syn::Expr, ident: &Ident) -> bool {
let mut visit = ContainsIdent { result: false, ident };
visit::visit_expr(&mut visit, expr);
visit.result
}