Skip to content

Commit

Permalink
feat: error on union types
Browse files Browse the repository at this point in the history
  • Loading branch information
gibbz00 committed Oct 15, 2024
1 parent 796f012 commit 02e513e
Showing 1 changed file with 19 additions and 18 deletions.
37 changes: 19 additions & 18 deletions crates/macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ fn derive_mock_impl(token_stream: TokenStream) -> TokenStream {
let self_definition_result = match type_definition.data {
Data::Struct(data_struct) => derive_struct(data_struct),
Data::Enum(data_enum) => derive_enum(data_enum),
Data::Union(_) => todo!(),
// TODO:
Data::Union(data_union) => Err(syn::Error::new(data_union.union_token.span, "union types not supported")),
};

match self_definition_result {
Expand All @@ -36,23 +37,6 @@ fn derive_mock_impl(token_stream: TokenStream) -> TokenStream {
.into()
}

mod fields {
use super::*;

pub fn named(named_fields: FieldsNamed) -> impl Iterator<Item = proc_macro2::TokenStream> {
let field_names = named_fields
.named
.into_iter()
.map(|field| field.ident.expect("encountered named field without an identifier"));

field_names.map(|field_name| quote! { #field_name: ::mock_default::Mock::mock() })
}

pub fn tuple(tuple_fields: syn::FieldsUnnamed) -> impl Iterator<Item = proc_macro2::TokenStream> {
tuple_fields.unnamed.into_iter().map(|_| quote! { ::mock_default::Mock::mock() })
}
}

fn derive_struct(data_struct: syn::DataStruct) -> syn::Result<proc_macro2::TokenStream> {
Ok(match data_struct.fields {
Fields::Named(named_fields) => {
Expand Down Expand Up @@ -125,3 +109,20 @@ fn derive_enum(data_enum: syn::DataEnum) -> syn::Result<proc_macro2::TokenStream
}
})
}

mod fields {
use super::*;

pub fn named(named_fields: FieldsNamed) -> impl Iterator<Item = proc_macro2::TokenStream> {
let field_names = named_fields
.named
.into_iter()
.map(|field| field.ident.expect("encountered named field without an identifier"));

field_names.map(|field_name| quote! { #field_name: ::mock_default::Mock::mock() })
}

pub fn tuple(tuple_fields: syn::FieldsUnnamed) -> impl Iterator<Item = proc_macro2::TokenStream> {
tuple_fields.unnamed.into_iter().map(|_| quote! { ::mock_default::Mock::mock() })
}
}

0 comments on commit 02e513e

Please sign in to comment.