aboutsummaryrefslogtreecommitdiff
path: root/tosin-macros/src/lib.rs
blob: 408378fae23ce03327e4fa15bbb73c4418807f35 (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
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
#![feature(proc_macro_span)]

extern crate proc_macro;

use std::fs;

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 {
            pub const META: tosin::db::models::ModelMeta = tosin::db::models::ModelMeta {
                name: 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()
}

#[proc_macro]
pub fn gather_models(_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 call_site_ast = syn::parse_file(&fs::read_to_string(call_site_path).unwrap()).unwrap();
    let models = call_site_ast.items.iter()
        .filter_map(|item| if let syn::Item::Struct(item) = item { Some(item) } else { None })
        .filter(|item| item.attrs.iter().any(|attr| {
            let attr = if let Ok(syn::Meta::List(attr)) = attr.parse_meta() { attr } else { return false; };
            if attr.path.get_ident().map_or(false, |hopefully_derive| hopefully_derive == "derive") {
                let mut derived = attr.nested.iter()
                    .filter_map(|derived| if let syn::NestedMeta::Meta(derived) = derived { Some(derived) } else { None });
                derived.any(|derived| derived.path().get_ident().map_or(false, |hopefully_model| hopefully_model == "Model"))
            } else {
                false
            }
        }))
        .map(|item| &item.ident);

    let gen = quote! {
        pub const ALL: &[tosin::db::models::ModelMeta] = &[
            #(#models::META),*
        ];
    };

    gen.into()
}