stylo_derive/
to_animated_zero.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use crate::animate::{AnimationFieldAttrs, AnimationInputAttrs, AnimationVariantAttrs};
6use crate::cg;
7use proc_macro2::TokenStream;
8use quote::TokenStreamExt;
9use syn;
10use synstructure;
11
12pub fn derive(mut input: syn::DeriveInput) -> TokenStream {
13    let animation_input_attrs = cg::parse_input_attrs::<AnimationInputAttrs>(&input);
14    let no_bound = animation_input_attrs.no_bound.unwrap_or_default();
15    let mut where_clause = input.generics.where_clause.take();
16    for param in input.generics.type_params() {
17        if !no_bound.iter().any(|name| name.is_ident(&param.ident)) {
18            cg::add_predicate(
19                &mut where_clause,
20                parse_quote!(#param: crate::values::animated::ToAnimatedZero),
21            );
22        }
23    }
24
25    let to_body = synstructure::Structure::new(&input).each_variant(|variant| {
26        let attrs = cg::parse_variant_attrs_from_ast::<AnimationVariantAttrs>(&variant.ast());
27        if attrs.error {
28            return Some(quote! { Err(()) });
29        }
30        let (mapped, mapped_bindings) = cg::value(variant, "mapped");
31        let bindings_pairs = variant.bindings().iter().zip(mapped_bindings);
32        let mut computations = quote!();
33        computations.append_all(bindings_pairs.map(|(binding, mapped_binding)| {
34            let field_attrs = cg::parse_field_attrs::<AnimationFieldAttrs>(&binding.ast());
35            if field_attrs.constant {
36                quote! {
37                    let #mapped_binding = std::clone::Clone::clone(#binding);
38                }
39            } else {
40                quote! {
41                    let #mapped_binding =
42                        crate::values::animated::ToAnimatedZero::to_animated_zero(#binding)?;
43                }
44            }
45        }));
46        computations.append_all(quote! { Ok(#mapped) });
47        Some(computations)
48    });
49    input.generics.where_clause = where_clause;
50
51    let name = &input.ident;
52    let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
53
54    quote! {
55        impl #impl_generics crate::values::animated::ToAnimatedZero for #name #ty_generics #where_clause {
56            #[allow(unused_variables)]
57            #[inline]
58            fn to_animated_zero(&self) -> Result<Self, ()> {
59                match *self {
60                    #to_body
61                }
62            }
63        }
64    }
65}