(input: TokenStream)
| 6 | use crate::{TypeSet, get_paths, struct_fields}; |
| 7 | |
| 8 | pub fn derive_diff(input: TokenStream) -> syn::Result<TokenStream2> { |
| 9 | let input: syn::DeriveInput = syn::parse(input)?; |
| 10 | let identifier = &input.ident; |
| 11 | let (firewheel_path, diff_path) = get_paths(); |
| 12 | |
| 13 | let (impl_generics, ty_generics, where_generics) = input.generics.split_for_impl(); |
| 14 | |
| 15 | fn generate_where( |
| 16 | where_clause: Option<&syn::WhereClause>, |
| 17 | bounds: &[TokenStream2], |
| 18 | ) -> TokenStream2 { |
| 19 | match where_clause { |
| 20 | Some(wg) => { |
| 21 | quote! { |
| 22 | #wg |
| 23 | #(#bounds,)* |
| 24 | } |
| 25 | } |
| 26 | None => { |
| 27 | quote! { |
| 28 | where #(#bounds,)* |
| 29 | } |
| 30 | } |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | let (body, where_generics) = match &input.data { |
| 35 | syn::Data::Struct(data) => { |
| 36 | let DiffOutput { body, bounds } = DiffOutput::from_struct(data, &diff_path)?; |
| 37 | |
| 38 | (body, generate_where(where_generics, &bounds)) |
| 39 | } |
| 40 | syn::Data::Enum(data) => { |
| 41 | let DiffOutput { body, bounds } = |
| 42 | DiffOutput::from_enum(identifier, data, &firewheel_path, &diff_path)?; |
| 43 | |
| 44 | (body, generate_where(where_generics, &bounds)) |
| 45 | } |
| 46 | syn::Data::Union(_) => { |
| 47 | return Err(syn::Error::new( |
| 48 | input.span(), |
| 49 | "`Diff` cannot be derived on unions.", |
| 50 | )); |
| 51 | } |
| 52 | }; |
| 53 | |
| 54 | Ok(quote! { |
| 55 | #[automatically_derived] |
| 56 | impl #impl_generics #diff_path::Diff for #identifier #ty_generics #where_generics { |
| 57 | fn diff<__E: #diff_path::EventQueue>(&self, baseline: &Self, path: #diff_path::PathBuilder, event_queue: &mut __E) { |
| 58 | #body |
| 59 | } |
| 60 | } |
| 61 | }) |
| 62 | } |
| 63 | |
| 64 | struct DiffOutput { |
| 65 | body: TokenStream2, |
nothing calls this directly
no test coverage detected