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
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
#![recursion_limit = "256"]
extern crate proc_macro;
extern crate syn;

use proc_macro::TokenStream;
use proc_macro2::TokenStream as Tokens;
use proc_macro2::{Delimiter, Ident, Span, TokenTree};
use std::collections::{BTreeMap, HashSet};
use std::process::Command;
use syn::Meta::{List, Word};
use syn::NestedMeta::Meta;
use syn::{Data, DeriveInput, Fields, GenericParam, Generics};

#[macro_use]
extern crate quote;

struct DeriveInfo {
    from_interact: bool,
    opaque: bool,
    basic: bool,
}

/// # Attributes that Interact derive macro supports
///
/// ## Type context:
///
/// ```ignore
/// #[interact(mut_fn(function_name(param_a, param_b)))
/// #[interact(immut_fn(function_name(param_a, param_b)))
/// ```
///
/// ### Per field:
///
/// The ignore attribute allows to make some fields invisible:
/// ```ignore
/// #[interact(ignore))
/// ```
///
/// The downside is that having any ignored field on a type means that it is unbuildable, and
/// therefore cannot be passed as value to functions or to be assigned using `=` in
/// an expression.
#[proc_macro_derive(Interact, attributes(interact))]
pub fn derive_interact(input: TokenStream) -> TokenStream {
    derive_interact_inner(
        input,
        DeriveInfo {
            from_interact: false,
            opaque: false,
            basic: false,
        },
    )
}

#[proc_macro]
pub fn derive_interact_prelude(input: TokenStream) -> TokenStream {
    derive_interact_inner(
        input,
        DeriveInfo {
            from_interact: true,
            opaque: false,
            basic: true,
        },
    )
}

#[proc_macro]
pub fn derive_interact_opaque(input: TokenStream) -> TokenStream {
    derive_interact_inner(
        input,
        DeriveInfo {
            from_interact: true,
            opaque: true,
            basic: false,
        },
    )
}

#[proc_macro]
pub fn derive_interact_extern_opqaue(input: TokenStream) -> TokenStream {
    derive_interact_inner(
        input,
        DeriveInfo {
            from_interact: false,
            opaque: true,
            basic: false,
        },
    )
}

#[proc_macro]
pub fn derive_interact_basic(input: TokenStream) -> TokenStream {
    derive_interact_inner(
        input,
        DeriveInfo {
            from_interact: true,
            opaque: true,
            basic: true,
        },
    )
}

fn derive_interact_inner(input: TokenStream, info: DeriveInfo) -> TokenStream {
    let input: DeriveInput = syn::parse(input).unwrap();
    let name = &input.ident;

    let expanded = inner_derive_interact(&input, info);

    if let Some((_, value)) =
        std::env::vars().find(|(key, _)| key.as_str() == "INTERACT_DERIVE_SAVE_DIR")
    {
        let dir = std::path::Path::new(value.as_str());
        tokens_to_rustfmt_file(&dir.join(format!("interact_derive_{}.rs", name)), &expanded);
    }

    expanded.into()
}

fn inner_derive_interact(input: &DeriveInput, info: DeriveInfo) -> Tokens {
    let name = &input.ident;
    let kr = if info.from_interact {
        quote! { crate }
    } else {
        quote! { crate::interact }
    };

    let (mut_assign, deser_impls) = impls_for_deser(&kr, &input, &info);
    let access_impls = impls_for_access(&input, mut_assign);
    let reflect_impls = impls_for_reflect(&input, &info);
    let uses = if info.basic {
        quote! {}
    } else {
        quote! {use super::#name;}
    };
    let module_name = Ident::new(&format!("impls_for_{}", name), Span::call_site());

    quote! {
        mod #module_name {
            #uses

            use #kr::*;

            #access_impls
            #reflect_impls
            #deser_impls
        }
    }
}

#[derive(Debug)]
struct Function {
    mutability: Mutability,
    name: String,
    args: Vec<String>,
}

type FuncMap = BTreeMap<String, Function>;

fn fill_skip_bound_from_attr(attribute: Option<TokenTree>, skip_bound_set: &mut HashSet<String>) {
    let tt = if let Some(tt) = attribute {
        tt
    } else {
        panic!("Expected a token tree after skip_bound term");
    };

    if let TokenTree::Group(group) = tt {
        let mut ts = group.stream().into_iter();
        if let Some(TokenTree::Ident(s)) = ts.next() {
            skip_bound_set.insert(s.to_string());
        } else {
            panic!("Expected term in attribute");
        }
    }
}

fn get_attr_info(
    attribute: &syn::Attribute,
    map: &mut FuncMap,
    skip_bound_set: &mut HashSet<String>,
    mut_assign: &mut bool,
) {
    for func in attribute.tts.clone().into_iter() {
        let ts = match &func {
            TokenTree::Literal { .. } | TokenTree::Punct { .. } => {
                // Skip comments
                continue;
            }
            TokenTree::Group(group) if group.delimiter() == Delimiter::Parenthesis => {
                group.stream()
            }
            _ => {
                panic!("extend () in `interact` data type attribute `{:?}`", func);
            }
        };

        let mut ts = ts.clone().into_iter();
        let mutability_term = if let Some(TokenTree::Ident(term)) = ts.next() {
            term.to_string()
        } else {
            panic!("expected mut_fn/immut_fn term in data type attribute")
        };

        let mutability = match mutability_term.as_str() {
            "mut_fn" => Mutability::ModifyAccess,
            "immut_fn" => Mutability::ReadAccess,
            "skip_bound" => {
                fill_skip_bound_from_attr(ts.next(), skip_bound_set);
                continue;
            }
            "mut_assign" => {
                *mut_assign = true;
                continue;
            }
            _ => panic!(
                "Invalid term {} in `{}`",
                mutability_term.as_str(),
                attribute.tts.to_string()
            ),
        };

        let fn_def = if let Some(fn_def) = ts.next() {
            fn_def
        } else {
            panic!(
                "Expected a token tree after mutability term in `{}`",
                attribute.tts.to_string()
            );
        };

        if let TokenTree::Group(group) = fn_def {
            if group.delimiter() == Delimiter::Parenthesis {
                let mut ts = group.stream().into_iter();

                let fn_name = if let Some(TokenTree::Ident(term)) = ts.next() {
                    term.to_string()
                } else {
                    panic!("Expected term in attribute `{}`", attribute.tts.to_string());
                };

                let name = String::from(fn_name.as_str());
                let mut func = Function {
                    name: name.clone(),
                    mutability,
                    args: vec![],
                };

                let fn_def = if let Some(fn_def) = ts.next() {
                    fn_def
                } else {
                    panic!(
                        "Expected parameter specification () after {} in `{}`",
                        fn_name.as_str(),
                        attribute.tts.to_string()
                    );
                };

                if let TokenTree::Group(group) = fn_def {
                    if group.delimiter() == Delimiter::Parenthesis {
                        for tt in group.stream().into_iter() {
                            if let TokenTree::Ident(term) = tt {
                                func.args.push(term.to_string());
                            } else if let TokenTree::Punct(ref punc) = tt {
                                if punc.as_char() == ',' {
                                    continue;
                                } else {
                                    panic!("Unexpected parameter token {:?}", tt);
                                }
                            } else {
                                panic!(
                                    "Expected term in attribute `{}`",
                                    attribute.tts.to_string()
                                );
                            }
                        }
                    }
                }

                if map.get(&name).is_some() {
                    panic!("Duplicate name {}", name.as_str());
                }

                map.insert(name, func);
            }
        }
    }
}

fn call_impls(fnmap: &FuncMap, mutability: Mutability) -> (Tokens, Vec<Tokens>) {
    let mut arms = vec![];
    let mut descs = vec![];

    for func in fnmap.values() {
        let name = &func.name;
        let name_ident = Ident::new(name.as_str(), Span::call_site());
        let mut match_vec = vec![];
        let mut arg_vec = vec![];
        let mut arg_vec_unpack = vec![];
        let mut arg_str_vec = vec![];

        for arg in &func.args {
            arg_str_vec.push(arg.as_str());
            match_vec.push({
                let v = syn::token::Underscore::default();
                if func.args.len() == 1 {
                    quote! { #v, }
                } else {
                    quote! { #v }
                }
            });
            arg_vec.push(Ident::new(arg.as_str(), Span::call_site()));
            arg_vec_unpack.push({
                let v = Ident::new(arg.as_str(), Span::call_site());
                if func.args.len() == 1 {
                    quote! { #v, }
                } else {
                    quote! { #v }
                }
            });
        }
        let arg_vec_ref = &arg_vec;
        let arg_vec_unpack_ref = &arg_vec_unpack;
        let match_vec = &match_vec;

        let mut call_impl = quote! {
            <(#(#match_vec),*)>::deser(&mut _climber.borrow_tracker()).map(|(#(#arg_vec_unpack_ref),*)| {
                if !_climber.is_probe_only() {
                    let _retval = self.#name_ident(#(#arg_vec_ref),*);
                    (_retcall)(&_retval, _climber);
                }
            }).map_err(CallError::Deser)
        };
        if mutability == Mutability::ReadAccess {
            if func.mutability == Mutability::ModifyAccess {
                call_impl = quote! {
                    <(#(#match_vec),*)>::deser(&mut _climber.borrow_tracker()).map(|(#(#arg_vec_unpack_ref),*)| {
                        if !_climber.is_probe_only() {
                            // Non executing unsafe code only for coercing type checking
                            if false {
                                let mself : &mut Self = unsafe { std::mem::uninitialized() };
                                let _ = mself.#name_ident(#(#arg_vec_ref),*);
                            }
                        }
                    }).map_err(CallError::Deser)?;

                    return Err(CallError::NeedMutable);
                };
            }
        }

        arms.push(quote! {
            #name => {
                #call_impl
            }
        });

        descs.push(quote! {
            Function {
                name: #name,
                args: &[#(#arg_str_vec),*],
            }
        });
    }

    let arms = if arms.len() != 0 {
        quote! { #(#arms),* }
    } else {
        quote! {}
    };

    (
        quote! {
            match func_name {
                #arms
                _ => return Err(CallError::NoSuchFunction),
            }
        },
        descs,
    )
}

fn impls_for_access(input: &DeriveInput, mut mut_assign: bool) -> Tokens {
    let name = &input.ident;
    let mut skip_bound_set = HashSet::new();
    let mut fnmap = BTreeMap::new();

    for attribute in &input.attrs {
        get_attr_info(attribute, &mut fnmap, &mut skip_bound_set, &mut mut_assign);
    }
    let generics = add_trait_bounds(
        input.generics.clone(),
        &skip_bound_set,
        &["Access", "Deser"],
    );
    let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();

    let code = {
        quote! { Reflect::Direct(self) }
    };
    let code_mut = {
        quote! { ReflectMut::Direct(self) }
    };

    let (immut_call_impls, immut_call_desc) = call_impls(&fnmap, Mutability::ReadAccess);
    let (mut_call_impls, mut_call_desc) = call_impls(&fnmap, Mutability::ModifyAccess);

    let mut_assign_code = if mut_assign {
        quote! {
            fn mut_assign<'a, 'b>(
                &mut self,
                _tracker: &mut deser::Tracker<'a, 'b>,
                _probe_only: bool,
            ) -> Result<(), AssignError> {
                deser_assign(self, _tracker, _probe_only)
            }
        }
    } else {
        quote! {}
    };

    quote! {
        impl #impl_generics Access for #name #ty_generics #where_clause {
            fn immut_access(&self) -> ImmutAccess {
                ImmutAccess {
                    reflect: #code,
                    functions: &[#(#immut_call_desc),*],
                }
            }

            fn mut_access(&mut self) -> MutAccess {
                MutAccess {
                    reflect: #code_mut,
                    functions: &[#(#mut_call_desc),*],
                }
            }

            fn immut_call<'a>(&self, func_name: &'static str,
                              _climber: &mut Climber<'a>,
                              mut _retcall: RetValCallback<'a>) -> Result<(), CallError> {
                #immut_call_impls
            }

            fn mut_call<'a>(&mut self, func_name: &'static str,
                            _climber: &mut Climber<'a>,
                            mut _retcall: RetValCallback<'a>) -> Result<(), CallError> {
                #mut_call_impls
            }

            #mut_assign_code
        }
    }
}

#[derive(Debug, Eq, PartialEq)]
enum Mutability {
    ModifyAccess,
    ReadAccess,
}

fn impls_by_mutability(
    name: syn::Ident,
    data_fields: &Fields,
    in_enum: bool,
    mtype: Mutability,
) -> (Tokens, Tokens, Tokens, Tokens) {
    let mut named_match_arms: Vec<Tokens> = vec![];
    let mut index_match_arms: Vec<Tokens> = vec![];
    let desc: Tokens;
    let name = format!("{}", format!("{}", name));
    let params;

    let qmut = match mtype {
        Mutability::ReadAccess => quote! {},
        Mutability::ModifyAccess => quote! { mut },
    };

    match data_fields {
        Fields::Named(ref fields) => {
            let mut idents: Vec<syn::Ident> = vec![];
            let fnames: Vec<_> = fields
                .named
                .iter()
                .filter(|f| !is_skipped(&f.attrs))
                .map(|f| {
                    let ident = &f.ident;
                    idents.push(ident.as_ref().unwrap().clone());

                    let f_i = if in_enum {
                        quote! { #ident }
                    } else {
                        quote! { & #qmut self.#ident }
                    };

                    (f_i, format!("{}", ident.as_ref().unwrap()))
                })
                .collect();

            let fnames_stringified: Vec<_> =
                fnames.iter().map(|(_, ref x)| quote! { #x }).collect();

            desc = quote! {
                Struct {
                    name: #name,
                    kind: StructKind::Fields(&[#(#fnames_stringified),*])
                }
            };

            named_match_arms = fnames
                .iter()
                .map(|(ref y, ref x)| {
                    quote! {
                        #x => Some(#y)
                    }
                })
                .collect();
            params = quote! {{#(#idents),*}};
        }
        Fields::Unnamed(ref fields) => {
            let mut idents: Vec<Tokens> = vec![];
            let mut i: usize = 0;

            let fnames: Vec<_> = fields
                .unnamed
                .iter()
                .filter(|f| !is_skipped(&f.attrs))
                .map(|_| {
                    let f_i = if in_enum {
                        let ident = Ident::new(&format!("f_{}", i), Span::call_site());
                        quote! { #ident }
                    } else {
                        quote! { & #qmut self.#i }
                    };

                    idents.push(f_i.clone());

                    let r = (quote! { #i }, f_i);
                    i += 1;
                    r
                })
                .collect();

            let n = fnames.len();
            desc = quote! {
                Struct{
                    name: #name,
                    kind: StructKind::Tuple(#n)
                }
            };

            index_match_arms = fnames
                .iter()
                .map(|(idx, ref x)| {
                    quote! { #idx => Some(#x) }
                })
                .collect();
            params = quote! {(#(#idents),*)};
        }
        Fields::Unit => {
            desc = quote! {
                Struct {
                    name: #name,
                    kind: StructKind::Unit,
                }
            };
            params = quote! {};
        }
    }

    let named_match_arms = if named_match_arms.len() != 0 {
        quote! { #(#named_match_arms),*, }
    } else {
        quote! {}
    };

    let index_match_arms = if index_match_arms.len() != 0 {
        quote! { #(#index_match_arms),*, }
    } else {
        quote! {}
    };

    (named_match_arms, index_match_arms, desc, params)
}

fn impls_for_reflect(input: &DeriveInput, info: &DeriveInfo) -> Tokens {
    if info.opaque {
        return quote! {};
    }

    let name = &input.ident;
    let generics = add_trait_bounds(input.generics.clone(), &HashSet::new(), &["Access"]);
    let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
    let str_name = format!("{}", format!("{}", name));

    match &input.data {
        Data::Struct(data) => {
            let (named_match_arms, index_match_arms, desc, _) =
                impls_by_mutability(name.clone(), &data.fields, false, Mutability::ReadAccess);
            let (named_match_mut_arms, index_match_mut_arms, _, _) =
                impls_by_mutability(name.clone(), &data.fields, false, Mutability::ModifyAccess);

            quote! {
                impl #impl_generics ReflectStruct for #name #ty_generics #where_clause {
                    fn get_desc(&self) -> Struct {
                        #desc
                    }

                    fn get_field_by_name(&self, name: &'static str) -> Option<&dyn Access> {
                        match name {
                            #named_match_arms
                            _ => None,
                        }
                    }

                    fn get_field_by_idx(&self, idx: usize) -> Option<&dyn Access> {
                        match idx {
                            #index_match_arms
                            _ => None,
                        }
                    }

                    fn get_field_by_name_mut(&mut self, name: &'static str) -> Option<&mut dyn Access> {
                        match name {
                            #named_match_mut_arms
                            _ => None,
                        }
                    }

                    fn get_field_by_idx_mut(&mut self, idx: usize) -> Option<&mut dyn Access> {
                        match idx {
                            #index_match_mut_arms
                            _ => None,
                        }
                    }
                }

                impl #impl_generics ReflectDirect for #name #ty_generics #where_clause {
                    fn immut_reflector(&self, reflector: &std::sync::Arc<Reflector>) -> NodeTree {
                        Reflector::reflect_struct(reflector, &self.get_desc(), self, false)
                    }

                    fn immut_climber<'a>(
                        &self,
                        climber: &mut Climber<'a>,
                    ) -> Result<Option<NodeTree>, ClimbError> {
                        climber.check_field_access_immut(&EnumOrStruct::Struct(self))
                    }

                    fn mut_climber<'a>(
                        &mut self,
                        climber: &mut Climber<'a>,
                    ) -> Result<Option<NodeTree>, ClimbError> {
                        climber.check_field_access_mut(EnumOrStructMut::Struct(self))
                    }
                }
            }
        }
        Data::Enum(data) => {
            let mut field_by_name_match_arms = vec![];
            let mut field_by_idx_match_arms = vec![];
            let mut field_by_name_match_mut_arms = vec![];
            let mut field_by_idx_match_mut_arms = vec![];
            let mut desc_match_arms = vec![];
            let mut opt_names = vec![];

            for variant in data.variants.iter() {
                let ident = &variant.ident;
                let variant_name = format!("{}", variant.ident);
                opt_names.push(variant_name);

                let (named_match_arms, index_match_arms, desc, params) = impls_by_mutability(
                    variant.ident.clone(),
                    &variant.fields,
                    true,
                    Mutability::ReadAccess,
                );
                let (named_match_mut_arms, index_match_mut_arms, _, _) = impls_by_mutability(
                    variant.ident.clone(),
                    &variant.fields,
                    true,
                    Mutability::ModifyAccess,
                );

                field_by_name_match_arms.push(quote! {
                    #name::#ident #params => {
                        match name {
                            #named_match_arms
                            _ => None,
                        }
                    }
                });

                field_by_idx_match_arms.push(quote! {
                    #name::#ident #params => {
                        match idx {
                            #index_match_arms
                            _ => None,
                        }
                    }
                });

                field_by_name_match_mut_arms.push(quote! {
                    #name::#ident #params => {
                        match name {
                            #named_match_mut_arms
                            _ => None,
                        }
                    }
                });

                field_by_idx_match_mut_arms.push(quote! {
                    #name::#ident #params => {
                        match idx {
                            #index_match_mut_arms
                            _ => None,
                        }
                    }
                });

                desc_match_arms.push(quote! {
                    #name::#ident #params => {
                        #desc
                    }
                });
            }

            let field_by_name_match_arms = if field_by_name_match_arms.len() != 0 {
                quote! { #(#field_by_name_match_arms),*, }
            } else {
                quote! { "_ => None"}
            };

            let field_by_idx_match_arms = if field_by_idx_match_arms.len() != 0 {
                quote! { #(#field_by_idx_match_arms),*, }
            } else {
                quote! { "_ => None"}
            };

            let field_by_name_match_mut_arms = if field_by_name_match_mut_arms.len() != 0 {
                quote! { #(#field_by_name_match_mut_arms),*, }
            } else {
                quote! { "_ => None"}
            };

            let field_by_idx_match_mut_arms = if field_by_idx_match_mut_arms.len() != 0 {
                quote! { #(#field_by_idx_match_mut_arms),*, }
            } else {
                quote! { "_ => None"}
            };

            let desc_match_arms = if desc_match_arms.len() != 0 {
                quote! { #(#desc_match_arms),*, }
            } else {
                quote! { "_ => panic!()"}
            };

            quote! {
                impl #impl_generics ReflectStruct for #name #ty_generics #where_clause {
                    fn get_desc(&self) -> Struct {
                        match self {
                            #desc_match_arms
                        }
                    }

                    fn get_field_by_name(&self, name: &'static str) -> Option<&dyn Access> {
                        match self {
                            #field_by_name_match_arms
                        }
                    }

                    fn get_field_by_idx(&self, idx: usize) -> Option<&dyn Access> {
                        match self {
                            #field_by_idx_match_arms
                        }
                    }

                    fn get_field_by_name_mut(&mut self, name: &'static str) -> Option<&mut dyn Access> {
                        match self {
                            #field_by_name_match_mut_arms
                        }
                    }

                    fn get_field_by_idx_mut(&mut self, idx: usize) -> Option<&mut dyn Access> {
                        match self {
                            #field_by_idx_match_mut_arms
                        }
                    }
                }

                impl #impl_generics ReflectEnum for #name #ty_generics #where_clause {
                    fn get_variant_desc(&self) -> Enum {
                        Enum {
                            name: #str_name,
                            opts: &[#(#opt_names),*],
                        }
                    }

                    fn get_variant_struct(&self) -> &dyn ReflectStruct {
                        self
                    }

                    fn get_variant_struct_mut(&mut self) -> &mut dyn ReflectStruct {
                        self
                    }
                }

                impl #impl_generics ReflectDirect for #name #ty_generics #where_clause {
                    fn immut_reflector(&self, reflector: &std::sync::Arc<Reflector>)
                        -> NodeTree
                    {
                        let p_struct = self.get_variant_struct();
                        let desc = p_struct.get_desc();
                        Reflector::reflect_struct(reflector, &desc, self, false)
                    }

                    fn immut_climber<'a>(
                        &self,
                        climber: &mut Climber<'a>,
                    ) -> Result<Option<NodeTree>, ClimbError> {
                        climber.check_field_access_immut(&EnumOrStruct::Enum(self))
                    }

                    fn mut_climber<'a>(
                        &mut self,
                        climber: &mut Climber<'a>,
                    ) -> Result<Option<NodeTree>, ClimbError> {
                        climber.check_field_access_mut(EnumOrStructMut::Enum(self))
                    }
                }
            }
        }
        _ => {
            panic!();
        }
    }
}

fn impl_struct_for_deser(name: Tokens, data_fields: &Fields, in_enum: bool) -> (Tokens, bool) {
    let name_str = format!("{}", format!("{}", name));
    let parse_name = if in_enum {
        quote! {}
    } else {
        quote! {
            let _name = Token::new_borrowed(TokenKind::Ident, #name_str);
            tracker.try_token(&_name)?;
        }
    };

    match data_fields {
        Fields::Named(ref fields) => {
            let mut match_arms = vec![];
            let mut match_arms_2 = vec![];
            let mut lets = vec![];
            let mut assigns = vec![];
            let mut names = vec![];

            for field in fields.named.iter() {
                if is_skipped(&field.attrs) {
                    return (
                        quote! {
                            return Err(deser::DeserError::Unbuildable);
                        },
                        false,
                    );
                }
                let ident = &field.ident;
                let ident_name = Ident::new(
                    &format!("_assign_{}", ident.as_ref().unwrap()),
                    Span::call_site(),
                );
                let ident_str = format!("{}", ident.as_ref().unwrap());

                lets.push(quote! {
                    let mut #ident_name = None;
                });
                match_arms.push(quote! { #ident_str => Some(#ident_str) });
                match_arms_2.push(quote! {
                    Some(#ident_str) => {
                        if #ident_name.is_some() {
                            return Err(deser::DeserError::Unbuildable);
                        }
                        tracker.step();
                        tracker.try_token(&_colon)?;
                        #ident_name = Some(Deser::deser(tracker)?);
                    }
                });
                names.push(quote! { (#ident_str, #ident_name.is_some()) });
                assigns.push(quote! { #ident : #ident_name.unwrap() });
            }

            let expecting = assigns.len();
            let assigns = if assigns.is_empty() {
                quote! {}
            } else {
                quote! { #(#assigns),* }
            };

            let processing = if expecting > 0 {
                let names = &names;
                quote! {
                    #(#lets);*
                    let mut __expecting = #expecting;
                    while __expecting > 0 {
                        if !tracker.has_remaining() {
                            for (name, exclude) in &[#(#names,)*] {
                                if *exclude {
                                    continue;
                                }
                                tracker.possible_token(
                                    Token::new_borrowed(TokenKind::Ident, *name));
                            }
                            return Err(deser::DeserError::EndOfTokenList);
                        }
                        if let TokenKind::Ident = tracker.top_kind() {
                            let opt = {
                                let text = tracker.top().text.as_ref();
                                match text {
                                    #(#match_arms),*,
                                    _ => None
                                }
                            };
                            match opt {
                                #(#match_arms_2),*,
                                _ => {
                                    let text = String::from(tracker.top().text.as_ref());
                                    for (name, exclude) in &[#(#names,)*] {
                                        if *exclude {
                                            continue;
                                        }
                                        if name.starts_with(&text) {
                                            tracker.possible_token(
                                                Token::new_borrowed(
                                                    TokenKind::Ident, *name));
                                        }
                                    }
                                    return Err(deser::DeserError::UnexpectedToken);
                                }
                            }
                            __expecting -= 1;
                            if __expecting == 0 {
                                tracker.try_token(&_curly_close)?;
                            } else {
                                tracker.try_token(&_comma)?;
                            }
                        } else {
                            return Err(deser::DeserError::UnexpectedToken);
                        }
                    }
                }
            } else {
                quote! {
                    tracker.try_token(&_culry_close)?;
                }
            };

            (
                quote! {
                    let _curly_open = Token::new_borrowed(TokenKind::CurlyOpen, " {");
                    let _curly_close = Token::new_borrowed(TokenKind::CurlyClose, "}");
                    let _comma = Token::new_borrowed(TokenKind::Comma, ", ");
                    let _colon = Token::new_borrowed(TokenKind::Colon, ": ");

                    #parse_name
                    tracker.try_token(&_curly_open)?;
                    #processing;

                    Ok(#name { #assigns })
                },
                true,
            )
        }
        Fields::Unnamed(ref fields) => {
            let mut assigns = vec![];

            for (idx, field) in fields.unnamed.iter().enumerate() {
                if is_skipped(&field.attrs) {
                    return (
                        quote! {
                            return Err(deser::DeserError::Unbuildable);
                        },
                        false,
                    );
                }

                let comma = if idx > 0 {
                    quote! { tracker.try_token(&_comma)?; }
                } else {
                    quote! {}
                };

                assigns.push(quote! {
                    {
                        #comma;
                        Deser::deser(tracker)?
                    }
                });
            }

            let assigns = if assigns.is_empty() {
                quote! {}
            } else {
                quote! { #(#assigns),* }
            };

            (
                quote! {
                    let _open = Token::new_borrowed(TokenKind::TupleOpen, "(");
                    let _close = Token::new_borrowed(TokenKind::TupleClose, ")");
                    let _comma = Token::new_borrowed(TokenKind::Comma, ", ");
                    let _colon = Token::new_borrowed(TokenKind::Colon, ": ");

                    #parse_name

                    tracker.try_token(&_open)?;
                    let rval = Ok(#name( #assigns ));
                    tracker.try_token(&_close)?;

                    rval
                },
                true,
            )
        }
        Fields::Unit => (
            quote! {
                #parse_name
                Ok(#name)
            },
            true,
        ),
    }
}

fn impls_for_deser(kr: &Tokens, input: &DeriveInput, info: &DeriveInfo) -> (bool, Tokens) {
    if info.opaque {
        return (false, quote! {});
    }

    let name = &input.ident;
    let generics = add_trait_bounds(input.generics.clone(), &HashSet::new(), &["Deser"]);
    let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();

    let (res, active) = match &input.data {
        Data::Struct(data) => impl_struct_for_deser(quote! {#name}, &data.fields, false),
        Data::Enum(data) => {
            let mut names = vec![];
            let mut match_arms = vec![];

            for variant in data.variants.iter() {
                let ident = &variant.ident;
                let variant_name = format!("{}", variant.ident);
                let variant_access = if info.from_interact {
                    quote! {#ident}
                } else {
                    quote! {#name::#ident}
                };

                let (code, _) = impl_struct_for_deser(variant_access, &variant.fields, true);
                match_arms.push(quote! {
                    #variant_name => {
                        return {
                            tracker.step();
                            #code
                        }
                    }
                });

                names.push(quote! { #variant_name });
            }

            let names = &names;
            (
                quote! {
                    if !tracker.has_remaining() {
                        for name in &[#(#names,)*] {
                            tracker.possible_token(
                                Token::new_borrowed(TokenKind::Ident, *name));
                        }
                        return Err(deser::DeserError::EndOfTokenList);
                    }
                    if let TokenKind::Ident = tracker.top_kind() {
                        let text = tracker.top().text.as_ref();
                        match text {
                            #(#match_arms),*
                            _ => {
                                let text = String::from(text);
                                for name in &[#(#names,)*] {
                                    if name.starts_with(&text) {
                                        tracker.possible_token(
                                            Token::new_borrowed(
                                                TokenKind::Ident, *name));
                                    }
                                }
                                return Err(deser::DeserError::UnexpectedToken);
                            }
                        }
                    } else {
                        return Err(deser::DeserError::UnexpectedToken);
                    }
                },
                true,
            )
        }
        _ => {
            panic!();
        }
    };

    (
        active,
        quote! {
            impl #impl_generics Deser for #name #ty_generics #where_clause {
                fn deser<'a, 'b>(tracker: &mut deser::Tracker<'a, 'b>) -> deser::Result<Self> {
                    use #kr::deser::*;
                    #res
                }
            }
        },
    )
}

fn get_interact_meta_items(attr: &syn::Attribute) -> Option<Vec<syn::NestedMeta>> {
    if attr.path.segments.len() == 1 && attr.path.segments[0].ident == "interact" {
        match attr.interpret_meta() {
            Some(List(ref meta)) => Some(meta.nested.iter().cloned().collect()),
            _ => {
                // TODO: produce an error
                None
            }
        }
    } else {
        None
    }
}

fn is_skipped(attrs: &Vec<syn::Attribute>) -> bool {
    for meta_items in attrs.iter().filter_map(get_interact_meta_items) {
        for meta_item in meta_items {
            match &meta_item {
                Meta(Word(word)) if word == "skip" => return true,
                _ => continue,
            }
        }
    }
    false
}

fn add_trait_bounds(
    mut generics: Generics,
    skip_set: &HashSet<String>,
    trait_names: &[&str],
) -> Generics {
    for param in &mut generics.params {
        if let GenericParam::Type(ref mut type_param) = *param {
            if let Some(_) = skip_set.get(&type_param.ident.to_string()) {
                continue;
            }
            for trait_name in trait_names {
                let trait_name = Ident::new(*trait_name, Span::call_site());
                let bound = syn::parse(quote! { #trait_name }.into()).unwrap();
                type_param.bounds.push(bound);
            }
        }
    }
    generics
}

fn tokens_to_rustfmt_file(filename: &std::path::Path, expanded: &Tokens) {
    let mut file = std::fs::File::create(&filename).unwrap();
    use std::io::Write;
    file.write_all(format!("{}", expanded).as_bytes()).unwrap();
    Command::new("rustfmt")
        .args(&[filename])
        .output()
        .expect("failed to execute process");
}