use crate::parse::{ImplInfo, MethodInfo};
use quote::quote;
pub fn generate_trait(info: &ImplInfo) -> proc_macro2::TokenStream {
let impl_attrs = &info.attributes;
let handle_trait_ident = &info.handle_trait_ident;
let generics = &info.generics;
let where_clause = &info.generics.where_clause;
let methods = info.methods.iter().map(method_signature);
quote! {
#(#impl_attrs)*
pub trait #handle_trait_ident #generics #where_clause
{
#(#methods)*
}
}
}
pub fn generate_trait_impl(info: &ImplInfo) -> proc_macro2::TokenStream {
let impl_attrs = &info.attributes;
let handle_trait_ident = &info.handle_trait_ident;
let impl_type = &info.impl_type;
let (_, trait_generics, where_clause) = info.generics.split_for_impl();
let mut generics_with_broadcast = info.generics.clone();
generics_with_broadcast.params.push(syn::parse_quote!(__V));
let (impl_generics, _, _) = generics_with_broadcast.split_for_impl();
let call_prefix = build_call_prefix(info);
let methods = info
.methods
.iter()
.map(|m| method_body(m, &call_prefix, info));
quote! {
#(#impl_attrs)*
#[allow(unused_parens)]
impl #impl_generics #handle_trait_ident #trait_generics for actify::Handle<#impl_type, __V> #where_clause
{
#(#methods)*
}
}
}
fn method_signature(method: &MethodInfo) -> proc_macro2::TokenStream {
let attrs = &method.attributes;
let ident = &method.ident;
let arg_names: Vec<_> = method.arg_names.iter().collect();
let arg_types: Vec<_> = method.arg_types.iter().collect();
let method_generics = &method.method_generics;
let where_clause = &method.method_generics.where_clause;
let return_type = quote_return_type(&method.output_type);
quote! {
#(#attrs)*
async fn #ident #method_generics(&self, #(#arg_names: #arg_types),*) #return_type #where_clause;
}
}
fn method_body(
method: &MethodInfo,
call_prefix: &proc_macro2::TokenStream,
info: &ImplInfo,
) -> proc_macro2::TokenStream {
let attrs: Vec<_> = method
.attributes
.iter()
.filter(|a| !a.path().is_ident("deprecated") && !a.path().is_ident("must_use"))
.collect();
let ident = &method.ident;
let arg_names: Vec<_> = method.arg_names.iter().collect();
let arg_types: Vec<_> = method.arg_types.iter().collect();
let method_generics = &method.method_generics;
let where_clause = &method.method_generics.where_clause;
let impl_type = &info.impl_type;
let output_type = &method.output_type;
let return_type = quote_return_type(output_type);
let ident_string = format!("{}::{}", info.type_ident, ident);
let awaiter = if method.is_async {
Some(quote! { .await })
} else {
None
};
let mutability = if method.is_mutable {
Some(quote! { mut })
} else {
None
};
let broadcast = if method.skip_broadcast {
None
} else {
Some(quote! { __actify_s.broadcast(#ident_string); })
};
quote! {
#(#attrs)*
async fn #ident #method_generics(&self, #(#arg_names: #arg_types),*) #return_type #where_clause {
let __actify_res = self.send_job(
Box::new(|__actify_s: &mut actify::Actor<#impl_type>, __actify_args: Box<dyn std::any::Any + Send>|
Box::pin(async move {
let (#(#arg_names),*): (#(#arg_types),*) = *__actify_args
.downcast()
.expect("Downcasting failed due to an error in the Actify macro");
let __actify_result: #output_type = #call_prefix::#ident(&#mutability __actify_s.inner, #(#arg_names),*)#awaiter;
#broadcast
Box::new(__actify_result) as Box<dyn std::any::Any + Send>
})),
Box::new((#(#arg_names),*)),
).await;
*__actify_res.downcast().expect("Downcasting failed due to an error in the Actify macro")
}
}
}
fn build_call_prefix(info: &ImplInfo) -> proc_macro2::TokenStream {
let impl_type = &info.impl_type;
match &info.trait_path {
None => quote! { <#impl_type> },
Some(path) => quote! { <#impl_type as #path> },
}
}
fn quote_return_type(ty: &syn::Type) -> proc_macro2::TokenStream {
match ty {
syn::Type::Tuple(tuple) if tuple.elems.is_empty() => quote! {},
_ => quote! { -> #ty },
}
}