proc macro ✨
Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
parent
2468e0c3de
commit
85f734ec74
10 changed files with 123 additions and 70 deletions
24
src/macros/Cargo.toml
Normal file
24
src/macros/Cargo.toml
Normal file
|
@ -0,0 +1,24 @@
|
|||
[package]
|
||||
name = "conduit_macros"
|
||||
categories.workspace = true
|
||||
description.workspace = true
|
||||
edition.workspace = true
|
||||
keywords.workspace = true
|
||||
license.workspace = true
|
||||
readme.workspace = true
|
||||
repository.workspace = true
|
||||
version.workspace = true
|
||||
|
||||
[lib]
|
||||
name = "conduit_macros"
|
||||
path = "mod.rs"
|
||||
proc-macro = true
|
||||
|
||||
[dependencies]
|
||||
syn.workspace = true
|
||||
quote.workspace = true
|
||||
proc-macro2.workspace = true
|
||||
conduit-core.workspace = true
|
||||
|
||||
[lints]
|
||||
workspace = true
|
50
src/macros/admin.rs
Normal file
50
src/macros/admin.rs
Normal file
|
@ -0,0 +1,50 @@
|
|||
use conduit_core::utils::string::camel_to_snake_string;
|
||||
use proc_macro::{Span, TokenStream};
|
||||
use proc_macro2::TokenStream as TokenStream2;
|
||||
use quote::quote;
|
||||
use syn::{parse_macro_input, AttributeArgs, Fields, Ident, ItemEnum, Variant};
|
||||
|
||||
pub(super) fn command_dispatch(args: TokenStream, input_: TokenStream) -> TokenStream {
|
||||
let input = input_.clone();
|
||||
let item = parse_macro_input!(input as ItemEnum);
|
||||
let _args = parse_macro_input!(args as AttributeArgs);
|
||||
let arm = item.variants.iter().map(dispatch_arm);
|
||||
let name = item.ident;
|
||||
let q = quote! {
|
||||
pub(super) async fn process(command: #name, body: Vec<&str>) -> Result<RoomMessageEventContent> {
|
||||
use #name::*;
|
||||
#[allow(non_snake_case)]
|
||||
Ok(match command {
|
||||
#( #arm )*
|
||||
})
|
||||
}
|
||||
};
|
||||
|
||||
[input_, q.into()].into_iter().collect::<TokenStream>()
|
||||
}
|
||||
|
||||
fn dispatch_arm(v: &Variant) -> TokenStream2 {
|
||||
let name = &v.ident;
|
||||
let target = camel_to_snake_string(&format!("{name}"));
|
||||
let handler = Ident::new(&target, Span::call_site().into());
|
||||
match &v.fields {
|
||||
Fields::Named(fields) => {
|
||||
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?,
|
||||
}
|
||||
},
|
||||
Fields::Unnamed(fields) => {
|
||||
let field = &fields.unnamed.first().expect("one field");
|
||||
quote! {
|
||||
#name ( #field ) => Box::pin(#handler::process(#field, body)).await?,
|
||||
}
|
||||
},
|
||||
Fields::Unit => {
|
||||
quote! {
|
||||
#name => Box::pin(#handler(body)).await?,
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
8
src/macros/mod.rs
Normal file
8
src/macros/mod.rs
Normal file
|
@ -0,0 +1,8 @@
|
|||
mod admin;
|
||||
|
||||
use proc_macro::TokenStream;
|
||||
|
||||
#[proc_macro_attribute]
|
||||
pub fn admin_command_dispatch(args: TokenStream, input: TokenStream) -> TokenStream {
|
||||
admin::command_dispatch(args, input)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue