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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
extern crate proc_macro;

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

#[derive(Clone)]
enum DelegateImplementer {
    Enum {
        variant_idents: Vec<syn::Ident>,
    },
    SingleFieldStruct {
        field_ident: proc_macro2::TokenStream,
    },
}

#[proc_macro_derive(Delegate, attributes(delegate))]
pub fn delegate_macro(input: TokenStream) -> TokenStream {
    // Parse the input tokens into a syntax tree
    let input = parse_macro_input!(input as DeriveInput);

    let delegate_attributes: Vec<syn::Attribute> = input
        .clone()
        .attrs
        .into_iter()
        .filter(|n| n.path.is_ident("delegate"))
        .collect();
    if delegate_attributes.len() == 0 {
        panic!("No #[delegate] attribute specified. If you want to delegate an implementation of trait `SomeTrait` add the attribute:\n#[delegate(SomeTrait)]")
    }

    let implementer_ident = input.ident.clone();
    let implementer: Option<DelegateImplementer> = match input.clone().data {
        syn::Data::Enum(enum_data) => {
            let variant_idents = enum_data.variants.into_iter().map(|n| n.ident).collect();
            Some(DelegateImplementer::Enum { variant_idents })
        }
        syn::Data::Struct(struct_data) => match struct_data.fields {
            syn::Fields::Unnamed(fields_unnamed) => {
                match fields_unnamed.unnamed.into_iter().collect::<Vec<_>>().len() {
                    1 => Some(DelegateImplementer::SingleFieldStruct {
                        field_ident: quote! { 0 },
                    }),
                    _ => None,
                }
            }
            syn::Fields::Named(fields_named) => {
                match fields_named
                    .named
                    .clone()
                    .into_iter()
                    .collect::<Vec<_>>()
                    .len()
                {
                    1 => {
                        let field_ident = fields_named
                            .named
                            .into_iter()
                            .next()
                            .unwrap()
                            .ident
                            .unwrap();
                        Some(DelegateImplementer::SingleFieldStruct {
                            field_ident: quote! { #field_ident },
                        })
                    }
                    _ => None,
                }
            }
            _ => None,
        },
        _ => None,
    };
    if let None = implementer {
        panic!(
            "ambassador currently only supports #[derive(Delegate)] for: \n\
             - single-field enums\n\
             - single field (tuple) structs"
        )
    }
    let implementer = implementer.unwrap();

    let mut impl_macros = vec![];

    for delegate_attr in delegate_attributes {
        let trait_path_full: syn::Path = delegate_attr.parse_args().unwrap();
        let trait_ident: syn::Ident = trait_path_full
            .segments
            .clone()
            .into_iter()
            .last()
            .unwrap()
            .ident;

        let (trait_path, trait_path_colon) = build_invocation_path(trait_path_full);
        let macro_name: syn::Ident = quote::format_ident!("ambassador_impl_{}", trait_ident);

        let impl_macro = match implementer.clone() {
            DelegateImplementer::Enum { variant_idents } => {
                quote! {
                    #trait_path#trait_path_colon#macro_name!{Enum; #implementer_ident; #(#implementer_ident::#variant_idents),*}
                }
            }
            DelegateImplementer::SingleFieldStruct { field_ident } => {
                quote! {
                    #trait_path#trait_path_colon#macro_name!{SingleFieldStruct; #implementer_ident; #field_ident}
                }
            }
        };
        impl_macros.push(impl_macro);
    }

    // Build the output, possibly using quasi-quotation
    let expanded = quote! {
        #(#impl_macros)*
    };

    // Hand the output tokens back to the compiler
    TokenStream::from(expanded)
}

/// Build the invocation path prefix for the macro invocation.
///
/// Macros are always imported from the crate root
///
/// (None, None) -> ""
/// (Some(...), Some(...)) -> "foo_crate::"
fn build_invocation_path(
    trait_path_full: syn::Path,
) -> (Option<syn::Path>, Option<syn::token::Colon2>) {
    let trait_path: Vec<syn::PathSegment> = trait_path_full
        .segments
        .clone()
        .into_iter()
        .take(usize::max(trait_path_full.segments.len() - 1, 0))
        .collect();
    let trait_path_str = trait_path
        .into_iter()
        .map(|n| n.ident.to_string())
        .collect::<Vec<_>>()
        .join("::");

    let (trait_path, trait_path_colon): (Option<syn::Path>, Option<syn::token::Colon2>) =
        match trait_path_str.as_ref() {
            "" => (None, None),
            _ => (
                Some(syn::parse_str(&trait_path_str).unwrap()),
                Some(syn::parse_quote!(::)),
            ),
        };
    (trait_path, trait_path_colon)
}

#[proc_macro_attribute]
pub fn delegatable_trait(_attr: TokenStream, item: TokenStream) -> TokenStream {
    let original_item: syn::ItemTrait = syn::parse(item).unwrap();
    let register_trait = build_register_trait(original_item.clone());

    let expanded = quote! {
        #original_item

        #register_trait
    };
    TokenStream::from(expanded)
}

#[proc_macro_attribute]
pub fn delegatable_trait_remote(_attr: TokenStream, item: TokenStream) -> TokenStream {
    let original_item: syn::ItemTrait = syn::parse(item).unwrap();
    let register_trait = build_register_trait(original_item.clone());

    let expanded = quote! {
        #register_trait
    };
    TokenStream::from(expanded)
}

fn build_register_trait(original_item: syn::ItemTrait) -> proc_macro2::TokenStream {
    let trait_ident = original_item.ident.clone();
    let macro_name: syn::Ident = quote::format_ident!("ambassador_impl_{}", trait_ident);

    let original_trait_methods: Vec<syn::TraitItemMethod> = original_item
        .items
        .clone()
        .into_iter()
        .map(|n| match n {
            syn::TraitItem::Method(method) => method,
            _ => unimplemented!(),
        })
        .collect();

    let enum_trait_methods = build_enum_trait_methods(original_trait_methods.clone());
    let single_field_struct_methods = build_single_field_struct_methods(original_trait_methods);

    let register_trait = quote! {
        #[macro_export]
        macro_rules! #macro_name {
            (Enum; $implementer:ty; $( $variants:path ),+) => {
                impl #trait_ident for $implementer {
                    #(#enum_trait_methods)*
                }
            };
            (SingleFieldStruct; $implementer:ty; $field_ident:tt) => {
                impl #trait_ident for $implementer {
                    #(#single_field_struct_methods)*
                }
            };
        }
    };

    register_trait
}

fn build_enum_trait_methods(
    original_trait_methods: Vec<syn::TraitItemMethod>,
) -> Vec<proc_macro2::TokenStream> {
    let mut enum_trait_methods = vec![];
    for original_method in original_trait_methods {
        let method_sig = original_method.sig.clone();
        let method_invocation = build_method_invocation(original_method, quote!(inner));

        let method_impl = quote! {
            #method_sig {
                match self {
                    $($variants(inner) => #method_invocation),*
                }
            }
        };
        enum_trait_methods.push(method_impl);
    }
    enum_trait_methods
}

fn build_single_field_struct_methods(
    original_trait_methods: Vec<syn::TraitItemMethod>,
) -> Vec<proc_macro2::TokenStream> {
    let mut enum_trait_methods = vec![];
    for original_method in original_trait_methods {
        let method_sig = original_method.sig.clone();
        let method_invocation = build_method_invocation(original_method, quote!(self.$field_ident));

        let method_impl = quote! {
            #method_sig {
                #method_invocation
            }
        };
        enum_trait_methods.push(method_impl);
    }
    enum_trait_methods
}

fn build_method_invocation(
    original_method: syn::TraitItemMethod,
    field_ident: proc_macro2::TokenStream,
) -> proc_macro2::TokenStream {
    let method_sig = original_method.sig.clone();
    let method_ident = method_sig.ident.clone();
    let argument_list: syn::punctuated::Punctuated<syn::Pat, syn::token::Comma> = method_sig
        .inputs
        .clone()
        .into_iter()
        .filter_map(|fn_arg| match fn_arg {
            syn::FnArg::Receiver(_) => None,
            syn::FnArg::Typed(pat_type) => Some(pat_type),
        })
        .map(|pat_type| *pat_type.pat.to_owned())
        .collect();

    let method_invocation = quote! { #field_ident.#method_ident(#argument_list) };
    method_invocation
}