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
230
231
#![recursion_limit = "128"]
extern crate proc_macro;
use itertools::izip;
use proc_macro::TokenStream;
use quote::quote;
use syn::{Data, DeriveInput, Ident};
#[proc_macro_derive(FromDataPoint )]
pub fn from_data_point(input: TokenStream) -> TokenStream {
let ast = syn::parse_macro_input!(input as DeriveInput);
let fields = match ast.data {
Data::Struct(st) => st.fields,
_ => panic!("Implementation must be a struct"),
};
let idents: Vec<&Ident> = fields
.iter()
.filter_map(|field| field.ident.as_ref())
.collect::<Vec<&Ident>>();
let keys: Vec<String> = idents
.clone()
.iter()
.map(|ident| ident.to_string())
.collect::<Vec<String>>();
let typenames = fields
.iter()
.map(|field| {
let t = field.ty.clone();
let s = quote! {#t}.to_string();
s
})
.collect::<Vec<String>>();
let name: &Ident = &ast.ident;
let (impl_generics, ty_generics, where_clause) = ast.generics.split_for_impl();
let datetime_re = regex::Regex::new(r"DateTime").unwrap();
let duration_re = regex::Regex::new(r"Duration").unwrap();
let base64_re = regex::Regex::new(r"Vec").unwrap();
let mut assignments = Vec::new();
for (key, typename, ident) in izip!(keys, typenames, idents) {
match &typename[..] {
"f64" => {
assignments.push(quote! {
let mut key = String::from(#key);
if !hashmap.contains_key(&key) {
key = format!("_{}", key);
}
match hashmap.entry(key.clone()) {
::std::collections::btree_map::Entry::Occupied(entry) => {
if let influxdb2_structmap::value::Value::Double(v) = entry.get() {
settings.#ident = (v as &::num_traits::cast::ToPrimitive).to_f64().unwrap();
}
},
_ => panic!("Cannot parse out map entry, key: {}", key),
}
})
}
"i64" => {
assignments.push(quote! {
let mut key = String::from(#key);
if !hashmap.contains_key(&key) {
key = format!("_{}", key);
}
match hashmap.entry(key.clone()) {
::std::collections::btree_map::Entry::Occupied(entry) => {
if let influxdb2_structmap::value::Value::Long(v) = entry.get() {
settings.#ident = *v;
}
},
_ => panic!("Cannot parse out map entry, key: {}", key),
}
})
}
"u64" => {
assignments.push(quote! {
let mut key = String::from(#key);
if !hashmap.contains_key(&key) {
key = format!("_{}", key);
}
match hashmap.entry(key.clone()) {
::std::collections::btree_map::Entry::Occupied(entry) => {
if let influxdb2_structmap::value::Value::UnsignedLong(v) = entry.get() {
settings.#ident = *v;
}
},
_ => panic!("Cannot parse out map entry, key: {}", key),
}
})
}
"bool" => {
assignments.push(quote! {
let mut key = String::from(#key);
if !hashmap.contains_key(&key) {
key = format!("_{}", key);
}
match hashmap.entry(key.clone()) {
::std::collections::btree_map::Entry::Occupied(entry) => {
if let influxdb2_structmap::value::Value::Bool(v) = entry.get() {
settings.#ident = *v;
}
},
_ => panic!("Cannot parse out map entry, key: {}", key),
}
})
}
"String" => {
assignments.push(quote! {
let mut key = String::from(#key);
if !hashmap.contains_key(&key) {
key = format!("_{}", key);
}
match hashmap.entry(key.clone()) {
::std::collections::btree_map::Entry::Occupied(entry) => {
if let influxdb2_structmap::value::Value::String(v) = entry.get() {
settings.#ident = v.clone();
}
},
_ => panic!("Cannot parse out map entry, key: {}", key),
}
})
}
x if duration_re.is_match(x) => {
assignments.push(quote! {
let mut key = String::from(#key);
if !hashmap.contains_key(&key) {
key = format!("_{}", key);
}
match hashmap.entry(key.clone()) {
::std::collections::btree_map::Entry::Occupied(entry) => {
if let influxdb2_structmap::value::Value::Duration(v) = entry.get() {
settings.#ident = *v;
}
},
_ => panic!("Cannot parse out map entry, key: {}", key),
}
})
}
x if datetime_re.is_match(x) => {
assignments.push(quote! {
let mut key = String::from(#key);
if !hashmap.contains_key(&key) {
key = format!("_{}", key);
}
match hashmap.entry(key.clone()) {
::std::collections::btree_map::Entry::Occupied(entry) => {
if let influxdb2_structmap::value::Value::TimeRFC(v) = entry.get() {
settings.#ident = *v;
}
},
_ => panic!("Cannot parse out map entry, key: {}", key),
}
})
}
x if base64_re.is_match(x) => {
assignments.push(quote! {
let mut key = String::from(#key);
if !hashmap.contains_key(&key) {
key = format!("_{}", key);
}
match hashmap.entry(key.clone()) {
::std::collections::btree_map::Entry::Occupied(entry) => {
if let influxdb2_structmap::value::Value::Base64Binary(v) = entry.get() {
settings.#ident = *v;
}
},
_ => panic!("Cannot parse out map entry, key: {}", key),
}
})
}
x => {
panic!("{} is not handled", x);
}
}
}
let tokens = quote! {
impl #impl_generics influxdb2_structmap::FromMap for #name #ty_generics #where_clause {
fn from_genericmap(mut hashmap: influxdb2_structmap::GenericMap) -> #name {
let mut settings = #name::default();
#(
#assignments
)*
settings
}
}
};
TokenStream::from(tokens)
}
#[cfg(test)]
mod tests {
#[test]
fn ui() {
let t = trybuild::TestCases::new();
t.pass("tests/struct.rs");
t.pass("tests/multistruct.rs");
}
}