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
#![recursion_limit = "128"]
extern crate proc_macro;
extern crate syn;
#[macro_use]
extern crate quote;
use proc_macro::TokenStream;
use syn::{Ident, VariantData};
#[proc_macro_derive(FromHashmap)]
pub fn from_hashmap(input: TokenStream) -> TokenStream {
let source = input.to_string();
let ast = syn::parse_macro_input(&source).unwrap();
let idents: Vec<Ident> = match ast.body {
syn::Body::Struct(vdata) => {
match vdata {
VariantData::Struct(fields) => {
let mut idents = Vec::new();
for ref field in fields.iter() {
match &field.ident {
&Some(ref ident) => idents.push(ident.clone()),
&None => panic!("Your struct is missing a field identity!"),
}
}
idents
},
VariantData::Tuple(_) | VariantData::Unit => {
panic!("You can only derive this for normal structs!");
},
}
},
syn::Body::Enum(_) => panic!("You can only derive this on structs!"),
};
let mut keys = Vec::new();
for ident in idents.iter() {
keys.push(String::from(ident.as_ref()));
}
let name = &ast.ident;
let (impl_generics, ty_generics, where_clause) = ast.generics.split_for_impl();
let tokens = quote! {
fn parse_pair<T>(v: &str) -> T where T : ::std::str::FromStr {
let res = v.parse::<T>();
match res {
Ok(val) => val,
Err(_) => panic!(format!("Unable to convert given input into required type: {}", v)),
}
}
impl #impl_generics FromHashmap<#name> for #name #ty_generics #where_clause {
fn from_hashmap(mut hm: ::std::collections::HashMap<String, String>) -> #name {
let mut settings = #name::default();
#(
match hm.entry(String::from(#keys)) {
::std::collections::hash_map::Entry::Occupied(occ_ent) => {
settings.#idents = parse_pair(occ_ent.get().as_str());
},
::std::collections::hash_map::Entry::Vacant(_) => (),
}
)*
settings
}
}
};
tokens.parse().unwrap()
}