forked from diesel-rs/diesel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathidentifiable.rs
More file actions
28 lines (25 loc) · 847 Bytes
/
Copy pathidentifiable.rs
File metadata and controls
28 lines (25 loc) · 847 Bytes
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
use quote::Tokens;
use syn;
use model::Model;
pub fn derive_identifiable(item: syn::MacroInput) -> Tokens {
let model = t!(Model::from_item(&item, "Identifiable"));
let table_name = model.table_name();
let struct_ty = &model.ty;
let lifetimes = model.generics.lifetimes;
let primary_key_names = model.primary_key_names;
let fields = model.attrs;
for pk in &primary_key_names {
if !fields.iter().any(|f| f.field_name.as_ref() == Some(pk)) {
panic!("Could not find a field named `{}` on `{}`", pk, &model.name);
}
}
quote!(impl_Identifiable! {
(
table_name = #table_name,
primary_key_names = (#(#primary_key_names),*),
struct_ty = #struct_ty,
lifetimes = (#(#lifetimes),*),
),
fields = [#(#fields)*],
})
}