Skip to content

Commit 6311619

Browse files
committed
Refactor and improve error msg
1 parent fd161df commit 6311619

File tree

1 file changed

+45
-32
lines changed
  • mullvad-types/intersection-derive/src

1 file changed

+45
-32
lines changed

mullvad-types/intersection-derive/src/lib.rs

+45-32
Original file line numberDiff line numberDiff line change
@@ -3,47 +3,60 @@
33
extern crate proc_macro;
44

55
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};
97

108
/// Derive macro for the [`Intersection`] trait on structs.
119
#[proc_macro_derive(Intersection)]
1210
pub fn intersection_derive(item: TokenStream) -> TokenStream {
1311
let input = parse_macro_input!(item as DeriveInput);
1412

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()
2214
}
2315

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+
}
3831
}
3932

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+
})
4750
}
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+
}
4962
}

0 commit comments

Comments
 (0)