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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
//! Emulate dynamic dispatch and ["sealed classes"](https://kotlinlang.org/docs/reference/sealed-classes.html) using a proxy enum, which defers all method calls to its variants.
//!
//! # Introduction
//! In rust, dynamic dispatch is done using trait objects (`dyn Trait`).
//! They enable us to have runtime polymorphism, a way of expressing that a type implements a
//! certain trait while ignoring its concrete implementation.
//!
//! ```
//! let animal: &dyn Animal = random_animal();
//! animal.feed(); // may print "mew", "growl" or "squeak"
//! ```
//!
//! Trait objects come with a downside though:
//! getting a concrete implementation back from a trait object (downcasting) is painfull.
//! (see [std::any::Any])
//!
//! If you know there are only a finite number of implentations to work with, an `enum` might be
//! better at expressing such a relationship:
//! ```
//! enum Animal {
//!     Cat(Cat),
//!     Lion(Lion),
//!     Mouse(Mouse)
//! }
//!
//! match random_animal() {
//!     Animal::Cat(cat) => cat.feed(),
//!     Animal::Lion(lion) => lion.feed(),
//!     Animal::Mouse(mouse) => mouse.feed()
//! }
//! ```
//! Some languages have special support for such types, like Kotlin with so called "sealed classes".
//!
//! Rust, however, does *not*.
//!
//! `proxy-enum` simplifies working with such types using procedural macros.
//!
//! # Usage
//! ```
//! #[proxy_enum::proxy(Animal)]
//! mod proxy {
//!     enum Animal {
//!         Cat(Cat),
//!         Lion(Lion),
//!         Mouse(Mouse)
//!     }
//!
//!     impl Animal {
//!         #[implement]
//!         fn feed(&self) {}
//!     }
//! }
//! ```
//! This will expand to:
//! ```
//! mod proxy {
//!     enum Animal {
//!         Cat(Cat),
//!         Lion(Lion),
//!         Mouse(Mouse)
//!     }
//!
//!     impl Animal {
//!         fn feed(&self) {
//!             match self {
//!                 Animal::Cat(cat) => cat.feed(),
//!                 Animal::Lion(lion) => lion.feed(),
//!                 Animal::Mouse(mouse) => mouse.feed()
//!             }
//!         }
//!     }
//!     
//!     impl From<Cat> for Animal {
//!         fn from(from: Cat) -> Self {
//!             Animal::Cat(from)
//!         }
//!     }
//!
//!     impl From<Lion> for Animal {
//!         fn from(from: Lion) -> Self {
//!             Animal::Lion(from)
//!         }
//!     }
//!
//!     impl From<Mouse> for Animal {
//!         fn from(from: Mouse) -> Self {
//!             Animal::Mouse(from)
//!         }
//!     }
//! }
//! ```
//! This, however, will only compile if `Cat`, `Lion` and `Mouse` all have a method called `feed`.
//! Since rust has traits to express common functionality, trait implentations can be generated too:
//! ```
//! #[proxy_enum::proxy(Animal)]
//! mod proxy {
//!     enum Animal {
//!         Cat(Cat),
//!         Lion(Lion),
//!         Mouse(Mouse)
//!     }
//!
//!     trait Eat {
//!         fn feed(&self);
//!     }
//!
//!     #[implement]
//!     impl Eat for Animal {}
//! }
//! ```
//! Since the macro has to know which methods the trait contains, it has to be defined within the
//! module. However, implementations for external traits can be generated too:
//!
//! ```
//! #[proxy_enum::proxy(Animal)]
//! mod proxy {
//!     enum Animal {
//!         Cat(Cat),
//!         Lion(Lion),
//!         Mouse(Mouse)
//!     }
//!
//!     #[external(std::string::ToString)]
//!     trait ToString {
//!         fn to_string(&self) -> String;
//!     }
//!
//!     #[implement]
//!     impl std::string::ToString for Animal {}
//! }
//! ```

extern crate proc_macro2;

use proc_macro::TokenStream;
use std::collections::HashMap;

use syn::export::TokenStream2;
use syn::visit_mut::VisitMut;
use syn::{
    parse2, parse_macro_input, Attribute, Fields, FnArg, Ident, ImplItem, Item, ItemEnum, ItemImpl,
    ItemMod, ItemTrait, Pat, PatType, Path, PathArguments, Signature, TraitItem, Type, Variant,
};

use quote::quote;

const IMPL_ATTR: &str = "implement";
const EXT_ATTR: &str = "external";

fn attr_idx(attrs: &[Attribute], ident: &str) -> Option<usize> {
    (0..attrs.len()).find(|idx| attrs[*idx].path.is_ident(ident))
}

fn pop_attr(attrs: &mut Vec<Attribute>, ident: &str) -> Option<Attribute> {
    attr_idx(attrs, ident).map(|idx| attrs.remove(idx))
}

fn find_attr<'a>(attrs: &'a [Attribute], ident: &str) -> Option<&'a Attribute> {
    attr_idx(&attrs, ident).map(|idx| &attrs[idx])
}

fn gen_static_method_call(receiver: TokenStream2, signature: &Signature) -> TokenStream2 {
    let method_ident = &signature.ident;

    let args = signature
        .inputs
        .iter()
        .skip(1) // `self`
        .map(|a| match a {
            FnArg::Typed(PatType { pat, .. }) => match &**pat {
                Pat::Ident(ident) => &ident.ident,
                other => panic!("unsupported pattern in parameter: `{}`", quote! { #other }),
            },
            _ => panic!("parameter binding must be an identifier"),
        });

    quote! { #receiver::#method_ident(__self #(, #args)*) }
}

struct WrapperVariant {
    variant: Variant,
    wrapped: Type,
}

impl From<Variant> for WrapperVariant {
    fn from(variant: Variant) -> Self {
        match &variant.fields {
            Fields::Unnamed(a) if a.unnamed.len() == 1 => WrapperVariant {
                variant: variant.clone(),
                wrapped: a.unnamed.first().unwrap().ty.clone(),
            },
            _ => panic!("expected a variant with a single unnamed value"),
        }
    }
}

fn gen_match_block(
    variants: &[WrapperVariant],
    action: impl Fn(&WrapperVariant) -> TokenStream2,
) -> TokenStream2 {
    let branches = variants
        .iter()
        .map(|variant| {
            let action = action(&variant);
            let ident = &variant.variant.ident;
            quote! { Self::#ident(__self) => #action }
        })
        .collect::<Vec<_>>();

    quote! {
        match self {
            #(#branches),*
        }
    }
}

fn has_self_param(sig: &Signature) -> bool {
    sig.inputs
        .first()
        .map(|param| match param {
            FnArg::Receiver(..) => true,
            FnArg::Typed(PatType { pat, .. }) => match &**pat {
                Pat::Ident(ident) => &ident.ident.to_string() == "self",
                _ => false,
            },
        })
        .unwrap_or(false)
}

/// populate an empty `#[implement] impl Trait for ProxyEnum {}` block
fn implement_trait(
    trait_decl: &ItemTrait,
    variants: &[WrapperVariant],
    pseudo_impl: &mut ItemImpl,
) {
    assert!(pseudo_impl.items.is_empty());

    let trait_ident = &trait_decl.ident;

    let proxy_methods = trait_decl.items.iter().map(|i| match i {
        TraitItem::Method(i) => {
            let sig = &i.sig;
            if !has_self_param(sig) {
                match &i.default {
                    Some(..) => return parse2(quote! { #i }).unwrap(),
                    None => panic!(
                        "`{}` has no self parameter or default implementation",
                        quote! { #sig }
                    ),
                }
            }

            let match_block = gen_match_block(variants, |_| gen_static_method_call(quote! { #trait_ident }, sig));
            let tokens = quote! { #sig { #match_block } };
            parse2::<ImplItem>(tokens).unwrap()
        }
        _ => panic!(
            "impl block annotated with `#[{}]` may only contain methods",
            IMPL_ATTR
        ),
    });

    pseudo_impl.items = proxy_methods.collect();
}

/// populate methods in a `impl ProxyEnum { #[implement] fn method(&self) {} }` block
fn implement_raw(variants: &[WrapperVariant], pseudo_impl: &mut ItemImpl) {
    pseudo_impl
        .items
        .iter_mut()
        .flat_map(|i| match i {
            ImplItem::Method(method) => pop_attr(&mut method.attrs, IMPL_ATTR).map(|_| method),
            _ => None,
        })
        .for_each(|mut method| {
            if !method.block.stmts.is_empty() {
                panic!("method annotated with `#[{}]` must be empty", IMPL_ATTR)
            }

            let match_block = gen_match_block(variants, |variant| {
                let ty = &variant.wrapped;
                gen_static_method_call(quote! { #ty }, &method.sig)
            });
            let body = quote! { { #match_block } };
            method.block = syn::parse2(body).unwrap();
        });
}

struct GenerateProxyImpl {
    proxy_enum: Ident,
    variants: Option<Vec<WrapperVariant>>,
    trait_defs: HashMap<String, ItemTrait>,
}

impl GenerateProxyImpl {
    fn new(proxy_enum: Ident) -> Self {
        GenerateProxyImpl {
            proxy_enum,
            variants: None,
            trait_defs: HashMap::new(),
        }
    }

    fn get_variants(&self) -> &[WrapperVariant] {
        self.variants
            .as_ref()
            .unwrap_or_else(|| panic!("proxy enum must be defined first"))
            .as_slice()
    }

    fn store_trait_decl(&mut self, attr: Option<Path>, decl: ItemTrait) {
        let mut path = match attr {
            Some(path) => quote! { #path },
            None => {
                let ident = &decl.ident;
                quote! { #ident }
            }
        }
        .to_string();
        path.retain(|c| !c.is_whitespace());
        self.trait_defs.insert(path, decl);
    }

    fn get_trait_decl(&self, mut path: Path) -> &ItemTrait {
        path.segments
            .iter_mut()
            .for_each(|seg| seg.arguments = PathArguments::None);
        let mut path = quote! { #path }.to_string();
        path.retain(|c| !c.is_whitespace());

        self.trait_defs
            .get(&path)
            .unwrap_or_else(|| panic!("missing declaration of trait `{}`", path))
    }
    
    fn impl_from_variants(&self, module: &mut ItemMod) {
        let proxy_enum = &self.proxy_enum;
        for WrapperVariant { variant, wrapped, .. } in self.get_variants() {
            let variant = &variant.ident;
            let tokens = quote! {
                impl From<#wrapped> for #proxy_enum {
                    fn from(from: #wrapped) -> Self {
                        #proxy_enum :: #variant(from)
                    }
                }
            };
            let from_impl: ItemImpl = syn::parse2(tokens).unwrap();
            module.content.as_mut().unwrap().1.push(from_impl.into()); 
        }
    }
}

impl VisitMut for GenerateProxyImpl {
    // store variants of our enum
    fn visit_item_enum_mut(&mut self, i: &mut ItemEnum) {
        if i.ident != self.proxy_enum {
            return;
        }
        assert!(self.variants.is_none());

        self.variants = Some(
            i.variants
                .iter()
                .cloned()
                .map(WrapperVariant::from)
                .collect(),
        );
    }

    fn visit_item_impl_mut(&mut self, impl_block: &mut ItemImpl) {
        match impl_block.trait_.as_mut() {
            // `impl Type { #[implement] fn abc() {} }
            None => implement_raw(self.get_variants(), impl_block),
            // #[implement] `impl Trait for Type {}`
            Some((_, path, _)) => {
                if pop_attr(&mut impl_block.attrs, IMPL_ATTR).is_some() {
                    implement_trait(
                        self.get_trait_decl(path.clone()),
                        self.get_variants(),
                        impl_block,
                    );
                }
            }
        };
    }

    fn visit_item_mod_mut(&mut self, module: &mut ItemMod) {
        syn::visit_mut::visit_item_mod_mut(self, module);
        // remove all items annotated with external
        module.content.as_mut().unwrap().1.retain(|item| {
            if let Item::Trait(ItemTrait { attrs, .. }) = item {
                find_attr(&attrs, EXT_ATTR).is_none()
            } else {
                true
            }
        });
        self.impl_from_variants(module);
    }

    // scan for trait declarations and store them
    fn visit_item_trait_mut(&mut self, trait_def: &mut ItemTrait) {
        let ext_attr = find_attr(&trait_def.attrs, EXT_ATTR).map(|attr| attr.parse_args().unwrap());
        self.store_trait_decl(ext_attr, trait_def.clone());
    }
}

#[proc_macro_attribute]
pub fn proxy(attr: TokenStream, item: TokenStream) -> TokenStream {
    let mut module = parse_macro_input!(item as ItemMod);
    let proxy_enum = parse_macro_input!(attr as Ident);

    GenerateProxyImpl::new(proxy_enum).visit_item_mod_mut(&mut module);

    TokenStream::from(quote! { #module })
}