cushy_macros/
animation.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
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
use manyhow::{bail, ensure};
use quote::ToTokens;
use syn::{Data, DeriveInput, Field, Variant};

use crate::*;

pub fn linear_interpolate(
    DeriveInput {
        ident: item_ident,
        generics,
        data,
        ..
    }: DeriveInput,
) -> Result<TokenStream> {
    if let Some(generic) = generics.type_params().next() {
        bail!(generic, "generics not supported");
    }

    let doc;

    let body = match data {
        Data::Struct(data) => {
            let fields = match data.fields {
                syn::Fields::Unit => bail!(item_ident, "unit structs are not supported"),
                fields => fields
                    .into_iter()
                    .enumerate()
                    .map(|(idx, Field {  ident, .. })| {
                        let ident = ident
                            .map(ToTokens::into_token_stream)
                            .unwrap_or_else(|| proc_macro2::Literal::usize_unsuffixed(idx).into_token_stream());
                            quote!(#ident: ::cushy::animation::LinearInterpolate::lerp(&self.#ident, &__target.#ident, __percent),)
                    }),
            };
            doc = "# Panics\n Panics if any field's lerp panics (this should only happen on percentages outside 0..1 range).";
            quote!(#item_ident{#(#fields)*})
        }
        Data::Enum(data) => {
            let variants = data
                .variants
                .into_iter()
                .map(
                    |Variant {
                         ident,
                         fields,
                         discriminant,
                         ..
                     }| {
                        if let Some(discriminant) = discriminant {
                            bail!(discriminant, "discriminants are not supported");
                        }
                        ensure!(fields.is_empty(), fields, "enum fields are not supported");
                        Ok(quote!(#item_ident::#ident #fields))
                    },
                )
                .collect::<Result<Vec<_>>>()?;
            let last = variants
                .last()
                .map(ToTokens::to_token_stream)
                .unwrap_or_else(|| quote!(unreachable!()));

            let idx: Vec<_> = (0..variants.len()).collect();
            doc = "# Panics\n Panics if the the enum variants are overflown (this can only happen on percentages outside 0..1 range).";
            quote! {
                # use ::cushy::animation::LinearInterpolate;
                fn variant_to_index(__v: &#item_ident) -> usize {
                    match __v {
                        #(#variants => #idx,)*
                    }
                }
                let __self = variant_to_index(&self);
                let __target = variant_to_index(&__target);
                match LinearInterpolate::lerp(&__self, &__target, __percent) {
                    #(#idx => #variants,)*
                    _ => #last,
                }
            }
        }
        Data::Union(union) => bail!((union.union_token, union.fields), "unions not supported"),
    };

    Ok(quote! {
        impl ::cushy::animation::LinearInterpolate for #item_ident {
            #[doc = #doc]
            fn lerp(&self, __target: &Self, __percent: f32) -> Self {
                #body
            }
        }
    })
}

#[cfg(test)]
mod test {
    use super::*;
    expansion_snapshot! {struct_
        #[derive(linear_interpolate)]
        struct HelloWorld {
            fielda: Hello,
            fieldb: World,
        }
    }
    expansion_snapshot! {tuple_struct
        #[derive(linear_interpolate)]
        struct HelloWorld(Hello, World);
    }
    expansion_snapshot! {enum_
        #[derive(linear_interpolate)]
        enum Enum{A, B}
    }
    expansion_snapshot! {empty_enum
        #[derive(linear_interpolate)]
        enum Enum{}
    }
}