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
use proc_macro::TokenStream;
use quote::{quote, quote_spanned};
use syn::{parse_macro_input, spanned::Spanned, Field, Fields, Index, ItemStruct};

#[proc_macro_derive(Lerp)]
pub fn lerp_derive(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as ItemStruct);

    let name = &input.ident;

    TokenStream::from(match input.fields {
        Fields::Named(fields) => {
            if let Some(Field { ty, .. }) = fields.named.first() {
                let fields = fields.named.iter().map(|f| {
                    if let Some(name) = f.ident.as_ref() {
                        quote! {
                            #name: self.#name.lerp(other.#name, t)
                        }
                    } else {
                        quote_spanned! {f.ident.span()=>
                            compile_error!("All fields must be named in the struct");
                        }
                    }
                });

                quote! {
                    #[automatically_derived]
                    impl Lerp<#ty> for #name {
                        fn lerp(self, other: Self, t: #ty) -> Self {
                            Self {
                                #(#fields),*
                            }
                        }
                    }
                }
            } else {
                quote_spanned! {fields.named.span()=>
                    compile_error!("Struct must have one or more fields");
                }
            }
        }
        Fields::Unnamed(fields) => {
            if let Some(Field { ty, .. }) = fields.unnamed.first() {
                let fields = fields.unnamed.iter().enumerate().map(|(i, _)| {
                    let name = Index::from(i);

                    quote! {
                        self.#name.lerp(other.#name, t)
                    }
                });

                quote! {
                    #[automatically_derived]
                    impl Lerp<#ty> for #name {
                        fn lerp(self, other: Self, t: #ty) -> Self {
                            Self (
                                #(#fields),*
                            )
                        }
                    }
                }
            } else {
                quote_spanned! {fields.unnamed.span()=>
                    compile_error!("Struct must have one or more fields");
                }
            }
        }
        _ => quote_spanned! {input.span()=>
            compile_error!("Struct must have fields");
        },
    })
}