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
/// This crate provides macros to generate boiler-plate CEF-trait code
/// to provide fixed values, inherit values and bubble them up to a larger item
/// or to convert fields/variants into header values.
///
/// It also allows anotating fields to have specific names in CEF extensions.
///
extern crate proc_macro;
extern crate proc_macro2;
#[macro_use]
extern crate quote;
#[macro_use]
extern crate syn;
#[macro_use]
extern crate lazy_static;

use crate::proc_macro::TokenStream;
use inflections::case::to_snake_case;
use proc_macro2::{Span, TokenStream as TokenStream2};
use std::convert::From;
use syn::spanned::Spanned;
use syn::{
    Attribute, Data, DataEnum, DataStruct, DeriveInput, Error as SynError, Field, Fields, Ident,
    Index, Lit, Meta, MetaNameValue, NestedMeta, Path, Variant,
};

const CEF_ALLOWED_HEADERS: &[&str] = &[
    "Version",
    "DeviceVendor",
    "DeviceProduct",
    "DeviceVersion",
    "DeviceEventClassID",
    "Name",
    "Severity",
];

enum FieldValueType {
    InheritTrait,
    DisplayTrait,
}

enum PrefixSelf {
    Yes,
    No,
}

type CompileResult = Result<TokenStream2, TokenStream2>;
type OptionalCompileResult = Result<Option<TokenStream2>, TokenStream2>;

type VariantFieldResult = Result<Ident, TokenStream2>;

lazy_static! {
    static ref CEF_ALLOWED_HEADERS_JOINED: String = CEF_ALLOWED_HEADERS.join(",");
    static ref CEF_INVALID_HEADER: String = ["header name should be one of the following:", CEF_ALLOWED_HEADERS_JOINED.as_str()].join(" ");
    static ref CEF_HEADERS_APPLICATION: String = "This attribute only applies to Structs or Enums.".to_owned();

    static ref CEF_HEADER_MISSING_VALUES_OR_INHERIT: String = "Deriving this trait requires a value for the header be provided through one of 'cef_values' or 'cef_inherit' macros on members of structs, and every variant of an enum.".to_owned();

    static ref CEF_VALUES_APPLICABLE: String = "'cef_values' macro may apply on a Struct, Enum or Enum::Variant, but never on fields".to_owned();
    static ref CEF_INHERIT_APPLICABLE: String = "'cef_inherit' macro should apply only to a struct/tuple field (possibly inside an Enum variant), but not the Struct, Enum, or Enum::Variant.".to_owned();
    static ref CEF_FIELD_APPLICABLE: String = "'cef_field' macro should apply only to a struct/tuple field (possibly inside an Enum variant), but not the Struct, Enum, or Enum::Variant.".to_owned();

    static ref CEF_VALUES_USAGE: String = "'cef_values' macro expects header values to be listed in the following syntax: #[cef_values(header1 = \"value1\", header2 = \"value2\", ...)] ".to_owned();
    static ref CEF_INHERIT_USAGE: String = "'cef_inherit' macro adapts the attributed field by inheriting the desired trait from that field: #[cef_inherit(headerTrait)] ".to_owned();
    static ref CEF_FIELD_USAGE: String = "'cef_field' macro adapts the attributed field using the fmt::Display trait into a CEF header trait. Use it on any field that implements fmt::Display: #[cef_field(headerTrait)]".to_owned();

    static ref CEF_VALUES_STRINGS: String = "'cef_values' macro expects all values to be string literals".to_owned();
}

struct TraitValue {
    pub ts: TokenStream2,
    pub span: Span,
}

type ParseAttrResult<T> = Result<T, TokenStream2>;

/// This macro derives the ToCef trait on the annotated item.
/// For now, the ToCef trait itself provides a useful implementation
/// of the trait, so this macro simply implements a blank trait.
///
#[proc_macro_derive(ToCef)]
pub fn derive_to_cef(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as DeriveInput);

    // type name
    let name = &input.ident;

    // generics
    let generics = input.generics;
    let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();

    // default implementation is great
    let to_cef_impl = quote! {
        impl #impl_generics rust_cef::ToCef for #name #ty_generics #where_clause {}
    };

    TokenStream::from(to_cef_impl)
}

#[proc_macro_derive(CefHeaderVersion, attributes(cef_values, cef_inherit, cef_field))]
pub fn derive_cef_header_version(item_tokens: TokenStream) -> TokenStream {
    let item = parse_macro_input!(item_tokens as DeriveInput);

    // Only applies to structs and enums
    if let Some(compile_error) = is_valid_item_type(&item) {
        return compile_error;
    }

    implement_trait("CefHeaderVersion", &item)
}

#[proc_macro_derive(CefHeaderDeviceVendor, attributes(cef_values, cef_inherit, cef_field))]
pub fn derive_cef_header_device_vendor(item_tokens: TokenStream) -> TokenStream {
    let item = parse_macro_input!(item_tokens as DeriveInput);

    // Only applies to structs and enums
    if let Some(compile_error) = is_valid_item_type(&item) {
        return compile_error;
    }

    implement_trait("CefHeaderDeviceVendor", &item)
}

#[proc_macro_derive(CefHeaderDeviceProduct, attributes(cef_values, cef_inherit, cef_field))]
pub fn derive_cef_header_device_product(item_tokens: TokenStream) -> TokenStream {
    let item = parse_macro_input!(item_tokens as DeriveInput);

    // Only applies to structs and enums
    if let Some(compile_error) = is_valid_item_type(&item) {
        return compile_error;
    }

    implement_trait("CefHeaderDeviceProduct", &item)
}

#[proc_macro_derive(CefHeaderDeviceVersion, attributes(cef_values, cef_inherit, cef_field))]
pub fn derive_cef_header_device_version(item_tokens: TokenStream) -> TokenStream {
    let item = parse_macro_input!(item_tokens as DeriveInput);

    // Only applies to structs and enums
    if let Some(compile_error) = is_valid_item_type(&item) {
        return compile_error;
    }

    implement_trait("CefHeaderDeviceVersion", &item)
}
#[proc_macro_derive(
    CefHeaderDeviceEventClassID,
    attributes(cef_values, cef_inherit, cef_field)
)]
pub fn derive_cef_header_device_event_class_id(item_tokens: TokenStream) -> TokenStream {
    let item = parse_macro_input!(item_tokens as DeriveInput);

    // Only applies to structs and enums
    if let Some(compile_error) = is_valid_item_type(&item) {
        return compile_error;
    }

    implement_trait("CefHeaderDeviceEventClassID", &item)
}
#[proc_macro_derive(CefHeaderName, attributes(cef_values, cef_inherit, cef_field))]
pub fn derive_cef_header_name(item_tokens: TokenStream) -> TokenStream {
    let item = parse_macro_input!(item_tokens as DeriveInput);

    // Only applies to structs and enums
    if let Some(compile_error) = is_valid_item_type(&item) {
        return compile_error;
    }

    implement_trait("CefHeaderName", &item)
}
#[proc_macro_derive(CefHeaderSeverity, attributes(cef_values, cef_inherit, cef_field))]
pub fn derive_cef_header_severity(item_tokens: TokenStream) -> TokenStream {
    let item = parse_macro_input!(item_tokens as DeriveInput);

    // Only applies to structs and enums
    if let Some(compile_error) = is_valid_item_type(&item) {
        return compile_error;
    }

    implement_trait("CefHeaderSeverity", &item)
}

#[proc_macro_derive(CefExtensions)]
pub fn derive_cef_extensions(input: TokenStream) -> TokenStream {
    let item = parse_macro_input!(input as DeriveInput);

    // Only applies to structs and enums
    if let Some(compile_error) = is_valid_item_type(&item) {
        return compile_error;
    }

    // type name
    let name = &item.ident;

    // generics
    let generics = item.generics;
    let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();

    // default implementation is great
    let cef_extensions_impl = quote! {
        impl #impl_generics rust_cef::CefExtensions for #name #ty_generics #where_clause {
            fn cef_extensions(&self) -> rust_cef::CefResult {
                Ok("".to_owned())
            }
        }
    };

    TokenStream::from(cef_extensions_impl)
}

fn is_valid_item_type(item: &DeriveInput) -> Option<TokenStream> {
    // Only applies to structs and enums
    match item.data {
        Data::Struct(_) | Data::Enum(_) => {}
        _ => {
            return Some(TokenStream::from(
                SynError::new(Span::call_site(), CEF_HEADERS_APPLICATION.to_owned())
                    .to_compile_error(),
            ))
        }
    }

    None
}

/// Implements the trait asked by any of the `#[derive(CefHeader*)]` attributes
/// It creates the trait skeleton and outsources the returned value
/// to a child-item function.
fn implement_trait(trait_name_str: &str, item: &DeriveInput) -> TokenStream {
    // type name
    let item_name = &item.ident;

    // generics
    let item_generics = &item.generics;
    let (item_impl_generics, item_ty_generics, item_where_clause) = item_generics.split_for_impl();

    let trait_name = format_ident!("{}", trait_name_str);
    let method_name = format_ident!("{}", to_snake_case(trait_name.to_string().as_str()));

    let value = header_value_from_child_item(&trait_name, &method_name, &item);

    let trait_impl = quote! {
        impl #item_impl_generics rust_cef::#trait_name for #item_name #item_ty_generics #item_where_clause {
            fn #method_name(&self) -> rust_cef::CefResult {
                #value
            }
        }
    };

    //println!("{:#?}", trait_impl.to_string());

    TokenStream::from(trait_impl)
}

/// This function provides the crucial value that
/// the implementing trait returns for the given item
///
/// Depending on whether the item is a Struct or an Enum,
/// the interpretation of other helper attributes changes.
/// This function does that detection and forks processing
/// to more specialized functions for each.
///
/// NOTE: Union types are not supported.
///
fn header_value_from_child_item(
    header_name: &Ident,
    method_name: &Ident,
    item: &DeriveInput,
) -> TokenStream2 {
    // Is the Item a struct or enum?
    match &item.data {
        Data::Struct(s) => header_value_from_child_struct(header_name, method_name, s, item),
        Data::Enum(e) => header_value_from_child_enum(header_name, method_name, e, item),
        Data::Union(_) => {
            return SynError::new(Span::call_site(), CEF_HEADERS_APPLICATION.to_owned())
                .to_compile_error()
        }
    }
}

/// This function retrievs the value for the header trait
/// from a Struct item
///
/// For a struct, v alues come from three primary ways:
///
/// `#[cef_values(HeaderName = value)]`
/// This sets the value as a constant string literal. This
/// attribute must be on the root the Struct only (not on fields or variants)
///
/// Essentially, this looks like
/// ```ignore
/// #[derive(Header)]
/// #[cef_values(Header = "value")]
/// struct Outer {
/// }
/// ```
///
/// and expands to:
/// ```ignore
/// impl Header for Outer {
///     method(&self) {
///         "value"
///     }
/// }
/// ```
///
/// `#[cef_inherit(HeaderName)`
/// This sets the value of that header as an inherited trait from a field.
/// This property only applies to fields of a struct.
///
/// Essentially, this looks like
/// ```ignore
/// #[derive(Header)]
/// struct Outer {
///     #[cef_inherit(Header)]
///     pub inner: Inner;
/// }
/// ```
///
/// and expands to:
/// ```ignore
/// impl Header for Outer {
///     method(&self) {
///         Trait::method(&self.inner)
///     }
/// }
/// ```
///
/// #[cef_field(HeaderName)]
/// This sets the value of that header as the default Display formatting for the field.
///
/// Essentially, this looks like
/// ```ignore
/// #[derive(Header)]
/// struct Outer {
///     #[cef_field(Header)]
///     pub inner: Inner;
/// }
/// ```
///
/// and expands to:
/// ```ignore
/// impl Header for Outer {
///     method(&self) {
///         format!("{}", &self.inner)
///     }
/// }
/// ```
///
/// NOTE: This method looks for ALL possible values first,
/// and then if only one is found, uses it. If no values are found,
/// an error is thrown, and if multiple values are found an error is
/// thrown to indicate conflict and ambiguity.
///
fn header_value_from_child_struct(
    header_name: &Ident,
    method_name: &Ident,
    s: &DataStruct,
    item: &DeriveInput,
) -> TokenStream2 {
    let mut trait_values: Vec<TraitValue> = vec![];

    // look for fixed cef_values in top-level
    if let Some(ts) = top_level_cef_values(header_name, &item.attrs, &mut trait_values) {
        return ts;
    }

    // now look for struct's field attributes
    for (index, field) in s.fields.iter().enumerate() {
        for attr in &field.attrs {
            if attr.path.is_ident("cef_inherit") || attr.path.is_ident("cef_field") {
                let (usage_message, value_type) = match attr.path.is_ident("cef_inherit") {
                    true => (CEF_INHERIT_USAGE.to_owned(), FieldValueType::InheritTrait),
                    false => (CEF_FIELD_USAGE.to_owned(), FieldValueType::DisplayTrait),
                };

                match parse_attrs_to_path(attr, usage_message.as_str()) {
                    Ok(paths) => {
                        for p in paths {
                            if p.is_ident(header_name) {
                                let ts = match &field.ident {
                                    Some(i) => field_value(
                                        header_name,
                                        method_name,
                                        &value_type,
                                        format_ident!("{}", i),
                                        PrefixSelf::Yes,
                                    ),
                                    None => field_value(
                                        header_name,
                                        method_name,
                                        &value_type,
                                        Index::from(index),
                                        PrefixSelf::Yes,
                                    ),
                                };

                                let span = p.span().clone();

                                let tv = TraitValue { ts, span };

                                trait_values.push(tv);
                            }
                        }
                    }
                    Err(e) => return e,
                }
            }
        }
    }

    match trait_values.len() {
        0 => return SynError::new(Span::call_site(), CEF_HEADER_MISSING_VALUES_OR_INHERIT.to_owned()).to_compile_error(),
        1 => return match trait_values.pop() {
            Some(tv) => tv.ts,
            None => return SynError::new(Span::call_site(), "FATAL Error in this macro. Thought it generated a value, but it apparently did not.".to_owned()).to_compile_error(),
        },
        _ => {
            let errs = trait_values.iter().map(|tv| {
                return SynError::new(tv.span, format!("Trait {} had values provided in multiple places. Please remove all but one of these.", header_name))
                    .to_compile_error();
            });

            quote!{
                #(#errs)*
            }
        },
    }
}

/// This function retrievs the value for the header trait
/// from am Enum item, and is significantly more complicated
/// than the Struct implementation for a number of reasons.
///
/// First let's understand the overall concept of how Enums work:
/// 1. If an Enum exposes a Header, then each variant must provide a value
///    for that header. It must be exhaustive and comprehensive.
/// 2. If an Enum sets a Header at the root (using `#[cef_values(Header = "value")]`)
///    None of its variants must provide that value or override it.
///
/// Enum values come from three primary ways:
///
/// * `#[cef_values(HeaderName = value)]`
/// This sets the value as a constant string literal. This
/// attribute may be on the root the Enum (in which case any Variants may NOT override or conflit with it.)
///
/// Essentially, this looks like
/// ```ignore
/// #[derive(Header)]
/// #[cef_values(Header = "value")]
/// enum Items {
/// }
/// ```
///
/// and expands to:
/// ```ignore
/// impl Header for Items {
///     method(&self) {
///         "value"
///     }
/// }
/// ```
///
/// It may also be applied to variants, thereby allowing each variant to choose a different
/// value, or mix and match with `cef_inherit` and `cef_field`.
///
/// * `#[cef_inherit(HeaderName)]`
/// This attribute applies only to a field.
///
/// Essentially, this looks like
/// ```ignore
/// #[derive(Header)]
/// enum Items {
///     #[cef_inherit(Header = 1)]
///     Variant1(
///         int,
///
///         #[cef_inherit(Header)]
///         HeaderImplementer1
///     ),
///
///     Variant2{
///         name: String,
///
///         #[cef_inherit(Header)]
///         address: HeaderImplementer2
///     },
/// }
///
/// // where
/// impl Header for HeaderImplementer1 {
/// //...
/// }
/// // and
/// impl Header for HeaderImplementer2 {
/// //...
/// }
/// ```
///
/// and expands to:
/// ```ignore
/// impl Header for Items {
///     method(&self) {
///         match &self {
///             Items::Variant1(_index0, _index1) => Header::method(_index1),
///             Items::Variant2{name: _name, address: _address} => Header::method(_address),
///         }
///     }
/// }
/// ```
///
/// As you can see, the variants don't have to be symmetric, so long as they each provide
/// an implementation for the Header. It is also not necessary that all variants inhert it. Some may
/// inherit, while others may use field values.
///
/// However, they may not override the root-level `#[cef_values(Header = "value")]` construct which is a conflict.
///
/// #[cef_field(HeaderName = ["field" | index])]
/// This operates identically to #[cef_inherit], except that instead of calling the field's
/// trait method (thus inheriting the trait), it uses the Display trait to produce a string
///
/// Essentially, this looks like
/// ```ignore
/// #[derive(Header)]
/// enum Items {
///
///     Variant1(
///         int,
///
///         #[cef_field(Header)]
///         DisplayImplementer1
///     ),
///
///     Variant2{
///         name: String,
///
///         #[cef_field(Header)]
///         address: DisplayImplementer2
///     },
/// }
///
/// // where
/// impl Display for DisplayImplementer1 {
/// //...
/// }
/// // and
/// impl Display for DisplayImplementer2 {
/// //...
/// }
/// ```
///
/// and expands to:
/// ```ignore
/// impl Header for Items {
///     method(&self) {
///         match &self {
///             Items::Variant1(_index0, _index1) => format!("{}",_index1),
///             Items::Variant2{name: _name, address: _address} => format!("{}",_address),
///         }
///     }
/// }
/// ```
///
/// Finally and for completeness, remember it is possible to mix-and-match
///
/// For Example:
/// ```ignore
/// #[derive(Header)]
/// enum Items {
///
///     Variant1(
///         int,
///
///         #[cef_field(Header)]
///         DisplayImplementer1
///     ),
///
///     Variant2{
///         name: String,
///
///         #[cef_inherit(Header)]
///         address: HeaderImplementer2
///     },
///
///     #[cef_value(Header = "Variant3")]
///     Variant3,
/// }
///
/// // where
/// impl Display for DisplayImplementer1 {
/// //...
/// }
/// // and
/// impl Header for HeaderImplementer2 {
/// //...
/// }
/// ```
///
/// and expands to:
/// ```ignore
/// impl Header for Items {
///     method(&self) {
///         match &self {
///             Items::Variant1(_index0, _index1) => format!("{}",_index1),
///             Items::Variant2{name: _name, address: _address} => Header::method(_address),
///             Items::Variant3 => "variant3",
///         }
///     }
/// }
/// ```
///
/// NOTE: This method looks for ALL possible values first,
/// and then if only one is found, uses it. If no values are found,
/// an error is thrown, and if multiple values are found an error is
/// thrown to indicate conflict and ambiguity.
///
fn header_value_from_child_enum(
    header_name: &Ident,
    method_name: &Ident,
    e: &DataEnum,
    item: &DeriveInput,
) -> TokenStream2 {
    let mut trait_values: Vec<TraitValue> = vec![];

    // look for fixed cef_values in top-level
    if let Some(ts) = top_level_cef_values(header_name, &item.attrs, &mut trait_values) {
        return ts;
    }

    // Set CEF value for this header from every variant
    if let Some(ts) = all_variants_cef_value(header_name, method_name, &e, &mut trait_values) {
        return ts;
    }

    match trait_values.len() {
        0 => return SynError::new(Span::call_site(), CEF_HEADER_MISSING_VALUES_OR_INHERIT.to_owned()).to_compile_error(),
        1 => match trait_values.pop() {
            Some(tv) => tv.ts,
            None => return SynError::new(Span::call_site(), "FATAL Error in this macro. Thought it generated a value, but it apparently did not.".to_owned()).to_compile_error(),
        },
        _ => {
            let errs = trait_values.iter().map(|tv| {
                return SynError::new(tv.span, format!("Trait {} had values provided in multiple places. Please remove all but one of these.", header_name))
                    .to_compile_error();
            });

            quote!{
                #(#errs)*
            }
        },
    }
}

/// This function creates a match statement with args for every variant for the Enum
/// this is what allows a unified Header trait to be implemented on the Enum.
///
fn all_variants_cef_value(
    header_name: &Ident,
    method_name: &Ident,
    e: &DataEnum,
    trait_values: &mut Vec<TraitValue>,
) -> Option<TokenStream2> {
    let match_branches_result: Result<Vec<Option<TokenStream2>>, TokenStream2> = e
        .variants
        .iter()
        .map(|variant| destructure_and_match_variant(header_name, method_name, &variant))
        .collect();

    let match_branches: Vec<TokenStream2> = match match_branches_result {
        Ok(tses) => tses.into_iter().filter_map(|v| v).collect(),
        Err(ts) => return Some(ts),
    };

    // No implementations from variant
    if match_branches.len() == 0 {
        return None;
    }

    // did we get ALL variants?
    if match_branches.len() < e.variants.len() {
        return Some(SynError::new(Span::call_site(), format!("Header trait {} was not implemented for ALL variants of this enum. Unable to derive for the overall enum.", header_name)).to_compile_error());
    }

    // Finally compile all branches into a match
    // operator block like thus:
    //
    // match &self {
    //       variant1 branch => value1,
    //       variant2 branch => value12,
    // ...
    // }
    let ts = quote! {
        match &self {
            #(#match_branches)*
        }
    };

    let tv = TraitValue {
        ts,
        span: Span::call_site(),
    };

    trait_values.push(tv);

    None
}

/// create a enum variant field de-structuring expression
/// and match the field which has an attribute for obtaining header
/// value.
///
/// create this field-matcher for named fields
///```ignore
///  {name: _name, address: _address} => _address
///  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
///
///  (_index0, index1) => _index0
///  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
///```
///
/// The value part may be a Dispay trait formatted value (i.e. format!("{}", field))
/// or an inheritance of the header trait (i.e. header_trait::method(&field))
///
///
///
fn destructure_and_match_variant(
    header_name: &Ident,
    method_name: &Ident,
    variant: &Variant,
) -> OptionalCompileResult {
    // Get the identity of the Variant
    // This part:
    // ```
    // V1{...}
    // ^^
    // V2{...}
    // ^^
    // ```
    //
    let ident = variant.ident.clone();

    let mut trait_values: Vec<TraitValue> = vec![];

    // See if there's any top-level cef_values attributes on the variant
    if let Some(ts) = top_level_cef_values(header_name, &variant.attrs, &mut trait_values) {
        return Err(ts);
    }

    // create a field-capture
    // field_captures is a Vector of either:
    // `<_, _, _, captureField: _captureField, _,>` - for named fields
    // `<_, _, _, _index3, _,>` - for unnamed fields
    //
    // if any field is named (and not ignored with an underscore), then the trait_values vector
    // will have a tokenstream for that value
    //
    let field_captures_result: Result<Vec<TokenStream2>, TokenStream2> = variant
        .fields
        .iter()
        .enumerate()
        .map(|(index, f)| -> CompileResult {
            // see if there's any field-level cef_inherit or cef_field attributes on the variant
            let (field_prefix, fieldid) = match &f.ident {
                Some(id) => (quote! {#id:}, format_ident!("_{}", id)),
                None => (quote! {}, format_ident!("_index{}", index)),
            };

            let final_fieldid =
                match variant_field_value(header_name, method_name, &fieldid, f, &mut trait_values)
                {
                    Err(ts) => return Err(ts),
                    Ok(ident) => ident,
                };

            Ok(quote! {#field_prefix#final_fieldid})
        })
        .collect();

    let field_captures = match field_captures_result {
        Err(ts) => return Err(ts),
        Ok(fc) => fc,
    };

    // Named fields (aka Struct variant) is wrapped with {},
    // whereas Unnamed fields (aka Tuple variant) is wrapped with ()
    // Now we have something like:
    // `{_, _, _, captureField: _captureField, _}` - for named fields
    // `(_, _, _, _index3, _)` - for unnamed fields
    let variant_capture = match &variant.fields {
        Fields::Named(_) => quote! {{#(#field_captures),*}},
        Fields::Unnamed(_) => quote! {(#(#field_captures),*)},
        Fields::Unit => quote! {},
    };

    let val = match trait_values.len() {
        // no values for this variant at this level. We return no branch.
        0 => return Ok(None),
        1 => match trait_values.pop() {
            Some(tv) => tv.ts,
            None => return Err(SynError::new(Span::call_site(), "FATAL Error in this macro. Thought it generated a value, but it apparently did not.".to_owned()).to_compile_error()),
        },
        _ => {
            let errs = trait_values.iter().map(|tv| {
                return SynError::new(tv.span, format!("Trait {} had values provided in multiple places for variant {}. Please remove all but one of these.", header_name, ident))
                    .to_compile_error();
            });

            return Err(quote!{
                #(#errs)*
            });
        },
    };

    //
    // The overall match branch now looks like this:
    //
    // For named fields:
    // Self::Variant1{name: _name, address: _}  => format!("{}", _name)
    // ^^^^^  ^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^      ^^^^^^^^^^^^^^^^^^^^
    // enum   variant   field matchers we just created     The value for this variant (which may refer to the fields - even unnamed fields are captured under positional names _ident0, _ident1, etc.)
    //
    // For unnamed fields:
    // Self::Variant1(_index, _)  => format!("{}", _index0)
    // ^^^^^  ^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^      ^^^^^^^^^^^^^^^^^^^^
    // enum   variant   field matchers we just created     The value for this variant (which may refer to the fields - even unnamed fields are captured under positional names _ident0, _ident1, etc.)
    let match_branch = quote! {
        Self::#ident#variant_capture => #val,
    };

    Ok(Some(match_branch))
}

fn variant_field_value(
    header_name: &Ident,
    method_name: &Ident,
    fieldid: &Ident,
    field: &Field,
    trait_values: &mut Vec<TraitValue>,
) -> VariantFieldResult {
    let mut ignore_ident: bool = true;

    for attr in &field.attrs {
        if attr.path.is_ident("cef_values") {
            return Err(
                SynError::new(attr.span(), CEF_VALUES_APPLICABLE.to_owned()).to_compile_error()
            );
        } else if attr.path.is_ident("cef_inherit") || attr.path.is_ident("cef_field") {
            let (message, value_type) = match attr.path.is_ident("cef_inherit") {
                true => (CEF_INHERIT_USAGE.to_owned(), FieldValueType::InheritTrait),
                false => (CEF_FIELD_USAGE.to_owned(), FieldValueType::DisplayTrait),
            };

            match parse_attrs_to_path(&attr, &message) {
                Err(e) => return Err(e),
                Ok(paths) => {
                    for p in paths {
                        if p.is_ident(header_name) {
                            let ts = field_value(
                                header_name,
                                method_name,
                                &value_type,
                                fieldid,
                                PrefixSelf::No,
                            );
                            let span = p.span().clone();

                            // no longer ignore the ident
                            ignore_ident = false;

                            trait_values.push(TraitValue { ts, span });
                        }
                    }
                }
            }
        }
    }

    match ignore_ident {
        true => Ok(format_ident!("_")),
        false => Ok(fieldid.clone()),
    }
}

// Helps cut through a lot of parse tree and doesn't confuse reading-context
fn parse_attrs_to_path(attr: &Attribute, messsage: &str) -> ParseAttrResult<Vec<Path>> {
    let mut paths: Vec<Path> = vec![];

    match attr.parse_meta() {
        Ok(parsed_meta) => match parsed_meta {
            Meta::List(list) => {
                for nested_meta in list.nested {
                    match nested_meta {
                        NestedMeta::Meta(meta) => match meta {
                            Meta::Path(p) => {
                                paths.push(p);
                            }
                            _ => {
                                return Err(SynError::new(attr.span(), messsage).to_compile_error())
                            }
                        },
                        _ => return Err(SynError::new(attr.span(), messsage).to_compile_error()),
                    }
                }
            }
            _ => return Err(SynError::new(attr.span(), messsage).to_compile_error()),
        },
        Err(e) => return Err(e.to_compile_error()),
    }

    Ok(paths)
}
// Helps cut through a lot of parse tree and doesn't confuse reading-context
fn parse_attrs_to_name_value(
    attr: &Attribute,
    message: &str,
) -> ParseAttrResult<Vec<MetaNameValue>> {
    let mut mnvs: Vec<MetaNameValue> = vec![];

    match attr.parse_meta() {
        Err(e) => return Err(e.to_compile_error()),
        Ok(metadata) => match metadata {
            Meta::List(list) => {
                for nestedmeta in list.nested {
                    match nestedmeta {
                        NestedMeta::Meta(meta) => match meta {
                            Meta::NameValue(mnv) => {
                                mnvs.push(mnv);
                            }
                            _ => {
                                return Err(SynError::new(attr.span(), message.to_owned())
                                    .to_compile_error())
                            }
                        },
                        _ => {
                            return Err(
                                SynError::new(attr.span(), message.to_owned()).to_compile_error()
                            )
                        }
                    }
                }
            }
            _ => return Err(SynError::new(attr.span(), message.to_owned()).to_compile_error()),
        },
    }

    Ok(mnvs)
}

/// Generates a value from a field
fn field_value<T: quote::ToTokens>(
    header_name: &Ident,
    method_name: &Ident,
    value_type: &FieldValueType,
    field_name: T,
    prefix_self: PrefixSelf,
) -> TokenStream2 {
    let maybe_self = match prefix_self {
        PrefixSelf::Yes => quote! {&self.},
        PrefixSelf::No => quote! {},
    };

    match value_type {
        FieldValueType::InheritTrait => quote! {
            rust_cef::#header_name::#method_name(#maybe_self#field_name)
        },
        FieldValueType::DisplayTrait => quote! {
            Ok(format!("{}", #maybe_self#field_name))
        },
    }
}

/// Looks for the #[cef_values] attribute at the top level of a Struct,
/// Enum or Enum::Variant and returns a fixed string value.
///
/// For example:
///
/// ```ignore
/// #[derive(CefHeaderName)]
/// #[cef_values(CefHeaderName = "fixedName")]
/// struct Foo {
/// }
/// ```
///
/// ```ignore
/// #[derive(CefHeaderName)]
/// #[cef_values(CefHeaderName = "fixedName")]
/// enum Foo {
///     Variant1,
///     Variant2,
/// }
/// ```
///
/// ```ignore
/// #[derive(CefHeaderName)]
/// enum Foo {
///     #[cef_values(CefHeaderName = "fixedVariant1Name")]
///     Variant1,
///     #[cef_values(CefHeaderName = "fixedVariant2Name")]
///     Variant2,
/// }
/// ```
///
fn top_level_cef_values(
    header_name: &Ident,
    attrs: &Vec<Attribute>,
    trait_values: &mut Vec<TraitValue>,
) -> Option<TokenStream2> {
    for attr in attrs {
        if attr.path.is_ident("cef_inherit") {
            return Some(
                SynError::new(attr.path.span(), CEF_INHERIT_APPLICABLE.to_owned())
                    .to_compile_error(),
            );
        } else if attr.path.is_ident("cef_field") {
            return Some(
                SynError::new(attr.path.span(), CEF_FIELD_APPLICABLE.to_owned()).to_compile_error(),
            );
        } else if attr.path.is_ident("cef_values") {
            match parse_attrs_to_name_value(attr, &CEF_VALUES_USAGE) {
                Err(ts) => return Some(ts),
                Ok(mnvs) => {
                    for mnv in mnvs {
                        if mnv.path.is_ident(header_name) {
                            match &mnv.lit {
                                Lit::Str(strval) => {
                                    let ts = quote! {
                                        Ok(#strval.to_owned())
                                    };
                                    let span = mnv.span().clone();
                                    trait_values.push(TraitValue { ts, span });
                                }
                                _ => {
                                    return Some(
                                        SynError::new(
                                            mnv.lit.span(),
                                            CEF_VALUES_STRINGS.to_owned(),
                                        )
                                        .to_compile_error(),
                                    )
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    None
}