zng_var_proc_macros/
transitionable.rs

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
use syn::{spanned::Spanned, *};

pub fn expand(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
    let input = parse_macro_input!(input as DeriveInput);

    let mut errors = crate::util::Errors::default();
    let mut lerp_block = quote!();

    let crate_ = quote!(::zng_var);
    let path = quote!(#crate_::animation::Transitionable);

    // params: self, to, step
    match input.data {
        Data::Struct(s) => match s.fields {
            Fields::Named(f) => {
                let f = f.named.iter().map(|f| {
                    let ident = &f.ident;
                    quote_spanned! {f.span()=>
                        #ident: #path::lerp(self.#ident, &to.#ident, step)
                    }
                });
                lerp_block = quote! {
                    {
                        Self {
                            #(#f,)*
                        }
                    }
                }
            }
            Fields::Unnamed(f) => {
                let f = f.unnamed.iter().enumerate().map(|(i, f)| {
                    let index = Index::from(i);
                    quote_spanned! {f.span()=>
                        #path::lerp(self.#index, &to.#index, step)
                    }
                });
                lerp_block = quote! {
                    {
                        Self(#(#f),*)
                    }
                }
            }
            Fields::Unit => {
                lerp_block = quote! {
                    {
                        let _ = (to, step);
                        self
                    }
                }
            }
        },
        Data::Enum(e) => {
            errors.push("cannot derive `Transitionable` for enums", e.enum_token.span);
        }
        Data::Union(u) => {
            errors.push("cannot derive `Transitionable` for unions", u.union_token.span);
        }
    }

    let out = if errors.is_empty() {
        let ident = input.ident;

        let mut generics = input.generics;
        for param in &mut generics.params {
            if let GenericParam::Type(ref mut type_param) = *param {
                type_param.bounds.push(parse_quote!(#path));
            }
        }
        let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();

        quote! {
            impl #impl_generics #path for #ident #ty_generics #where_clause {
                fn lerp(self, to: &Self, step: #crate_::animation::easing::EasingStep) -> Self #lerp_block
            }
        }
    } else {
        quote! {
            #errors
        }
    };

    out.into()
}