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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
extern crate proc_macro;
#[macro_use]
extern crate syn;
#[macro_use]
extern crate quote;

use quote::{quote};

use syn::{DeriveInput, ItemImpl, ImplItem, ImplItemMethod, PathSegment, TypePath, Item, Lifetime, Ident};
use proc_macro::TokenStream;

trait ToHelperName {
    fn to_helper_name(&self) -> Ident;
}

const HELPER_PREFIX: &str = "__pinar_helper_";

fn to_helper_name(name: &str) -> Ident {
    format_ident!("{}{}", HELPER_PREFIX, name.to_lowercase())
}

impl ToHelperName for Ident {
    fn to_helper_name(&self) -> Ident {
        to_helper_name(&self.to_string())
    }
}

impl ToHelperName for String {
    fn to_helper_name(&self) -> Ident {
        to_helper_name(&self)
    }
}

trait ElideOutputLifetime {
    fn elide_output_lifetime(&mut self);
}

impl ElideOutputLifetime for ImplItemMethod {
    fn elide_output_lifetime(&mut self) {
        let ImplItemMethod {
            attrs,
            sig: syn::Signature {
                constness,
                asyncness,
                generics,
                inputs,
                output,
                unsafety,
                ident,
                ..
            },
            block,
            ..
        } = self;

        *self = parse_quote! {
            #(#attrs)*
            #constness #asyncness #unsafety fn #ident #generics ( #inputs , __pinar__env__: Env ) -> JsResult<Option<Value>> {
                let fun = move || #output {
                    #block
                };
                fun().get_result(__pinar__env__)
            }
        };
    }
}

#[proc_macro_derive(Pinar)]
pub fn pinar_derive(input: TokenStream) -> TokenStream {
    let input: DeriveInput = parse_macro_input!(input as DeriveInput);
    let name = &input.ident;

    let expanded = quote! {
        impl<'e> ToJs<'e> for #name {
            type Value = JsObject<'e>;
            fn to_js(&self, env: Env) -> JsResult<JsObject<'e>> {
                Ok(JsObject::from(serialize_to_js(env, &self).unwrap()))
            }
        }

        impl FromArguments for #name {
            fn from_args(args: &Arguments) -> JsResult<Self> {
                match args.next_arg() {
                    Some(any) => {
                        pinar_serde::de::from_any(args.env(), any)
                            .map_err(|e| {
                                ArgumentsError::Deserialization(format!("{}", e)).into()
                            })
                    },
                    _ => Err(ArgumentsError::missing(args.arg_number()))
                }
            }
        }

        impl<'e> ToRust<#name> for JsObject<'e>
        {
            fn to_rust(&self) -> JsResult<#name> {
                pinar_serde::de::from_value(self.env(), self.get_value())
                    .map_err(|e| {
                        ArgumentsError::Deserialization(format!("{}", e)).into()
                    })
            }
        }
    };

    TokenStream::from(expanded)
}


use syn::visit_mut::{VisitMut};
use syn::{ItemFn, FnArg, PatType, ReturnType, Pat, Type, Attribute, Signature};

#[derive(Debug, PartialEq)]
enum MethodKind {
    Normal,
    Setter,
    Getter,
    Constructor
}

impl Default for MethodKind {
    fn default() -> Self { Self::Normal }
}

// #[derive(Debug)]
struct MethodDetail {
    name: String,
    helper_name: Option<String>,
    with_self: bool,
    self_mutable: bool,
    return_ref: bool,
    kind: MethodKind,
    node: ImplItemMethod
}

impl MethodDetail {
    fn new(name: String, node: ImplItemMethod) -> MethodDetail {
        MethodDetail {
            name,
            helper_name: None,
            node,
            with_self: false,
            self_mutable: false,
            return_ref: false,
            kind: Default::default()
        }
    }
}

impl std::fmt::Debug for MethodDetail {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        f.debug_struct("MethodKind")
            .field("name", &self.name)
            .field("with_self", &self.with_self)
            .field("self_mutable", &self.self_mutable)
            .field("return_ref", &self.return_ref)
            .field("kind", &self.kind)
            .finish()
    }
}

#[derive(Default, Debug)]
struct ExportVisitor {
    methods: Vec<MethodDetail>,
    helpers: Vec<(Ident, ImplItemMethod, bool)>
}

impl ExportVisitor {
    fn new(len: usize) -> ExportVisitor {
        ExportVisitor {
            methods: Vec::with_capacity(len),
            helpers: vec![]
        }
    }

    fn add_helpers(&self, input: &mut ItemImpl) {
        for (name, item, return_ref) in &self.helpers {

            let ImplItemMethod {
                attrs,
                sig: syn::Signature {
                    constness,
                    asyncness,
                    generics,
                    inputs,
                    unsafety,
                    ..
                },
                ..
            } = item;

            let name_helper = name.to_helper_name();

            let pats: Vec<_> = extract_args_pat(inputs).into_iter().map(|mut pat| {
                if let Pat::Ident(ref mut ident) = *pat {
                    ident.mutability = None;
                };
                pat
            }).collect();

//            println!("PATS: {:?}", pats);

            let inputs: Vec<&PatType> = inputs.iter().filter_map(|input| {
                if let FnArg::Typed(t) = input { Some(t) } else { None }
            }).collect();

            let fun = match return_ref {
                true => format_ident!("get_result_from_ref"),
                _ => format_ident!("get_result")
            };

            let a: ImplItemMethod = parse_quote! {
                #(#attrs)*
                #constness #asyncness #unsafety fn #name_helper #generics ( &mut self, #(#inputs,)* __pinar__env__: Env ) -> JsResult<Option<Value>> {
                    self.#name( #(#pats,)* )
                        .#fun(__pinar__env__)
                }
            };

            input.items.push(ImplItem::Method(a));
        }
    }
}

use syn::punctuated::{Punctuated};
use syn::token::{Colon2, Comma};

fn get_method_kind_helper(punc: &Punctuated<PathSegment, Colon2>) -> Option<MethodKind> {
    if !punc.is_empty() {
        let first = punc.first().unwrap();
        let last = punc.last().unwrap();

        if punc.len() == 1 || first.ident == "pinar" {
            if last.ident == "setter" {
                return Some(MethodKind::Setter);
            } else if last.ident == "getter" {
                return Some(MethodKind::Getter);
            } else if last.ident == "constructor" {
                return Some(MethodKind::Constructor);
            }
        }
    }
    None
}

fn get_method_kind(attrs: &[Attribute]) -> MethodKind {
    for attr in attrs {
        if let Some(kind) = get_method_kind_helper(&attr.path.segments) {
            return kind;
        };
    }
    MethodKind::Normal
}



fn get_lifetimes_inner(ty: &syn::Type, lifetimes: &mut Vec<Option<Lifetime>>) {
    use syn::Type::*;
    use syn::PathArguments::*;
    use syn::GenericArgument as GA;

    match ty {
        Array(ty) => { get_lifetimes_inner(&ty.elem, lifetimes); }
        Group(ty) => { get_lifetimes_inner(&ty.elem, lifetimes); }
        Paren(ty) => { get_lifetimes_inner(&ty.elem, lifetimes); }
        Slice(ty) => { get_lifetimes_inner(&ty.elem, lifetimes); }
        Reference(ty) => {
            lifetimes.push(ty.lifetime.clone());
            get_lifetimes_inner(&ty.elem, lifetimes);
        }
        Tuple(ty) => {
            for elem in &ty.elems {
                get_lifetimes_inner(elem, lifetimes);
            }
        }
        Path(ty) => {
            for seg in ty.path.segments.iter() {
                match &seg.arguments {
                    AngleBracketed(ab) => {
                        for arg in ab.args.iter() {
                            match arg {
                                GA::Lifetime(x) => { lifetimes.push(Some(x.clone())); }
                                GA::Type(x) => { get_lifetimes_inner(x, lifetimes); }
                                GA::Binding(x) => { get_lifetimes_inner(&x.ty, lifetimes); }
                                GA::Constraint(_x) => { unimplemented!() }
                                GA::Const(_) => { unimplemented!() }
                            }
                        }
                    }
                    Parenthesized(_p) => { unimplemented!() }
                    None => {}
                }
            }
        }
        // BareFn(TypeBareFn) => {}
        // ImplTrait(TypeImplTrait) => {}
        // Infer(TypeInfer) => {}
        // Macro(TypeMacro) => {}
        // Never(TypeNever) => {}
        // Ptr(TypePtr) => {}
        // Verbatim(TokenStream) => {}
        TraitObject(_type_trait_object) => { unimplemented!() }
        _ => {}
    };
}

fn get_lifetimes(output: &ReturnType) -> Option<Vec<String>> {


    let mut vec = vec![];

    match output {
        ReturnType::Default => None,
        ReturnType::Type(_, ty) => {
            get_lifetimes_inner(&ty, &mut vec);
            if vec.is_empty() {
                None
            } else {
                Some(vec.into_iter().map(|lifetime| {
                    lifetime.map(|v| format!("{}", v))
                            .unwrap_or_else(String::new)
                }).collect())
            }
        }
    }
}

fn get_lifetimes_input(sig: &Signature) -> String {
    use syn::FnArg::*;

    for input in &sig.inputs {
        match input {
            Receiver(r) => {
                if let Some((_, Some(ref r))) = r.reference {
                    return format!("{}", r);
                };
            }
            _ => {}
        }
    }
    String::new()
}

fn is_correct_lifetime(sig: &Signature, detail: &mut MethodDetail) -> bool {
    // println!("LIFETIMES: {} {:#?}", name, get_lifetimes(&item.sig.output));
    // println!("LIFETIMES INPUTS: {} {:#?}", name, get_lifetimes_input(&item.sig));

    let _name = sig.ident.to_string();

    let lifetimes = get_lifetimes(&sig.output);
    let lifetime_self = get_lifetimes_input(&sig);

    // println!("LIFETIMES: {} {:#?}", name, lifetimes);
    // println!("LIFETIMES INPUTS: {} {:#?}", name, lifetime_self);

    match lifetimes {
        Some(ref lifetimes) => {
            detail.return_ref = lifetimes.iter().all(|lifetime| {
                lifetime.as_str() == lifetime_self.as_str()
            });

            lifetimes.iter().all(|lifetime| {
                lifetime.as_str() == lifetime_self.as_str() || lifetime == "'static"
            })
        }
        _ => lifetime_self.is_empty()
    }
}

impl VisitMut for ExportVisitor {
    fn visit_impl_item_method_mut(&mut self, item: &mut ImplItemMethod) {
        let name = item.sig.ident.to_string();

        let mut detail = MethodDetail::new(name.clone(), item.clone());

        if let Some(FnArg::Receiver(receiver)) = item.sig.inputs.first() {
            detail.with_self = true;
            if receiver.mutability.is_some() {
                detail.self_mutable = true;
            }
        };

        // if let ReturnType::Type(_, ret) = &item.sig.output {
        //     if let Type::Reference(_) = &**ret {
        //         detail.return_ref = true;
        //     };
        // };

        detail.kind = if name == "constructor" {
            MethodKind::Constructor
        } else {
            get_method_kind(&item.attrs)
        };

        //if detail.kind != MethodKind::Constructor {
        if name != "constructor" {
            let _correct = is_correct_lifetime(&item.sig, &mut detail);
            //println!("CORRECT: {} {:#?}", name, correct);
        }

        if detail.kind != MethodKind::Constructor && (detail.return_ref || !detail.self_mutable) {
//            self.helpers.push((item.sig.ident.clone(), item.clone()));
            self.helpers.push((format_ident!("{}", name.to_lowercase()), item.clone(), detail.return_ref));
            detail.helper_name = Some(name.to_helper_name().to_string());
        }

        item.sig.ident = format_ident!("{}", name.to_lowercase());

        if detail.kind == MethodKind::Constructor {
            *item = parse_quote!( fn _dummy_(&self) {} );
        }

        self.methods.push(detail);
    }
}

fn extract_args_ty(inputs: &Punctuated<FnArg, Comma>) -> Vec<Box<Type>> {
    let mut vec = vec![];
    for input in inputs {
        match input {
            FnArg::Receiver(_r) => {
                unreachable!()
            },
            FnArg::Typed(arg) => vec.push(arg.ty.clone())
        }
    }
    vec
}

fn extract_args_pat(inputs: &Punctuated<FnArg, Comma>) -> Vec<Box<Pat>> {
    let mut vec = vec![];
    for input in inputs {
        match input {
            FnArg::Receiver(_r) => {
                //unreachable!()
            },
            FnArg::Typed(arg) => vec.push(arg.pat.clone())
        }
    }
    vec
}

fn filter_constructor(attrs: Vec<Attribute>) -> Vec<Attribute> {
    attrs.into_iter().filter(|attr| {
        !(get_method_kind_helper(&attr.path.segments) == Some(MethodKind::Constructor))
    }).collect()
}

// #[proc_macro_attribute]
// pub fn exportfn(attr: TokenStream, item: TokenStream) -> TokenStream {
//     let input: ItemFn = parse_macro_input!(item as ItemFn);

//     let name = &input.sig.ident;
//     let name_str = format!("{}", &input.sig.ident);

//     let ptr_init_fn = format_ident!("__PINAR_FN_{}", name);
//     let init_fn = format_ident!("__pinar_init_{}", name);

//     // TODO: Check input
//     // No self in params

//     (quote! {
//         #[distributed_slice(PINAR_FUNCTIONS)]
//         static #ptr_init_fn: fn(&mut ModuleBuilder) = #init_fn;

//         fn #init_fn(builder: &mut ModuleBuilder) {
//             builder.with_function(#name_str, #name);
//         }

//         #input
//     }).into()
// }

fn export_fn(input: &ItemFn) -> TokenStream {
    let name = &input.sig.ident;
    let name_str = format!("{}", &input.sig.ident);

    let init_fn = format_ident!("__PINAR_FN_{}", name.to_string().to_uppercase());

    // TODO: Check input
    // No self in params

    (quote! {
        #[distributed_slice(PINAR_FUNCTIONS)]
        static #init_fn: fn(&mut ModuleBuilder) = {
            fn init(builder: &mut ModuleBuilder) {
                builder.with_function(#name_str, #name);
            }
            init
        };

        #input
    }).into()
}

use syn::export::TokenStream2;

fn make_constructor(
    methods: &[MethodDetail]
) -> (TokenStream2, TokenStream2, TokenStream2)
{
    if let Some(cons) = methods.iter().find(|m| m.kind == MethodKind::Constructor) {
        let node = &cons.node;

        let ImplItemMethod {
            attrs,
            sig: syn::Signature {
                generics,
                inputs,
                output,
                ..
            },
            block,
            ..
        } = node;

        let attrs = filter_constructor(attrs.to_vec());
        let args_ty = extract_args_ty(inputs);
        let args_pat = extract_args_pat(inputs);

        let args_ty = quote!( #(#args_ty),* );
        let args_pat = quote!( #(#args_pat),* );
        let constructor = quote!(
            #(#attrs)*
            fn constructor #generics( ( #args_pat ) : Self::ArgsConstructor ) #output #block
        );

        (constructor, args_ty, args_pat)
    } else {
        (quote!(), quote!(), quote!())
    }
}

fn process_class(input: &mut ItemImpl) -> TokenStream {
    let name = match &*input.self_ty {
        Type::Path(TypePath { path, .. }) => path.get_ident().unwrap().clone(),
        _ => unreachable!()
    };

    let mut visitor = ExportVisitor::new(input.items.len());

    visitor.visit_item_impl_mut(input);
    visitor.add_helpers(input);

    // println!("RESULT: {:#?}", visitor);

    let name_str = name.to_string();

    let normals = visitor.methods
                         .iter()
                         .filter(|m| m.kind == MethodKind::Normal)
                         .collect::<Vec<_>>();

    let idents_str = normals.iter().map(|m| m.name.as_str());
    let idents = normals.iter().map(|m| format_ident!("{}", m.helper_name.as_ref().unwrap_or(&m.name).to_lowercase()));

    // let idents_str = normals.iter().map(|m| m.name.as_str());
    // let idents = normals.iter().map(|m| format_ident!("{}", &m.name));

    let (constructor, args, _pats) = make_constructor(&visitor.methods);

    let init_fn = format_ident!("__PINAR_FN_{}", name.to_string().to_uppercase());

    (quote! {
        impl JsClass for #name {
            const CLASSNAME: &'static str = #name_str;
            type ArgsConstructor = ( #args );

            #constructor

            fn default_properties(builder: ClassBuilder<Self>) -> ClassBuilder<Self> {
                builder
                    #( .with_method(#idents_str, Self::#idents) )*
            }
        }

        #[distributed_slice(PINAR_CLASSES)]
        static #init_fn: fn(&mut ModuleBuilder) = {
            fn init(builder: &mut ModuleBuilder) {
                builder.with_class::<#name>();
            }
            init
        };

        #input
    }).into()
}

fn process(_attr: (), input: &mut Item) -> Result<TokenStream, SetGetError> {
    match input {
        syn::Item::Fn(fun) => {
            Ok(export_fn(fun))
        }
        syn::Item::Impl(ref mut class) => {
            Ok(process_class(class))
        }
        _ => {
            unreachable!();
            // Unsuported
        }
    }
}

#[proc_macro_attribute]
pub fn pinar(_attr: TokenStream, input: TokenStream) -> TokenStream {
    let mut input: Item = parse_macro_input!(input as Item);

    match process((), &mut input) {
        Ok(input) => input,
        Err(_e) => unimplemented!()
    }
}

// #[proc_macro_attribute]
// pub fn export(attr: TokenStream, item: TokenStream) -> TokenStream {
//     let item_clone = item.clone();
//     let input: ItemImpl = parse_macro_input!(item_clone as ItemImpl);

//     let name = match &*input.self_ty {
//         Type::Path(TypePath { path, .. }) => path.get_ident().unwrap(),
//         _ => unreachable!()
//     };

//     let mut visitor = ExportVisitor::new(input.items.len());

//     // visitor.visit_item_impl(&input);

//     // println!("RESULT: {:#?}", visitor);

//     let name_str = name.to_string();

//     let normals = visitor.methods.iter().filter(|m| m.kind == MethodKind::Normal).collect::<Vec<_>>();
//     let idents_str = normals.iter().map(|m| m.name.as_str());
//     let idents = normals.iter().map(|m| format_ident!("{}", &m.name));

//     let (constructor, args, pats) = if let Some(cons) = visitor.methods.iter().find(|m| m.kind == MethodKind::Constructor) {
//         let node = &cons.node;

//         let ImplItemMethod {
//             attrs,
//             sig: syn::Signature {
//                 generics,
//                 inputs,
//                 output,
//                 ..
//             },
//             block,
//             ..
//         } = node;

//         let attrs = filter_constructor(attrs.to_vec());
//         let args_ty = extract_args_ty(inputs);
//         let args_pat = extract_args_pat(inputs);

//         let args_ty = quote!( #(#args_ty),* );
//         let args_pat = quote!( #(#args_pat),* );
//         let output = quote!(
//             #(#attrs)*
//             fn constructor #generics( ( #args_pat ) : Self::ArgsConstructor ) #output #block
//         );

//         (output, args_ty, args_pat)
//     } else {
//         (quote!(), quote!(), quote!())
//     };

//     // let init_fn = format_ident!("__pinar_init_{}", name);
//     //let init_fn_ptr = format_ident!("__PINAR_FN_{}", name);
//     let init_fn_ptr = format_ident!("__PINAR_FN_{}", name.to_string().to_uppercase());

//     (quote! {
//         impl JsClass for #name {
//             const CLASSNAME: &'static str = #name_str;
//             type ArgsConstructor = ( #args );

//             #constructor

//             fn default_properties(builder: ClassBuilder<Self>) -> ClassBuilder<Self> {
//                 builder
//                     #( .with_method(#idents_str, Self::#idents) )*
//             }
//         }

//         #[distributed_slice(PINAR_CLASSES)]
//         static #init_fn_ptr: fn(&mut ModuleBuilder) = {
//             fn init(builder: &mut ModuleBuilder) {
//                 builder.with_class::<#name>();
//             }
//             init
//         };

//         // #[distributed_slice(PINAR_CLASSES)]
//         // static #init_fn_ptr: fn(&mut ModuleBuilder) = #init_fn;

//         // fn #init_fn(builder: &mut ModuleBuilder) {
//         //     builder.with_class::<#name>();
//         // }

//         #input
//     }).into()
// }

//mod helper;

#[derive(Debug)]
enum SetGetError {
    Setter,
    SetterMutability,
    Getter,
    GetterMutability,
    GetterReturn(Span)
}

impl Into<&'static str> for SetGetError {
    fn into(self) -> &'static str {
        use SetGetError::*;
        match self {
            Setter => "setters must receive 2 arguments: (&mut self, ..)",
            SetterMutability => "setters must receive a mutable self: (&mut self, ..)",
            Getter => "getters must receive 1 argument: (&self, ..)",
            GetterMutability => "getters must receive an immutable self: (&self, ..)",
            GetterReturn(_) => "getters must return a value"
        }
    }
}

fn check_inout(kind: MethodKind, sig: &Signature) -> Option<SetGetError> {
    use MethodKind::*;

    let inputs = &sig.inputs;

    match kind {
        Setter => {
            if inputs.len() < 2 {
                return Some(SetGetError::Setter)
            }
            if let Some(FnArg::Receiver(receiver)) = inputs.first() {
                if receiver.mutability.is_some() {
                    return None;
                }
            }
            return Some(SetGetError::SetterMutability);
        },
        Getter => {
            if inputs.is_empty() {
                return Some(SetGetError::Getter)
            }

            if let ReturnType::Default = &sig.output {
                return Some(SetGetError::GetterReturn(sig.span()));
            };

            if let ReturnType::Type(_, ret) = &sig.output {
                if let Type::Tuple(tuple) = &**ret {
                    if tuple.elems.is_empty() {
                        return Some(SetGetError::GetterReturn(ret.span()));
                    }
                };
            };

            if let Some(FnArg::Receiver(receiver)) = inputs.first() {
                if receiver.mutability.is_some() {
                    return Some(SetGetError::GetterMutability)
                }
            }
        },
        _ => {},
    }

    None
}

use syn::export::Span;

use syn::spanned::Spanned;

fn check_setget(kind: MethodKind, item: TokenStream) -> TokenStream {
    let item_clone = item.clone();
    let input: ImplItemMethod = parse_macro_input!(item_clone as ImplItemMethod);

    let result = check_inout(kind, &input.sig);

    let span = match result {
        Some(SetGetError::GetterReturn(span)) => span,
        _ => input.sig.paren_token.span
    };

    if let Some(err) = result {
        return syn::Error::new::<&str>(span, err.into()).to_compile_error().into();
    };

    item
}

#[proc_macro_attribute]
pub fn setter(_attr: TokenStream, item: TokenStream) -> TokenStream {
    check_setget(MethodKind::Setter, item)
}

#[proc_macro_attribute]
pub fn getter(_attr: TokenStream, item: TokenStream) -> TokenStream {
    check_setget(MethodKind::Getter, item)
}

#[proc_macro_attribute]
pub fn constructor(_attr: TokenStream, _item: TokenStream) -> TokenStream {
    TokenStream::new()
}