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
use _um::{container_attributes::*, field_attributes::*, option::*};
use proc_macro::TokenStream;
use quote::{format_ident, quote};
use syn::{parse_macro_input, Data, DeriveInput, Field};

#[proc_macro_derive(Partial, attributes(partial))]
pub fn derive_partial(input: TokenStream) -> TokenStream {
    let DeriveInput {
        attrs,
        data,
        ident: full_ident,
        ..
    } = parse_macro_input!(input as DeriveInput);

    let ContainerAttributesData {
        ident: partial_ident,
        derives,
    } = container_attributes("partial", attrs, format_ident!("Partial{}", full_ident));

    let Data::Struct(data) = data else {
        panic!("Expected struct")
    };

    let mut static_assertions = Vec::new();
    let mut struct_body = Vec::new();
    let mut to_partial_body = Vec::new();
    let mut to_full_body = Vec::new();
    let mut impl_partial_eq = true;
    let mut partial_eq = Vec::new();

    for field in &data.fields {
        let Field {
            vis,
            ty,
            ident: full_ident,
            ..
        } = field;

        let full_ident = full_ident.clone().expect("Expected ident");

        let FieldAttributesData {
            ident: partial_ident,
            skip,
        } = field_attributes("partial", field);

        if skip {
            if is_option(ty) {
                to_full_body.push(quote! {
                    #full_ident: None,
                });
                partial_eq.push(quote! {
                    self.#full_ident.is_none()
                });
            } else {
                static_assertions.push(quote! {
                    ::utility_macros::_um::_sa::assert_impl_all!(#ty: Default);
                });
                to_full_body.push(quote! {
                    #full_ident: Default::default(),
                });
                impl_partial_eq = false;
            }
            continue;
        }

        let opt_ty = as_option(ty);
        struct_body.push(quote! {
            #vis #partial_ident: #opt_ty,
        });

        static_assertions.push(quote! {
            ::utility_macros::_um::_sa::assert_impl_all!(#opt_ty: Clone);
        });

        if is_option(ty) {
            to_partial_body.push(quote! {
                #partial_ident: self.#full_ident.clone(),
            });
            to_full_body.push(quote! {
                #full_ident: self.#partial_ident.clone(),
            });
            partial_eq.push(quote! {
                self.#full_ident == other.#partial_ident
            });
        } else {
            to_partial_body.push(quote! {
                #partial_ident: Some(self.#full_ident.clone()),
            });
            to_full_body.push(quote! {
                #full_ident: self.#partial_ident.clone().ok_or_else(|| ::utility_macros::_um::error::Error::MissingField(stringify!(#partial_ident)))?,
            });
            partial_eq.push(quote! {
                other.#partial_ident.clone().map_or(false, |val| self.#full_ident == val)
            });
        }
    }

    let derives = if derives.is_empty() {
        quote! {}
    } else {
        quote! {
            #[derive(#(#derives),*)]
        }
    };

    let partial_eq_impl = if impl_partial_eq {
        quote! {
            impl PartialEq<#partial_ident> for #full_ident {
                fn eq(&self, other: &#partial_ident) -> bool {
                    #(#partial_eq)&& *
                }
            }
        }
    } else {
        quote! {
            impl PartialEq<#partial_ident> for #full_ident {
                fn eq(&self, _: &#partial_ident) -> bool {
                    panic!("Partial equality can't be implemented for types that have skipped, non-optional fields");
                }
            }
        }
    };

    quote! {
        #(#static_assertions)*

        #derives
        pub struct #partial_ident {
            #(#struct_body)*
        }

        impl ::utility_macros::_um::partial::HasPartial for #full_ident {
            type Partial = #partial_ident;

            fn partial(&self) -> Self::Partial {
                Self::Partial {
                    #(#to_partial_body)*
                }
            }
        }

        impl ::utility_macros::_um::partial::Partial for #partial_ident {
            type Full = #full_ident;

            fn full(&self) -> ::utility_macros::_um::error::Result<Self::Full> {
                Ok(#full_ident {
                    #(#to_full_body)*
                })
            }
        }

        impl From<#full_ident> for #partial_ident {
            fn from(full: #full_ident) -> Self {
                ::utility_macros::_um::partial::HasPartial::partial(&full)
            }
        }

        impl TryFrom<#partial_ident> for #full_ident {
            type Error = ::utility_macros::_um::error::Error;

            fn try_from(partial: #partial_ident) -> ::utility_macros::_um::error::Result<Self> {
                ::utility_macros::_um::partial::Partial::full(&partial)
            }
        }

       #partial_eq_impl
    }
    .into()
}