feat: create route handler macro

This commit is contained in:
Valerio Ageno
2024-05-18 20:45:35 +02:00
parent 74968b27cc
commit d4c9b25b90
12 changed files with 149 additions and 43 deletions
+12
View File
@@ -0,0 +1,12 @@
[package]
name = "tuono_lib_macros"
version = "0.0.1"
edition = "2021"
[lib]
crate_type = ["proc-macro"]
proc-marco = true
[dependencies]
syn = { version = "1.0.59", features = ["full"] }
quote = "1.0"
+1
View File
@@ -0,0 +1 @@
# Tuonolib Macro
+35
View File
@@ -0,0 +1,35 @@
use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, ItemFn};
pub fn handler_core(_args: TokenStream, item: TokenStream) -> TokenStream {
let item = parse_macro_input!(item as ItemFn);
let fn_name = item.clone().sig.ident;
quote! {
use tuono_lib::Response;
use axum::response::Html;
use axum::http::Uri;
use tuono_lib::ssr;
#item
pub async fn route(uri: Uri) -> Html<String> {
println!("Custom handler");
let props = #fn_name();
let res = match props {
Response::Props(val) => ssr::Js::SSR.with(|ssr| ssr.borrow_mut().render_to_string(Some(val.as_str()))),
_ => Ok("500 Internal server error".to_string())
};
match res {
Ok(html) => Html(html),
_ => Html("500".to_string())
}
}
}
.into()
}
+11
View File
@@ -0,0 +1,11 @@
extern crate proc_macro;
use proc_macro::TokenStream;
mod handler;
#[proc_macro_attribute]
pub fn handler(args: TokenStream, item: TokenStream) -> TokenStream {
handler::handler_core(args, item)
}