de-global services from admin

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-07-27 00:11:41 +00:00
parent 7a3cc3941e
commit 7e50db4193
37 changed files with 1131 additions and 1127 deletions

View file

@ -2,15 +2,27 @@ use itertools::Itertools;
use proc_macro::{Span, TokenStream};
use proc_macro2::TokenStream as TokenStream2;
use quote::{quote, ToTokens};
use syn::{Error, Fields, Ident, ItemEnum, Meta, Variant};
use syn::{parse_quote, Attribute, Error, Fields, Ident, ItemEnum, ItemFn, Meta, Variant};
use crate::{utils::camel_to_snake_string, Result};
pub(super) fn command(mut item: ItemFn, _args: &[Meta]) -> Result<TokenStream> {
let attr: Attribute = parse_quote! {
#[conduit_macros::implement(crate::Command, params = "<'_>")]
};
item.attrs.push(attr);
Ok(item.into_token_stream().into())
}
pub(super) fn command_dispatch(item: ItemEnum, _args: &[Meta]) -> Result<TokenStream> {
let name = &item.ident;
let arm: Vec<TokenStream2> = item.variants.iter().map(dispatch_arm).try_collect()?;
let switch = quote! {
pub(super) async fn process(command: #name, body: Vec<&str>) -> Result<RoomMessageEventContent> {
pub(super) async fn process(
command: #name,
context: &crate::Command<'_>
) -> Result<ruma::events::room::message::RoomMessageEventContent> {
use #name::*;
#[allow(non_snake_case)]
Ok(match command {
@ -34,7 +46,7 @@ fn dispatch_arm(v: &Variant) -> Result<TokenStream2> {
let field = fields.named.iter().filter_map(|f| f.ident.as_ref());
let arg = field.clone();
quote! {
#name { #( #field ),* } => Box::pin(#handler(&body, #( #arg ),*)).await?,
#name { #( #field ),* } => Box::pin(context.#handler(#( #arg ),*)).await?,
}
},
Fields::Unnamed(fields) => {
@ -42,12 +54,12 @@ fn dispatch_arm(v: &Variant) -> Result<TokenStream2> {
return Err(Error::new(Span::call_site().into(), "One unnamed field required"));
};
quote! {
#name ( #field ) => Box::pin(#handler::process(#field, body)).await?,
#name ( #field ) => Box::pin(#handler::process(#field, context)).await?,
}
},
Fields::Unit => {
quote! {
#name => Box::pin(#handler(&body)).await?,
#name => Box::pin(context.#handler()).await?,
}
},
};

View file

@ -14,6 +14,11 @@ use syn::{
pub(crate) type Result<T> = std::result::Result<T, Error>;
#[proc_macro_attribute]
pub fn admin_command(args: TokenStream, input: TokenStream) -> TokenStream {
attribute_macro::<ItemFn, _>(args, input, admin::command)
}
#[proc_macro_attribute]
pub fn admin_command_dispatch(args: TokenStream, input: TokenStream) -> TokenStream {
attribute_macro::<ItemEnum, _>(args, input, admin::command_dispatch)