mirror of
https://github.com/tuono-labs/tuono
synced 2026-07-29 13:52:46 -07:00
feat: create route handler macro
This commit is contained in:
@@ -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"
|
||||
@@ -0,0 +1 @@
|
||||
# Tuonolib Macro
|
||||
@@ -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()
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user