Skip to main content

stylo_derive/
to_animated_value.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;
6use crate::{cg, to_computed_value};
7use proc_macro2::TokenStream;
8use syn::DeriveInput;
9use synstructure::BindStyle;
10
11pub fn derive(input: DeriveInput) -> TokenStream {
12    let trait_impl = |from_body, to_body| {
13        quote! {
14             #[inline]
15             fn from_animated_value(from: Self::AnimatedValue) -> Self {
16                 #from_body
17             }
18
19             #[inline]
20             fn to_animated_value(self, context: &crate::values::animated::Context) -> Self::AnimatedValue {
21                 #to_body
22             }
23        }
24    };
25
26    to_computed_value::derive_to_value(
27        input,
28        parse_quote!(crate::values::animated::ToAnimatedValue),
29        parse_quote!(AnimatedValue),
30        BindStyle::Move,
31        |binding| {
32            let attrs = cg::parse_field_attrs::<AnimationFieldAttrs>(&binding.ast());
33            to_computed_value::ToValueAttrs {
34                field_bound: attrs.field_bound,
35                no_field_bound: false,
36            }
37        },
38        |binding| quote!(crate::values::animated::ToAnimatedValue::from_animated_value(#binding)),
39        |binding| quote!(crate::values::animated::ToAnimatedValue::to_animated_value(#binding, context)),
40        trait_impl,
41    )
42}