From 88257a8f7ea5e26015a1b28e14cc39fbebc90459 Mon Sep 17 00:00:00 2001 From: = <=> Date: Mon, 9 Sep 2024 00:25:25 +0200 Subject: [PATCH] trying macros --- .gitignore | 1 + Cargo.lock | 46 ++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 11 +++++++++++ src/handler.rs | 6 ++++++ src/lib.rs | 33 +++++++++++++++++++++++++++++++++ 5 files changed, 97 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 src/handler.rs create mode 100644 src/lib.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..62fb3cc --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,46 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "medusa_macros" +version = "0.1.0" +dependencies = [ + "quote", + "syn", +] + +[[package]] +name = "proc-macro2" +version = "1.0.86" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.37" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "syn" +version = "2.0.77" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f35bcdf61fd8e7be6caf75f429fdca8beb3ed76584befb503b1569faee373ed" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..db028c1 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "medusa_macros" +version = "0.1.0" +edition = "2021" + +[lib] +proc-macro = true + +[dependencies] +syn = { version = "2.0.77", features = ["full"] } +quote = "1.0.37" diff --git a/src/handler.rs b/src/handler.rs new file mode 100644 index 0000000..085b783 --- /dev/null +++ b/src/handler.rs @@ -0,0 +1,6 @@ + +pub struct Handler { + pub module: &'static str, + pub method: &'static str, + pub handle: fn(model: &str, body: &str) -> Pin + Send>>, +} \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..50c12e4 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,33 @@ +extern crate proc_macro; + +use proc_macro::TokenStream; +use quote::quote; +use syn::{parse_macro_input, punctuated::Punctuated, LitStr, Token}; + +#[proc_macro_attribute] +pub fn handler(args: TokenStream, input: TokenStream) -> TokenStream { + let args = parse_macro_input!(args with Punctuated::::parse_terminated); + + let module = &args[0].value(); + let method = &args[1].value(); + + let input_fn = parse_macro_input!(input as syn::ItemFn); + let fn_name: syn::Ident = input_fn.sig.ident; + let inner_fn_name = syn::Ident::new(&format!("{}_handler", fn_name), fn_name.span()); + + let output = quote! { + pub const #fn_name: Handler = Handler { + module: #module, + method: #method, + handle: |model: &str, body: &str| -> Pin + Send>> { + Box::pin(#inner_fn_name(model, body)) + } + }; + + pub fn #inner_fn_name(model: &str, body: &str) -> impl Future + Send { + #fn_name(model, body) + } + }; + + output.into() +}