1 use proc_macro2::TokenStream;
3 use syn::{parse_macro_input, Data, DeriveInput};
5 /// Generates implementation ActivityHandler for an enum, which looks like the following (handling
6 /// all enum variants).
8 /// Based on this code:
10 /// #[derive(serde::Deserialize, serde::Serialize, ActivityHandler)]
11 /// #[serde(untagged)]
12 /// pub enum PersonInboxActivities {
13 /// CreateNote(CreateNote),
14 /// UpdateNote(UpdateNote),
16 /// It will generate this:
18 /// impl ActivityHandler for PersonInboxActivities {
22 /// context: &LemmyContext,
23 /// request_counter: &mut i32,
24 /// ) -> Result<(), LemmyError> {
26 /// PersonInboxActivities::CreateNote(a) => a.verify(context, request_counter).await,
27 /// PersonInboxActivities::UpdateNote(a) => a.verify(context, request_counter).await,
33 /// context: &LemmyContext,
34 /// request_counter: &mut i32,
35 /// ) -> Result<(), LemmyError> {
37 /// PersonInboxActivities::CreateNote(a) => a.receive(context, request_counter).await,
38 /// PersonInboxActivities::UpdateNote(a) => a.receive(context, request_counter).await,
41 /// fn common(&self) -> &ActivityCommonFields {
43 /// PersonInboxActivities::CreateNote(a) => a.common(),
44 /// PersonInboxActivities::UpdateNote(a) => a.common(),
50 /// TODO: consider replacing this macro with https://crates.io/crates/typetag crate, though it
51 /// doesnt support untagged enums which we need for apub.
52 #[proc_macro_derive(ActivityHandler)]
53 pub fn derive_activity_handler(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
54 // Parse the input tokens into a syntax tree.
55 let input = parse_macro_input!(input as DeriveInput);
57 // Used in the quasi-quotation below as `#name`.
58 let name = input.ident;
60 let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
62 let input_enum = if let Data::Enum(d) = input.data {
68 let impl_verify = input_enum
71 .map(|variant| variant_impl_verify(&name, variant));
72 let impl_receive = input_enum
75 .map(|variant| variant_impl_receive(&name, variant));
76 let impl_common = input_enum
79 .map(|variant| variant_impl_common(&name, variant));
81 // The generated impl.
82 let expanded = quote! {
83 #[async_trait::async_trait(?Send)]
84 impl #impl_generics lemmy_apub_lib::ActivityHandler for #name #ty_generics #where_clause {
87 context: &LemmyContext,
88 request_counter: &mut i32,
89 ) -> Result<(), LemmyError> {
96 context: &LemmyContext,
97 request_counter: &mut i32,
98 ) -> Result<(), LemmyError> {
103 fn common(&self) -> &ActivityCommonFields {
111 // Hand the output tokens back to the compiler.
112 proc_macro::TokenStream::from(expanded)
115 fn variant_impl_common(name: &syn::Ident, variant: &syn::Variant) -> TokenStream {
116 let id = &variant.ident;
117 match &variant.fields {
118 syn::Fields::Unnamed(_) => {
120 #name::#id(a) => a.common(),
123 _ => unimplemented!(),
127 fn variant_impl_verify(name: &syn::Ident, variant: &syn::Variant) -> TokenStream {
128 let id = &variant.ident;
129 match &variant.fields {
130 syn::Fields::Unnamed(_) => {
132 #name::#id(a) => a.verify(context, request_counter).await,
135 _ => unimplemented!(),
139 fn variant_impl_receive(name: &syn::Ident, variant: &syn::Variant) -> TokenStream {
140 let id = &variant.ident;
141 match &variant.fields {
142 syn::Fields::Unnamed(_) => {
144 #name::#id(a) => a.receive(context, request_counter).await,
147 _ => unimplemented!(),