blob: 7efbac6c3fd32423dee544e74e323b6bca77994d (
plain)
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
|
#![feature(proc_macro_span)]
extern crate proc_macro;
use proc_macro::TokenStream;
use quote::quote;
#[proc_macro_derive(Model, attributes(model))]
pub fn model_derive(input: TokenStream) -> TokenStream {
// Construct a representation of Rust code as a syntax tree
// that we can manipulate
let ast = syn::parse(input).unwrap();
// Build the trait implementation
impl_model(&ast)
}
fn impl_model(ast: &syn::DeriveInput) -> TokenStream {
let name = &ast.ident;
let gen = quote! {
impl #name {
fn hello_macro() {
println!("Hello, Macro! My name is {}!", stringify!(#name));
}
}
};
gen.into()
}
#[proc_macro]
pub fn gather_migrations(_input: TokenStream) -> TokenStream {
let call_site = proc_macro::Span::call_site();
let call_site_file = call_site.source_file();
let call_site_path = call_site_file.path();
if !call_site_file.is_real() {
panic!("call site does not have a real path");
}
let migrations_dir = call_site_path.parent().unwrap();
let migrations: Vec<syn::Ident> = migrations_dir.read_dir()
.unwrap()
.map(Result::unwrap)
.map(|x| x.path().file_stem().unwrap().to_string_lossy().into_owned())
.filter(|x| x != "mod")
.map(|x| syn::parse_str(&x).unwrap())
.collect();
let gen = quote! {
#( mod #migrations; )*
pub const migrations: &[Migration] = &[
#(#migrations::MIGRATION),*
];
};
gen.into()
}
|