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
//! Machinery for parsing [`syn::Attribute`]s into a custom defined struct.

use proc_macro2::Span;
use syn::{parse::Parse, spanned::Spanned};

use crate::has;

use super::err;

#[doc(inline)]
pub use self::{dedup::Dedup, kind::Kind, validate::Validation};

/// [`Parse`]ing of [`syn::Attribute`]s into a custom defined struct.
pub trait Attrs: Default + Parse {
    /// Tries to merge two sets of parsed attributes into a single one,
    /// reporting about duplicates, if any.
    ///
    /// # Errors
    ///
    /// If merging cannot be performed.
    fn try_merge(self, another: Self) -> syn::Result<Self>;

    /// Validates these parsed attributes to meet additional invariants, if
    /// required.
    ///
    /// The provided string contains name of the parsed [`syn::Attribute`], and
    /// the provided [`Span`] refers to the item this [`syn::Attribute`] is
    /// applied to. Use them to make reported errors well descriptive.
    ///
    /// # Errors
    ///
    /// If validation fails.
    #[inline]
    fn validate(&self, _: &str, _: Span) -> syn::Result<()> {
        Ok(())
    }

    /// Falls back to another values from [`syn::Attribute`]s, if required.
    ///
    /// # Errors
    ///
    /// If retrieving fallback values fails.
    #[inline]
    fn fallback(&mut self, _: &[syn::Attribute]) -> syn::Result<()> {
        Ok(())
    }

    /// Parses this structure from the [`syn::Attribute`]s with the given `name`
    /// and contained in the given `item`.
    ///
    /// If multiple [`syn::Attribute`]s occur with the same `name` then they all
    /// are parsed separately and then [`Attrs::try_merge`]d.
    ///
    /// If none [`syn::Attribute`]s occur with the given `name` then [`Default`]
    /// value is returned, modulo [`Attrs::validate`].
    ///
    /// # Errors
    ///
    /// - If [`Parse`]ing of this [`Attrs`] fails.
    /// - If either [`Attrs::try_merge()`], [`Attrs::validate()`] or
    ///   [`Attrs::fallback()`] fails.
    fn parse_attrs<T>(name: &str, item: &T) -> syn::Result<Self>
    where
        T: has::Attrs + Spanned,
    {
        let attrs = item.attrs();
        filter_by_name(name, attrs)
            .map(syn::Attribute::parse_args)
            .try_fold(Self::default(), |prev, curr| prev.try_merge(curr?))
            .and_then(|mut parsed| {
                parsed.fallback(attrs)?;
                parsed.validate(name, item.span())?;
                Ok(parsed)
            })
    }
}

impl<V: Attrs + Default + Parse> Attrs for Box<V> {
    #[inline]
    fn try_merge(self, another: Self) -> syn::Result<Self> {
        (*self).try_merge(*another).map(Self::new)
    }

    #[inline]
    fn validate(&self, attr_name: &str, item_span: Span) -> syn::Result<()> {
        (&**self).validate(attr_name, item_span)
    }

    #[inline]
    fn fallback(&mut self, attrs: &[syn::Attribute]) -> syn::Result<()> {
        (&mut **self).fallback(attrs)
    }

    #[inline]
    fn parse_attrs<T>(name: &str, item: &T) -> syn::Result<Self>
    where
        T: has::Attrs + Spanned,
    {
        V::parse_attrs(name, item).map(Self::new)
    }
}

/// Filters the given `attrs` to contain [`syn::Attribute`]s only with the given
/// `name`.
#[inline]
pub fn filter_by_name<'n: 'ret, 'a: 'ret, 'ret>(
    name: &'n str,
    attrs: &'a [syn::Attribute],
) -> impl Iterator<Item = &'a syn::Attribute> + 'ret {
    attrs.iter().filter(move |attr| path_eq_single(&attr.path, name))
}

/// Compares the given `path` with the one-segment string `value` to be equal.
#[inline]
#[must_use]
fn path_eq_single(path: &syn::Path, value: &str) -> bool {
    path.segments.len() == 1 && path.segments[0].ident == value
}

pub mod field {
    //! Batteries for working with [`Attrs`]' fields.
    //!
    //! [`Attrs`]: super::Attrs

    use sealed::sealed;

    use crate::field;

    use super::{Dedup, Kind};

    /// Applying a value to a [`field::Container`] according to a parsing
    /// [`Kind`] and [`Dedup`]lication strategy.
    pub trait TryApply<V, K: Kind + ?Sized, D: Dedup + ?Sized>:
        field::Container<V>
    {
        /// Applies the provided `value` to this [`field::Container`].
        ///
        /// # Errors
        ///
        /// If this [`field::Container`] refuses to apply the `value` according
        /// to the [`Dedup`]lication strategy.
        fn try_apply(&mut self, value: V) -> syn::Result<()>;
    }

    /// Applying a value to a [`field::Container`] according to a parsing
    /// [`Kind`] and [`Dedup`]lication strategy from another
    /// [`field::Container`].
    pub trait TryApplySelf<V, K: Kind + ?Sized, D: Dedup + ?Sized>:
        TryApply<V, K, D>
    {
        /// Applies the value extracted from `another` [`field::Container`] to
        /// this [`field::Container`].
        ///
        /// # Errors
        ///
        /// If this [`field::Container`] refuses to apply the extracted value
        /// according to the [`Dedup`]lication strategy.
        fn try_apply_self(&mut self, another: Self) -> syn::Result<()>;
    }

    mod option {
        //! [`TryApply`] impls for [`Option`].

        use syn::spanned::Spanned;

        use crate::field::Container as _;

        use super::{
            super::{dedup, err, kind, Dedup, Kind},
            TryApply, TryApplySelf,
        };

        impl<V, K> TryApply<V, K, dedup::Unique> for Option<V>
        where
            V: Spanned,
            K: Kind + kind::Single + ?Sized,
        {
            fn try_apply(&mut self, val: V) -> syn::Result<()> {
                if self.has(&val) {
                    return Err(err::dup_attr_arg(&val));
                }
                self.set(val);
                Ok(())
            }
        }

        impl<V, K> TryApply<V, K, dedup::First> for Option<V>
        where
            K: Kind + kind::Single + ?Sized,
        {
            fn try_apply(&mut self, val: V) -> syn::Result<()> {
                if !self.has(&val) {
                    self.set(val);
                }
                Ok(())
            }
        }

        impl<V, K> TryApply<V, K, dedup::Last> for Option<V>
        where
            K: Kind + kind::Single + ?Sized,
        {
            fn try_apply(&mut self, val: V) -> syn::Result<()> {
                self.set(val);
                Ok(())
            }
        }

        impl<V, K, D> TryApplySelf<V, K, D> for Option<V>
        where
            V: Spanned,
            K: Kind + kind::Single + ?Sized,
            D: Dedup + ?Sized,
            Self: TryApply<V, K, D>,
        {
            fn try_apply_self(&mut self, another: Self) -> syn::Result<()> {
                if let Some(val) = another {
                    self.try_apply(val)?;
                }
                Ok(())
            }
        }
    }

    mod required {
        //! [`TryApply`] impls for [`Required`].

        use syn::spanned::Spanned;

        use crate::{field::Container as _, Required};

        use super::{
            super::{dedup, err, kind, Dedup, Kind},
            TryApply, TryApplySelf,
        };

        impl<V, K> TryApply<V, K, dedup::Unique> for Required<V>
        where
            V: Spanned,
            K: Kind + kind::Single + ?Sized,
        {
            fn try_apply(&mut self, val: V) -> syn::Result<()> {
                if self.has(&val) {
                    return Err(err::dup_attr_arg(&val));
                }
                self.set(val);
                Ok(())
            }
        }

        impl<V, K> TryApply<V, K, dedup::First> for Required<V>
        where
            K: Kind + kind::Single + ?Sized,
        {
            fn try_apply(&mut self, val: V) -> syn::Result<()> {
                if !self.has(&val) {
                    self.set(val);
                }
                Ok(())
            }
        }

        impl<V, K> TryApply<V, K, dedup::Last> for Required<V>
        where
            K: Kind + kind::Single + ?Sized,
        {
            fn try_apply(&mut self, val: V) -> syn::Result<()> {
                self.set(val);
                Ok(())
            }
        }

        impl<V, K, D> TryApplySelf<V, K, D> for Required<V>
        where
            V: Spanned,
            K: Kind + kind::Single + ?Sized,
            D: Dedup + ?Sized,
            Self: TryApply<V, K, D>,
        {
            fn try_apply_self(&mut self, mut another: Self) -> syn::Result<()> {
                if let Some(val) = another.take() {
                    self.try_apply(val)?;
                }
                Ok(())
            }
        }
    }

    mod vec {
        //! [`TryApply`] impls for [`Vec`].

        use syn::spanned::Spanned;

        use crate::field::Container as _;

        use super::{
            super::{dedup, err, kind, Dedup},
            TryApply, TryApplySelf,
        };

        impl<V> TryApply<V, kind::Nested, dedup::Unique> for Vec<V>
        where
            V: Spanned + PartialEq,
        {
            fn try_apply(&mut self, val: V) -> syn::Result<()> {
                if self.has(&val) {
                    return Err(err::dup_attr_arg(&val));
                }
                self.set(val);
                Ok(())
            }
        }

        impl<V: PartialEq> TryApply<V, kind::Nested, dedup::First> for Vec<V> {
            fn try_apply(&mut self, val: V) -> syn::Result<()> {
                if !self.has(&val) {
                    self.set(val);
                }
                Ok(())
            }
        }

        impl<V: PartialEq> TryApply<V, kind::Nested, dedup::Last> for Vec<V> {
            fn try_apply(&mut self, val: V) -> syn::Result<()> {
                self.set(val);
                Ok(())
            }
        }

        impl<V, D> TryApplySelf<V, kind::Nested, D> for Vec<V>
        where
            V: Spanned + PartialEq,
            D: Dedup + ?Sized,
            Self: TryApply<V, kind::Nested, D>,
        {
            fn try_apply_self(&mut self, another: Self) -> syn::Result<()> {
                for val in another {
                    self.try_apply(val)?;
                }
                Ok(())
            }
        }

        impl<V> TryApply<V, kind::Value, dedup::Unique> for Vec<V>
        where
            V: Spanned + PartialEq,
        {
            fn try_apply(&mut self, val: V) -> syn::Result<()> {
                if self.has(&val) {
                    return Err(err::dup_attr_arg(&val));
                }
                self.set(val);
                Ok(())
            }
        }

        impl<V: PartialEq> TryApply<V, kind::Value, dedup::First> for Vec<V> {
            fn try_apply(&mut self, val: V) -> syn::Result<()> {
                if !self.has(&val) {
                    self.set(val);
                }
                Ok(())
            }
        }

        impl<V: PartialEq> TryApply<V, kind::Value, dedup::Last> for Vec<V> {
            fn try_apply(&mut self, val: V) -> syn::Result<()> {
                self.set(val);
                Ok(())
            }
        }

        impl<V, D> TryApplySelf<V, kind::Value, D> for Vec<V>
        where
            V: Spanned + PartialEq,
            D: Dedup + ?Sized,
            Self: TryApply<V, kind::Value, D>,
        {
            fn try_apply_self(&mut self, another: Self) -> syn::Result<()> {
                for val in another {
                    self.try_apply(val)?;
                }
                Ok(())
            }
        }
    }

    mod hashset {
        //! [`TryApply`] impls for [`HashSet`].

        use std::{
            collections::HashSet,
            hash::{BuildHasher, Hash},
        };

        use syn::spanned::Spanned;

        use crate::field::Container as _;

        use super::{
            super::{dedup, err, kind, Dedup},
            TryApply, TryApplySelf,
        };

        impl<V, S> TryApply<V, kind::Nested, dedup::Unique> for HashSet<V, S>
        where
            V: Spanned + Eq + Hash,
            S: BuildHasher,
        {
            fn try_apply(&mut self, val: V) -> syn::Result<()> {
                if self.has(&val) {
                    return Err(err::dup_attr_arg(&val));
                }
                self.set(val);
                Ok(())
            }
        }

        impl<V, S> TryApply<V, kind::Nested, dedup::First> for HashSet<V, S>
        where
            V: Eq + Hash,
            S: BuildHasher,
        {
            fn try_apply(&mut self, val: V) -> syn::Result<()> {
                if !self.has(&val) {
                    self.set(val);
                }
                Ok(())
            }
        }

        impl<V, S> TryApply<V, kind::Nested, dedup::Last> for HashSet<V, S>
        where
            V: Eq + Hash,
            S: BuildHasher,
        {
            fn try_apply(&mut self, val: V) -> syn::Result<()> {
                self.set(val);
                Ok(())
            }
        }

        impl<V, S, D> TryApplySelf<V, kind::Nested, D> for HashSet<V, S>
        where
            V: Spanned + Eq + Hash,
            D: Dedup + ?Sized,
            S: BuildHasher,
            Self: TryApply<V, kind::Nested, D>,
        {
            fn try_apply_self(&mut self, another: Self) -> syn::Result<()> {
                for val in another {
                    self.try_apply(val)?;
                }
                Ok(())
            }
        }

        impl<V, S> TryApply<V, kind::Value, dedup::Unique> for HashSet<V, S>
        where
            V: Spanned + Eq + Hash,
            S: BuildHasher,
        {
            fn try_apply(&mut self, val: V) -> syn::Result<()> {
                if self.has(&val) {
                    return Err(err::dup_attr_arg(&val));
                }
                self.set(val);
                Ok(())
            }
        }

        impl<V, S> TryApply<V, kind::Value, dedup::First> for HashSet<V, S>
        where
            V: Eq + Hash,
            S: BuildHasher,
        {
            fn try_apply(&mut self, val: V) -> syn::Result<()> {
                if !self.has(&val) {
                    self.set(val);
                }
                Ok(())
            }
        }

        impl<V, S> TryApply<V, kind::Value, dedup::Last> for HashSet<V, S>
        where
            V: Eq + Hash,
            S: BuildHasher,
        {
            fn try_apply(&mut self, val: V) -> syn::Result<()> {
                self.set(val);
                Ok(())
            }
        }

        impl<V, S, D> TryApplySelf<V, kind::Value, D> for HashSet<V, S>
        where
            V: Spanned + Eq + Hash,
            D: Dedup + ?Sized,
            S: BuildHasher,
            Self: TryApply<V, kind::Value, D>,
        {
            fn try_apply_self(&mut self, another: Self) -> syn::Result<()> {
                for val in another {
                    self.try_apply(val)?;
                }
                Ok(())
            }
        }
    }

    mod btreeset {
        //! [`TryApply`] impls for [`BTreeSet`].

        use std::collections::BTreeSet;

        use syn::spanned::Spanned;

        use crate::field::Container as _;

        use super::{
            super::{dedup, err, kind, Dedup},
            TryApply, TryApplySelf,
        };

        impl<V> TryApply<V, kind::Nested, dedup::Unique> for BTreeSet<V>
        where
            V: Spanned + Ord,
        {
            fn try_apply(&mut self, val: V) -> syn::Result<()> {
                if self.has(&val) {
                    return Err(err::dup_attr_arg(&val));
                }
                self.set(val);
                Ok(())
            }
        }

        impl<V: Ord> TryApply<V, kind::Nested, dedup::First> for BTreeSet<V> {
            fn try_apply(&mut self, val: V) -> syn::Result<()> {
                if !self.has(&val) {
                    self.set(val);
                }
                Ok(())
            }
        }

        impl<V: Ord> TryApply<V, kind::Nested, dedup::Last> for BTreeSet<V> {
            fn try_apply(&mut self, val: V) -> syn::Result<()> {
                self.set(val);
                Ok(())
            }
        }

        impl<V, D> TryApplySelf<V, kind::Nested, D> for BTreeSet<V>
        where
            V: Spanned + Ord,
            D: Dedup + ?Sized,
            Self: TryApply<V, kind::Nested, D>,
        {
            fn try_apply_self(&mut self, another: Self) -> syn::Result<()> {
                for val in another {
                    self.try_apply(val)?;
                }
                Ok(())
            }
        }

        impl<V> TryApply<V, kind::Value, dedup::Unique> for BTreeSet<V>
        where
            V: Spanned + Ord,
        {
            fn try_apply(&mut self, val: V) -> syn::Result<()> {
                if self.has(&val) {
                    return Err(err::dup_attr_arg(&val));
                }
                self.set(val);
                Ok(())
            }
        }

        impl<V: Ord> TryApply<V, kind::Value, dedup::First> for BTreeSet<V> {
            fn try_apply(&mut self, val: V) -> syn::Result<()> {
                if !self.has(&val) {
                    self.set(val);
                }
                Ok(())
            }
        }

        impl<V: Ord> TryApply<V, kind::Value, dedup::Last> for BTreeSet<V> {
            fn try_apply(&mut self, val: V) -> syn::Result<()> {
                self.set(val);
                Ok(())
            }
        }

        impl<V, D> TryApplySelf<V, kind::Value, D> for BTreeSet<V>
        where
            V: Spanned + Ord,
            D: Dedup + ?Sized,
            Self: TryApply<V, kind::Value, D>,
        {
            fn try_apply_self(&mut self, another: Self) -> syn::Result<()> {
                for val in another {
                    self.try_apply(val)?;
                }
                Ok(())
            }
        }
    }

    mod hashmap {
        //! [`TryApply`] impls for [`HashMap`].

        use std::{
            collections::HashMap,
            hash::{BuildHasher, Hash},
        };

        use syn::spanned::Spanned;

        use crate::field::Container as _;

        use super::{
            super::{dedup, err, kind, Dedup},
            TryApply, TryApplySelf,
        };

        impl<K: Spanned + Eq + Hash, V, S: BuildHasher>
            TryApply<(K, V), kind::Map, dedup::Unique> for HashMap<K, V, S>
        {
            fn try_apply(&mut self, val: (K, V)) -> syn::Result<()> {
                if self.has(&val) {
                    return Err(err::dup_attr_arg(&val.0));
                }
                self.set(val);
                Ok(())
            }
        }

        impl<K: Eq + Hash, V, S: BuildHasher>
            TryApply<(K, V), kind::Map, dedup::First> for HashMap<K, V, S>
        {
            fn try_apply(&mut self, val: (K, V)) -> syn::Result<()> {
                if !self.has(&val) {
                    self.set(val);
                }
                Ok(())
            }
        }

        impl<K: Eq + Hash, V, S: BuildHasher>
            TryApply<(K, V), kind::Map, dedup::Last> for HashMap<K, V, S>
        {
            fn try_apply(&mut self, val: (K, V)) -> syn::Result<()> {
                self.set(val);
                Ok(())
            }
        }

        impl<K, V, D, S> TryApplySelf<(K, V), kind::Map, D> for HashMap<K, V, S>
        where
            K: Spanned + Eq + Hash,
            D: Dedup + ?Sized,
            S: BuildHasher,
            Self: TryApply<(K, V), kind::Map, D>,
        {
            fn try_apply_self(&mut self, another: Self) -> syn::Result<()> {
                for val in another {
                    self.try_apply(val)?;
                }
                Ok(())
            }
        }
    }

    mod btreemap {
        //! [`TryApply`] impls for [`BTreeMap`].

        use std::collections::BTreeMap;

        use syn::spanned::Spanned;

        use crate::field::Container as _;

        use super::{
            super::{dedup, err, kind, Dedup},
            TryApply, TryApplySelf,
        };

        impl<K, V> TryApply<(K, V), kind::Map, dedup::Unique> for BTreeMap<K, V>
        where
            K: Spanned + Ord,
        {
            fn try_apply(&mut self, val: (K, V)) -> syn::Result<()> {
                if self.has(&val) {
                    return Err(err::dup_attr_arg(&val.0));
                }
                self.set(val);
                Ok(())
            }
        }

        impl<K, V> TryApply<(K, V), kind::Map, dedup::First> for BTreeMap<K, V>
        where
            K: Ord,
        {
            fn try_apply(&mut self, val: (K, V)) -> syn::Result<()> {
                if !self.has(&val) {
                    self.set(val);
                }
                Ok(())
            }
        }

        impl<K, V> TryApply<(K, V), kind::Map, dedup::Last> for BTreeMap<K, V>
        where
            K: Ord,
        {
            fn try_apply(&mut self, val: (K, V)) -> syn::Result<()> {
                self.set(val);
                Ok(())
            }
        }

        impl<K, V, D> TryApplySelf<(K, V), kind::Map, D> for BTreeMap<K, V>
        where
            K: Spanned + Ord,
            D: Dedup + ?Sized,
            Self: TryApply<(K, V), kind::Map, D>,
        {
            fn try_apply_self(&mut self, another: Self) -> syn::Result<()> {
                for val in another {
                    self.try_apply(val)?;
                }
                Ok(())
            }
        }
    }

    /// [`TryApply`] and [`TryApplySelf`] traits' shim allowing to specify a
    /// parsing [`Kind`] and [`Dedup`]lication strategy as method's type
    /// parameters.
    #[sealed]
    pub trait TryMerge<V> {
        /// Merges the provided `value` to this [`field::Container`] with the
        /// specified parsing [`Kind`] and [`Dedup`]lication strategy.
        ///
        /// # Errors
        ///
        /// If this [`field::Container`] refuses to apply the `value` according
        /// to the [`Dedup`]lication strategy.
        fn try_merge<K, D>(&mut self, value: V) -> syn::Result<()>
        where
            Self: TryApply<V, K, D>,
            K: Kind + ?Sized,
            D: Dedup + ?Sized;

        /// Merges the value extracted from `another` [`field::Container`] to
        /// this [`field::Container`] with the specified parsing [`Kind`] and
        /// [`Dedup`]lication strategy.
        ///
        /// # Errors
        ///
        /// If this [`field::Container`] refuses to apply the extracted value
        /// according to the [`Dedup`]lication strategy.
        fn try_merge_self<K, D>(&mut self, another: Self) -> syn::Result<()>
        where
            Self: TryApplySelf<V, K, D> + Sized,
            K: Kind + ?Sized,
            D: Dedup + ?Sized;
    }

    #[sealed]
    impl<T: ?Sized, V> TryMerge<V> for T {
        #[inline]
        fn try_merge<K, D>(&mut self, val: V) -> syn::Result<()>
        where
            Self: TryApply<V, K, D>,
            K: Kind + ?Sized,
            D: Dedup + ?Sized,
        {
            <Self as TryApply<V, K, D>>::try_apply(self, val)
        }

        #[inline]
        fn try_merge_self<K, D>(&mut self, another: Self) -> syn::Result<()>
        where
            Self: TryApplySelf<V, K, D> + Sized,
            K: Kind + ?Sized,
            D: Dedup + ?Sized,
        {
            <Self as TryApplySelf<V, K, D>>::try_apply_self(self, another)
        }
    }
}

pub mod kind {
    //! Kinds of an [`Attrs`]' field parsing.
    //!
    //! [`Attrs`]: super::Attrs

    use sealed::sealed;

    /// Abstracted kind of an [`Attrs`]' field parsing into a
    /// [`field::Container`].
    ///
    /// [`Attrs`]: super::Attrs
    /// [`field::Container`]: crate::field::Container
    #[sealed]
    pub trait Kind {}

    /// [`Kind`]s allowing to parse only a single value of an [`Attrs`]' field.
    ///
    /// [`Attrs`]: super::Attrs
    #[sealed]
    pub trait Single {}

    /// [`Kind`] defining parsing an [`Attrs`]' field as a simple
    /// [`syn::Ident`].
    ///
    /// ```text
    /// #[attr(ident)]
    /// ```
    ///
    /// [`Attrs`]: super::Attrs
    /// [`syn::Ident`]: struct@syn::Ident
    #[derive(Clone, Copy, Debug)]
    pub enum Ident {}

    #[sealed]
    impl Kind for Ident {}

    #[sealed]
    impl Single for Ident {}

    /// [`Kind`] defining parsing an [`Attrs`]' field as nested [`Attrs`].
    ///
    /// ```text
    /// #[attr(nested(ident, key = val))]
    /// ```
    ///
    /// [`Attrs`]: super::Attrs
    #[derive(Clone, Copy, Debug)]
    pub enum Nested {}

    #[sealed]
    impl Kind for Nested {}

    #[sealed]
    impl Single for Nested {}

    /// [`Kind`] defining parsing an [`Attrs`]' field as values behind a
    /// [`syn::Ident`].
    ///
    /// ```text
    /// #[attr(ident = value)]
    /// #[attr(ident value)]
    /// #[attr(ident(value1, value2))]
    /// ```
    ///
    /// [`Attrs`]: super::Attrs
    /// [`syn::Ident`]: struct@syn::Ident
    #[derive(Clone, Copy, Debug)]
    pub enum Value {}

    #[sealed]
    impl Kind for Value {}

    #[sealed]
    impl Single for Value {}

    /// [`Kind`] defining parsing an [`Attrs`]' field as key-value pairs behind
    /// a [`syn::Ident`].
    ///
    /// ```text
    /// #[attr(ident key = value)]
    /// ```
    ///
    /// [`Attrs`]: super::Attrs
    /// [`syn::Ident`]: struct@syn::Ident
    #[derive(Clone, Copy, Debug)]
    pub enum Map {}

    #[sealed]
    impl Kind for Map {}
}

pub mod dedup {
    //! Deduplication strategies of an [`Attrs`]' field parsing.
    //!
    //! [`Attrs`]: super::Attrs

    use sealed::sealed;

    /// Abstracted deduplication strategy of an [`Attrs`]' field parsing into a
    /// [`field::Container`].
    ///
    /// [`Attrs`]: super::Attrs
    /// [`field::Container`]: crate::field::Container
    #[sealed]
    pub trait Dedup {}

    /// [`Dedup`]lication strategy allowing only a single value of an [`Attrs`]'
    /// field to appear.
    ///
    /// [`Attrs`]: super::Attrs
    #[derive(Clone, Copy, Debug)]
    pub enum Unique {}

    #[sealed]
    impl Dedup for Unique {}

    /// [`Dedup`]lication strategy picking only the first parsed value of an
    /// [`Attrs`]' field.
    ///
    /// [`Attrs`]: super::Attrs
    #[derive(Clone, Copy, Debug)]
    pub enum First {}

    #[sealed]
    impl Dedup for First {}

    /// [`Dedup`]lication strategy picking only the last parsed value of an
    /// [`Attrs`]' field.
    ///
    /// [`Attrs`]: super::Attrs
    #[derive(Clone, Copy, Debug)]
    pub enum Last {}

    #[sealed]
    impl Dedup for Last {}
}

pub mod validate {
    //! Validation machinery of an [`Attrs`]' field parsing.
    //!
    //! [`Attrs`]: super::Attrs

    use sealed::sealed;

    #[doc(inline)]
    pub use self::rule::Rule;

    /// Validation of a [`Rule`] during an [`Attrs`]' field parsing into a
    /// [`field::Container`].
    ///
    /// [`Attrs`]: super::Attrs
    /// [`field::Container`]: crate::field::Container
    pub trait Validation<R: Rule + ?Sized> {
        /// Checks whether the validation [`Rule`] is satisfied.
        ///
        /// # Errors
        ///
        /// If validation fails.
        fn validation(&self) -> syn::Result<()>;
    }

    mod option {
        use super::{rule, Validation};

        impl<V> Validation<rule::Provided> for Option<V> {
            #[inline]
            fn validation(&self) -> syn::Result<()> {
                Ok(())
            }
        }
    }

    mod required {
        use proc_macro2::Span;

        use crate::Required;

        use super::{rule, Validation};

        impl<V> Validation<rule::Provided> for Required<V> {
            #[inline]
            fn validation(&self) -> syn::Result<()> {
                self.is_present().then(|| ()).ok_or_else(|| {
                    syn::Error::new(
                        Span::call_site(),
                        "is expected to be present, but is absent",
                    )
                })
            }
        }
    }

    mod vec {
        use super::{rule, Validation};

        impl<V> Validation<rule::Provided> for Vec<V> {
            #[inline]
            fn validation(&self) -> syn::Result<()> {
                Ok(())
            }
        }
    }

    mod hashset {
        use std::{
            collections::HashSet,
            hash::{BuildHasher, Hash},
        };

        use super::{rule, Validation};

        impl<V, S> Validation<rule::Provided> for HashSet<V, S>
        where
            V: Eq + Hash,
            S: BuildHasher,
        {
            #[inline]
            fn validation(&self) -> syn::Result<()> {
                Ok(())
            }
        }
    }

    mod btreeset {
        use std::collections::BTreeSet;

        use super::{rule, Validation};

        impl<V: Ord> Validation<rule::Provided> for BTreeSet<V> {
            #[inline]
            fn validation(&self) -> syn::Result<()> {
                Ok(())
            }
        }
    }

    mod hashmap {
        use std::{
            collections::HashMap,
            hash::{BuildHasher, Hash},
        };

        use super::{rule, Validation};

        impl<K, V, S> Validation<rule::Provided> for HashMap<K, V, S>
        where
            K: Eq + Hash,
            S: BuildHasher,
        {
            #[inline]
            fn validation(&self) -> syn::Result<()> {
                Ok(())
            }
        }
    }

    mod btreemap {
        use std::collections::BTreeMap;

        use super::{rule, Validation};

        impl<K: Ord, V> Validation<rule::Provided> for BTreeMap<K, V> {
            #[inline]
            fn validation(&self) -> syn::Result<()> {
                Ok(())
            }
        }
    }

    /// [`Validation`] trait's shim allowing to specify its [`Rule`] as a
    /// method's type parameter.
    #[sealed]
    pub trait Validate {
        /// Checks whether the specified validation [`Rule`] is satisfied.
        ///
        /// # Errors
        ///
        /// If validation fails.
        fn validate<R: Rule + ?Sized>(&self) -> syn::Result<()>
        where
            Self: Validation<R>;
    }

    #[sealed]
    impl<T: ?Sized> Validate for T {
        #[inline]
        fn validate<R: Rule + ?Sized>(&self) -> syn::Result<()>
        where
            Self: Validation<R>,
        {
            self.validation()
        }
    }

    pub mod rule {
        //! Validation [`Rule`]s of an [`Attrs`]' field parsing.
        //!
        //! [`Attrs`]: super::super::Attrs

        use sealed::sealed;

        /// Abstracted validation rule of an [`Attrs`]' field parsing.
        ///
        /// [`Attrs`]: super::super::Attrs
        #[sealed]
        pub trait Rule {}

        /// Validation [`Rule`] verifying whether an [`Attrs`]' field has been
        /// provided for parsing.
        ///
        /// [`Attrs`]: super::super::Attrs
        #[derive(Clone, Copy, Debug)]
        pub enum Provided {}

        #[sealed]
        impl Rule for Provided {}
    }
}