|
3 | 3 | extern crate proc_macro;
|
4 | 4 |
|
5 | 5 | use proc_macro::TokenStream;
|
6 |
| -use proc_macro2::TokenStream as TokenStream2; |
7 |
| -use quote::{quote, TokenStreamExt}; |
8 |
| -use syn::{parse_macro_input, spanned::Spanned, DeriveInput, Error}; |
| 6 | +use syn::{parse_macro_input, DeriveInput}; |
9 | 7 |
|
10 | 8 | /// Derive macro for the [`Intersection`] trait on structs.
|
11 | 9 | #[proc_macro_derive(Intersection)]
|
12 | 10 | pub fn intersection_derive(item: TokenStream) -> TokenStream {
|
13 | 11 | let input = parse_macro_input!(item as DeriveInput);
|
14 | 12 |
|
15 |
| - match &input.data { |
16 |
| - syn::Data::Struct(data) => derive_for_struct(&input, data), |
17 |
| - syn::Data::Enum(_) => todo!(), |
18 |
| - syn::Data::Union(_) => todo!(), |
19 |
| - } |
20 |
| - .unwrap_or_else(Error::into_compile_error) |
21 |
| - .into() |
| 13 | + inner::derive(input).into() |
22 | 14 | }
|
23 | 15 |
|
24 |
| -fn derive_for_struct(input: &DeriveInput, data: &syn::DataStruct) -> syn::Result<TokenStream2> { |
25 |
| - let my_type = &input.ident; |
26 |
| - let mut field_conversions = quote! {}; |
27 |
| - for field in &data.fields { |
28 |
| - let Some(name) = &field.ident else { |
29 |
| - return Err(syn::Error::new( |
30 |
| - field.span(), |
31 |
| - "Tuple structs are not currently supported", |
32 |
| - )); |
33 |
| - }; |
34 |
| - |
35 |
| - field_conversions.append_all(quote! { |
36 |
| - #name: Intersection::intersection(self.#name, other.#name)?, |
37 |
| - }) |
| 16 | +mod inner { |
| 17 | + use proc_macro2::TokenStream; |
| 18 | + use quote::{quote, TokenStreamExt}; |
| 19 | + use syn::{spanned::Spanned, DeriveInput, Error}; |
| 20 | + |
| 21 | + pub(crate) fn derive(input: DeriveInput) -> TokenStream { |
| 22 | + if let syn::Data::Struct(data) = &input.data { |
| 23 | + derive_for_struct(&input, data).unwrap_or_else(Error::into_compile_error) |
| 24 | + } else { |
| 25 | + syn::Error::new( |
| 26 | + input.span(), |
| 27 | + "Deriving `Intersection` is only supported for structs", |
| 28 | + ) |
| 29 | + .into_compile_error() |
| 30 | + } |
38 | 31 | }
|
39 | 32 |
|
40 |
| - Ok(quote! { |
41 |
| - impl Intersection for #my_type { |
42 |
| - fn intersection(self, other: Self) -> ::core::option::Option<Self> { |
43 |
| - ::core::option::Option::Some(Self { |
44 |
| - #field_conversions |
45 |
| - }) |
46 |
| - } |
| 33 | + pub(crate) fn derive_for_struct( |
| 34 | + input: &DeriveInput, |
| 35 | + data: &syn::DataStruct, |
| 36 | + ) -> syn::Result<TokenStream> { |
| 37 | + let my_type = &input.ident; |
| 38 | + let mut field_conversions = quote! {}; |
| 39 | + for field in &data.fields { |
| 40 | + let Some(name) = &field.ident else { |
| 41 | + return Err(syn::Error::new( |
| 42 | + field.span(), |
| 43 | + "Tuple structs are not currently supported", |
| 44 | + )); |
| 45 | + }; |
| 46 | + |
| 47 | + field_conversions.append_all(quote! { |
| 48 | + #name: Intersection::intersection(self.#name, other.#name)?, |
| 49 | + }) |
47 | 50 | }
|
48 |
| - }) |
| 51 | + |
| 52 | + Ok(quote! { |
| 53 | + impl Intersection for #my_type { |
| 54 | + fn intersection(self, other: Self) -> ::core::option::Option<Self> { |
| 55 | + ::core::option::Option::Some(Self { |
| 56 | + #field_conversions |
| 57 | + }) |
| 58 | + } |
| 59 | + } |
| 60 | + }) |
| 61 | + } |
49 | 62 | }
|
0 commit comments