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
#![deny(warnings)]
extern crate proc_macro;
extern crate syn;
#[macro_use]
extern crate quote;
use proc_macro::TokenStream;
use syn::MetaItem::*;
#[proc_macro_derive(IsDao)]
pub fn is_dao(input: TokenStream) -> TokenStream {
let s = input.to_string();
let ast = syn::parse_macro_input(&s).unwrap();
let gen = impl_is_dao(&ast);
gen.parse().unwrap()
}
fn impl_is_dao(ast: &syn::MacroInput) -> quote::Tokens {
let name = &ast.ident;
let fields:Vec<(&syn::Ident, &syn::Ty)> = match ast.body {
syn::Body::Struct(ref data) => {
match *data{
syn::VariantData::Struct(ref fields) => {
fields.iter().map(|f| {
let ident = f.ident.as_ref().unwrap();
let ty = &f.ty;
(ident,ty)
}).collect::<Vec<_>>()
},
_ => panic!("tuples and unit are not covered")
}
},
syn::Body::Enum(_) => panic!("#[derive(NumFields)] can only be used with structs"),
};
let from_fields:Vec<quote::Tokens> =
fields.iter().map(|&(field,_ty)| {
quote!{
#field: {
let v = dao.get(stringify!(#field)).unwrap();
FromValue::from_type(v.to_owned())
},
}
}).collect::<Vec<_>>();
let to_dao:Vec<quote::Tokens> =
fields.iter().map(|&(field,_ty)| {
quote!{
dao.insert(stringify!(#field).to_string(), self.#field.to_db_type());
}
}).collect::<Vec<_>>();
quote! {
impl IsDao for #name {
fn from_dao(dao: &Dao) -> Self{
#name{
#(#from_fields)*
}
}
fn to_dao(&self) -> Dao {
let mut dao = Dao::new();
#(#to_dao)*
dao
}
}
}
}
#[proc_macro_derive(IsTable)]
pub fn to_table_name(input: TokenStream) -> TokenStream {
let s = input.to_string();
let ast = syn::parse_macro_input(&s).unwrap();
let gen = impl_to_table_name(&ast);
gen.parse().unwrap()
}
fn get_table_attr(attrs: &Vec<syn::Attribute>)->Option<String>{
for att in attrs{
println!("{:?}", att);
match att.value{
Word(_) => continue,
List(_,_) => continue,
NameValue(ref name, ref value) => {
if name == "table"{
match *value{
syn::Lit::Str(ref s,ref _style) => {
return Some(s.to_owned())
}
_ => continue
}
}else{continue}
}
};
}
None
}
fn impl_to_table_name(ast: &syn::MacroInput) -> quote::Tokens {
let name = &ast.ident;
let attrs = &ast.attrs;
let tbl = get_table_attr(attrs);
let table_name = match tbl{
Some(tbl) => tbl,
None => format!("{}",name).to_lowercase()
};
let fields:Vec<&syn::Ident> = match ast.body {
syn::Body::Struct(ref data) => {
match *data{
syn::VariantData::Struct(ref fields) => {
fields.iter().map(|f| {
let ident = f.ident.as_ref().unwrap();
let _ty = &f.ty;
ident
}).collect::<Vec<_>>()
},
_ => panic!("tuples and unit are not covered")
}
},
syn::Body::Enum(_) => panic!("#[derive(NumFields)] can only be used with structs"),
};
let from_fields:Vec<quote::Tokens> =
fields.iter().map(|field| {
quote!{
ColumnName{
column: stringify!(#field).to_string(),
table: Some(#table_name.to_string()),
schema: None
}
}
}).collect::<Vec<_>>();
quote! {
impl IsTable for #name {
fn table_name() -> TableName{
TableName{
schema: None,
name: #table_name.to_string(),
columns: vec![#(#from_fields),*],
}
}
}
}
}