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
extern crate proc_macro;
mod attr;
use crate::proc_macro::TokenStream;
use attr::GuzzleAttributes;
use quote::quote;
use syn::{parse_macro_input, Data, DeriveInput, Field, Fields, FieldsNamed, Ident, LitStr};
struct FieldAttribute<'a> {
field: &'a Ident,
attributes: GuzzleAttributes,
}
impl<'a> FieldAttribute<'a> {
fn get_arm_parts(&self) -> Vec<(&Ident, &LitStr)> {
self.attributes
.keys
.iter()
.map(|matcher| (self.field, matcher))
.collect()
}
}
impl<'a> From<&'a Field> for FieldAttribute<'a> {
fn from(field: &'a Field) -> Self {
let mut attributes = GuzzleAttributes::new();
let all_attrs = &field.attrs;
for attr in all_attrs {
let path = &attr.path;
if let "guzzle" = quote!(#path).to_string().as_ref() {
let tokens = attr.tts.clone();
let is_empty = tokens.is_empty();
attributes = syn::parse2(tokens).unwrap_or_else(|err| {
let tokens_str = if is_empty {
String::new()
} else {
format!("problematic tokens: {}", &attr.tts)
};
panic!("{}, {}", err.to_string(), tokens_str)
});
}
}
let field = field.ident.as_ref().unwrap();
FieldAttribute { field, attributes }
}
}
#[proc_macro_derive(Guzzle, attributes(guzzle, noguzzle))]
pub fn guzzle_macro_derive(input: TokenStream) -> TokenStream {
let ast = parse_macro_input!(input);
impl_guzzle(ast)
}
fn impl_guzzle(ast: DeriveInput) -> TokenStream {
match &ast.data {
Data::Struct(s) => match &s.fields {
Fields::Named(fields) => impl_guzzle_named_fields(&ast, fields),
_ => unimplemented!(),
},
_ => unimplemented!(),
}
}
fn fields_to_attributes(fields: &FieldsNamed) -> Vec<FieldAttribute> {
fields.named.iter().map(|field| field.into()).collect()
}
fn impl_guzzle_named_fields(ast: &DeriveInput, fields: &FieldsNamed) -> TokenStream {
let name = &ast.ident;
let (impl_generics, ty_generics, where_clause) = ast.generics.split_for_impl();
let attributes = fields_to_attributes(fields);
let mut keys_and_matchers = Vec::new();
for attr in &attributes {
keys_and_matchers.append(&mut attr.get_arm_parts());
}
let (keys, matchers): (Vec<_>, Vec<_>) = keys_and_matchers.into_iter().unzip();
let gen = quote! {
impl #impl_generics Guzzle for #name #ty_generics #where_clause {
fn guzzle<T>(&mut self, (key, value): (T, String)) -> Option<(T, String)>
where T: AsRef<str>
{
match key.as_ref() {
#( #matchers => self.#keys = value, )*
_ => return Some((key, value)),
};
None
}
}
};
gen.into()
}