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

/// This crate should not be used on it's own, it implements the (IntoQuery trait)[https://docs.rs/into_query]

use proc_macro::TokenStream;
use proc_macro2::TokenStream as TokenStream2;
use quote::quote;
use syn::{parse_macro_input, DeriveInput};

fn get_types(data: &syn::Data) -> Vec<(proc_macro2::Ident, syn::Type)> {
    let mut map = vec![];
    if let syn::Data::Struct(syn::DataStruct {
        fields: syn::Fields::Named(syn::FieldsNamed { named, .. }),
        ..
    }) = data.to_owned()
    {
        let mut iter = named.into_iter();
        while let Some(syn::Field {
            ident: Some(ident),
            ty,
            ..
        }) = iter.next()
        {
            map.push((ident, ty));
        }
    }

    map
}

fn is_vec(ty: &syn::Type) -> bool {
    match ty.to_owned() {
        syn::Type::Path(syn::TypePath {
            path: syn::Path { segments, .. },
            ..
        }) => match segments.iter().next() {
            Some(syn::PathSegment { ident, arguments })
                if ident == &syn::Ident::new("Option", proc_macro2::Span::call_site()) =>
            {
                match arguments {
                    syn::PathArguments::AngleBracketed(syn::AngleBracketedGenericArguments {
                        args,
                        ..
                    }) => match args.iter().next() {
                        Some(syn::GenericArgument::Type(ty2)) => is_vec(ty2),
                        _ => false,
                    },
                    _ => unreachable!(),
                }
            }

            Some(syn::PathSegment { ident, .. })
                if ident == &syn::Ident::new("Vec", proc_macro2::Span::call_site()) =>
            {
                true
            }
            _ => false,
        },
        _ => false,
    }
}

fn attr_ident(attr: &syn::Attribute) -> Option<&syn::Ident> {
    let syn::Path { segments, .. } = &attr.path;
    if let Some(syn::PathSegment { ident, .. }) = segments.iter().next() { 
        Some(ident)
    } else {
        None
    }
}
fn attr_lit(attr: &syn::Attribute) -> Option<proc_macro2::Literal> {
    match attr.tokens.clone().into_iter().nth(1) {
        Some(proc_macro2::TokenTree::Literal(lit)) => Some(lit),
        _ => None,
    }
}

fn literal_to_ident(lit: proc_macro2::Literal) -> syn::Ident {
    let mut s = format!("{}", lit);
    s.pop();
    s.remove(0);
    syn::Ident::new(&s, proc_macro2::Span::call_site())
}

fn table_name(item: &syn::DeriveInput) -> Option<syn::Ident> {
    item.attrs
        .iter()
        .map(|attr| (attr_ident(attr).unwrap(), attr_lit(attr).unwrap()))
        .find(|(ident, _)| {
            ident == &&syn::Ident::new("table_name", proc_macro2::Span::call_site())
        })
        .map(|optional| literal_to_ident(optional.1))
}

#[proc_macro_derive(IntoQuery, attributes(table_name))]
pub fn derive_into_query(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as DeriveInput);
    let types = get_types(&input.data);
    let struct_ident = &input.ident;
    let table = table_name(&input).expect("table_name not specified");

    let body = types
        .iter()
        .map(|(ident, ty)| {
            if is_vec(ty) {
                quote! {
                    if let Some(container) = self.#ident {
                        let mut iter = container.into_iter();
                        if let Some(first) = iter.next() {
                            query = query.filter(#ident.eq(first));
                            for item in iter {
                                query = query.or_filter(#ident.eq(item));
                            }
                        }
                    }
                }
            } else {
                quote! {
                    if let Some(item) = self.#ident {
                        query = query.filter(#ident.eq(item));
                    }
                }
            }
        })
        .fold(TokenStream2::new(), |mut acc, cur| {
            acc.extend(cur.into_iter());
            acc
        });

    let gen = quote! {
        impl ::into_query::IntoQuery<crate::db::schema::#table::dsl::#table> for #struct_ident {
            fn into_query(
                self,
            ) -> diesel::helper_types::IntoBoxed<
                'static,
                crate::db::schema::#table::dsl::#table,
                diesel::mysql::Mysql,
            > {
                use crate::db::schema::#table::dsl::*;
                use diesel::prelude::*;
                let mut query = #table.into_boxed();
                #body
                query
            }
        }
    };
    gen.into()
}