forked from diesel-rs/diesel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsertable.rs
More file actions
43 lines (36 loc) · 1.13 KB
/
Copy pathinsertable.rs
File metadata and controls
43 lines (36 loc) · 1.13 KB
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
use syn;
use quote;
use model::Model;
pub fn derive_insertable(item: syn::DeriveInput) -> quote::Tokens {
let model = t!(Model::from_item(&item, "Insertable"));
if !model.has_table_name_annotation() {
panic!(
r#"`#[derive(Insertable)]` requires the struct to be annotated \
with `#[table_name="something"]`"#
);
}
if !model.generics.ty_params.is_empty() {
panic!("`#[derive(Insertable)]` does not support generic types");
}
let struct_name = &model.name;
let struct_ty = &model.ty;
let table_name = &model.table_name();
let lifetimes = model.generics.lifetimes;
let fields = model.attrs.as_slice();
if fields.is_empty() {
panic!(
"Failed to derive `Insertable` for `{}`: `Insertable` \
cannot be used on structs with empty fields",
struct_name
);
}
quote!(impl_Insertable! {
(
struct_name = #struct_name,
table_name = #table_name,
struct_ty = #struct_ty,
lifetimes = (#(#lifetimes),*),
),
fields = [#(#fields)*],
})
}