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
5 use darling::util::PathList;
6 use derive_common::cg;
7 use proc_macro2::TokenStream;
8 use quote::TokenStreamExt;
9 use syn::{DeriveInput, WhereClause};
10 use synstructure::{Structure, VariantInfo};
11
derive(mut input: DeriveInput) -> TokenStream12 pub fn derive(mut input: DeriveInput) -> TokenStream {
13 let animation_input_attrs = cg::parse_input_attrs::<AnimationInputAttrs>(&input);
14
15 let no_bound = animation_input_attrs.no_bound.unwrap_or_default();
16 let mut where_clause = input.generics.where_clause.take();
17 for param in input.generics.type_params() {
18 if !no_bound.iter().any(|name| name.is_ident(¶m.ident)) {
19 cg::add_predicate(
20 &mut where_clause,
21 parse_quote!(#param: crate::values::animated::Animate),
22 );
23 }
24 }
25 let (mut match_body, needs_catchall_branch) = {
26 let s = Structure::new(&input);
27 let needs_catchall_branch = s.variants().len() > 1;
28 let match_body = s.variants().iter().fold(quote!(), |body, variant| {
29 let arm = derive_variant_arm(variant, &mut where_clause);
30 quote! { #body #arm }
31 });
32 (match_body, needs_catchall_branch)
33 };
34
35 input.generics.where_clause = where_clause;
36
37 if needs_catchall_branch {
38 // This ideally shouldn't be needed, but see
39 // https://github.com/rust-lang/rust/issues/68867
40 match_body.append_all(quote! { _ => unsafe { debug_unreachable!() } });
41 }
42
43 let name = &input.ident;
44 let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
45
46 quote! {
47 impl #impl_generics crate::values::animated::Animate for #name #ty_generics #where_clause {
48 #[allow(unused_variables, unused_imports)]
49 #[inline]
50 fn animate(
51 &self,
52 other: &Self,
53 procedure: crate::values::animated::Procedure,
54 ) -> Result<Self, ()> {
55 if std::mem::discriminant(self) != std::mem::discriminant(other) {
56 return Err(());
57 }
58 match (self, other) {
59 #match_body
60 }
61 }
62 }
63 }
64 }
65
derive_variant_arm( variant: &VariantInfo, where_clause: &mut Option<WhereClause>, ) -> TokenStream66 fn derive_variant_arm(
67 variant: &VariantInfo,
68 where_clause: &mut Option<WhereClause>,
69 ) -> TokenStream {
70 let variant_attrs = cg::parse_variant_attrs_from_ast::<AnimationVariantAttrs>(&variant.ast());
71 let (this_pattern, this_info) = cg::ref_pattern(&variant, "this");
72 let (other_pattern, other_info) = cg::ref_pattern(&variant, "other");
73
74 if variant_attrs.error {
75 return quote! {
76 (&#this_pattern, &#other_pattern) => Err(()),
77 };
78 }
79
80 let (result_value, result_info) = cg::value(&variant, "result");
81 let mut computations = quote!();
82 let iter = result_info.iter().zip(this_info.iter().zip(&other_info));
83 computations.append_all(iter.map(|(result, (this, other))| {
84 let field_attrs = cg::parse_field_attrs::<AnimationFieldAttrs>(&result.ast());
85 if field_attrs.field_bound {
86 let ty = &this.ast().ty;
87 cg::add_predicate(
88 where_clause,
89 parse_quote!(#ty: crate::values::animated::Animate),
90 );
91 }
92 if field_attrs.constant {
93 quote! {
94 if #this != #other {
95 return Err(());
96 }
97 let #result = std::clone::Clone::clone(#this);
98 }
99 } else {
100 quote! {
101 let #result =
102 crate::values::animated::Animate::animate(#this, #other, procedure)?;
103 }
104 }
105 }));
106
107 quote! {
108 (&#this_pattern, &#other_pattern) => {
109 #computations
110 Ok(#result_value)
111 }
112 }
113 }
114
115 #[derive(Default, FromDeriveInput)]
116 #[darling(attributes(animation), default)]
117 pub struct AnimationInputAttrs {
118 pub no_bound: Option<PathList>,
119 }
120
121 #[derive(Default, FromVariant)]
122 #[darling(attributes(animation), default)]
123 pub struct AnimationVariantAttrs {
124 pub error: bool,
125 // Only here because of structs, where the struct definition acts as a
126 // variant itself.
127 pub no_bound: Option<PathList>,
128 }
129
130 #[derive(Default, FromField)]
131 #[darling(attributes(animation), default)]
132 pub struct AnimationFieldAttrs {
133 pub constant: bool,
134 pub field_bound: bool,
135 }
136