Skip to content

Commit

Permalink
Fix output when no functions provided in input
Browse files Browse the repository at this point in the history
  • Loading branch information
speelbarrow committed Jul 25, 2023
1 parent 9497760 commit 35e2ee5
Showing 1 changed file with 23 additions and 17 deletions.
40 changes: 23 additions & 17 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@ impl Enum {
}
}
```
You can also create an empty `enum` by not providing any functions in the `impl` block (though I'm not sure why you
would want to do this).
```
# use enum_from_functions::enum_from_functions;
#[enum_from_functions]
impl EmptyEnum {}
```
If you need to export the generated `enum` type out of its parent module, provide the `pub` argument to the macro
attribute.
```
Expand Down Expand Up @@ -156,7 +163,7 @@ impl Enum {
use convert_case::{Case, Casing};
use proc_macro::{Span, TokenStream};
use syn::{
parse_macro_input,
parse_macro_input, parse_quote,
punctuated::{Pair, Punctuated},
token::Comma,
FnArg, ImplItem, Pat,
Expand Down Expand Up @@ -238,37 +245,36 @@ pub fn enum_from_functions(args: TokenStream, input: TokenStream) -> TokenStream
}

let enum_name = &parsed_impl.self_ty;
let (map_sig, arg_names) = first_sig.map_or((None, None), |some| {
let mut r_sig = some.clone();
r_sig.ident = syn::Ident::new("map", Span::call_site().into());
r_sig.inputs.insert(0, syn::parse_quote!(self));
if let Some(item) = first_sig.map(|some| {
let mut sig = some.clone();
sig.ident = syn::Ident::new("map", Span::call_site().into());
sig.inputs.insert(0, syn::parse_quote!(self));

let r_args = Punctuated::<&Box<Pat>, Comma>::from_iter(some.inputs.pairs().map(|pair| {
let args = Punctuated::<&Box<Pat>, Comma>::from_iter(some.inputs.pairs().map(|pair| {
match pair.value() {
FnArg::Typed(arg) => Pair::new(&arg.pat, pair.punct().map(|_| Comma::default())),
FnArg::Receiver(_) => unreachable!(),
}
}));

(Some(r_sig), Some(r_args))
});
parse_quote! {
pub #sig {
match self {
#(Self::#variants => Self::#function_names(#args)),*
}
}
}
}) {
parsed_impl.items.push(item)
}
let out = quote::quote! {
#(#attrs)*
#parsed_pub enum #enum_name {
#(#variants),*
}

#parsed_impl

impl #enum_name {
pub #map_sig {
match self {
#(Self::#variants => Self::#function_names (#arg_names)),*
}
}
}
};

dbg!(&out.to_string());
out.into()
}

0 comments on commit 35e2ee5

Please sign in to comment.