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
mod models;

use models::{FieldAttributeBuilder, TypeAttributeBuilder};
use quote::quote;
use syn::{Data, DeriveInput, Meta};

use super::TraitHandler;
use crate::Trait;

pub(crate) struct EqHandler;

impl TraitHandler for EqHandler {
    #[inline]
    fn trait_meta_handler(
        ast: &mut DeriveInput,
        token_stream: &mut proc_macro2::TokenStream,
        traits: &[Trait],
        meta: &Meta,
    ) -> syn::Result<()> {
        #[cfg(feature = "PartialEq")]
        let contains_partial_eq = traits.contains(&Trait::PartialEq);

        #[cfg(not(feature = "PartialEq"))]
        let contains_partial_eq = false;

        let type_attribute = TypeAttributeBuilder {
            enable_flag:  true,
            enable_bound: !contains_partial_eq,
        }
        .build_from_eq_meta(meta)?;

        // if `contains_partial_eq` is true, the implementation is handled by the `PartialEq` attribute, and field attributes is also handled by the `PartialEq` attribute
        if !contains_partial_eq {
            match &ast.data {
                Data::Struct(data) => {
                    for field in data.fields.iter() {
                        let _ =
                            FieldAttributeBuilder.build_from_attributes(&field.attrs, traits)?;
                    }
                },
                Data::Enum(data) => {
                    for variant in data.variants.iter() {
                        let _ = TypeAttributeBuilder {
                            enable_flag: false, enable_bound: false
                        }
                        .build_from_attributes(&variant.attrs, traits)?;

                        for field in variant.fields.iter() {
                            let _ = FieldAttributeBuilder
                                .build_from_attributes(&field.attrs, traits)?;
                        }
                    }
                },
                Data::Union(data) => {
                    for field in data.fields.named.iter() {
                        let _ =
                            FieldAttributeBuilder.build_from_attributes(&field.attrs, traits)?;
                    }
                },
            }

            let ident = &ast.ident;

            /*
                #[derive(PartialEq)]
                struct B<T> {
                    f1: PhantomData<T>,
                }

                impl<T> Eq for B<T> {

                }

                // The above code will throw a compile error because T have to be bound to `PartialEq`. However, it seems not to be necessary logically.
            */
            let bound = type_attribute.bound.into_where_predicates_by_generic_parameters(
                &ast.generics.params,
                &syn::parse2(quote!(::core::cmp::PartialEq)).unwrap(),
            );

            let where_clause = ast.generics.make_where_clause();

            for where_predicate in bound {
                where_clause.predicates.push(where_predicate);
            }

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

            token_stream.extend(quote! {
                impl #impl_generics ::core::cmp::Eq for #ident #ty_generics #where_clause {
                }
            });
        }

        Ok(())
    }
}