Files
tuono/crates/tuono_lib_macros/src/handler.rs
T

78 lines
2.3 KiB
Rust
Raw Normal View History

2024-11-21 19:02:11 +01:00
use crate::utils::{
crate_application_state_extractor, create_struct_fn_arg, import_main_application_state,
params_argument, request_argument,
};
2024-05-18 20:45:35 +02:00
use proc_macro::TokenStream;
use quote::quote;
2024-11-11 21:52:22 +01:00
use syn::punctuated::Punctuated;
use syn::token::Comma;
2024-11-21 19:02:11 +01:00
use syn::{parse_macro_input, FnArg, ItemFn, Pat};
2024-11-11 21:52:22 +01:00
2024-05-18 20:45:35 +02:00
pub fn handler_core(_args: TokenStream, item: TokenStream) -> TokenStream {
let item = parse_macro_input!(item as ItemFn);
2024-11-11 21:52:22 +01:00
let fn_name = &item.sig.ident;
2024-05-18 20:45:35 +02:00
2024-11-11 21:52:22 +01:00
let mut argument_names: Punctuated<Pat, Comma> = Punctuated::new();
let mut axum_arguments: Punctuated<FnArg, Comma> = Punctuated::new();
// Fn Arguments minus the first which always is the request
for (i, arg) in item.sig.inputs.iter().enumerate() {
if i == 0 {
axum_arguments.insert(i, params_argument());
continue;
}
2024-11-17 16:28:56 +01:00
if i == 1 {
axum_arguments.insert(1, create_struct_fn_arg())
}
2024-11-11 21:52:22 +01:00
if let FnArg::Typed(pat_type) = arg {
let index = i - 1;
let argument_name = *pat_type.pat.clone();
argument_names.insert(index, argument_name.clone());
}
}
2024-05-18 20:45:35 +02:00
2024-11-11 21:52:22 +01:00
axum_arguments.insert(axum_arguments.len(), request_argument());
2024-11-17 16:28:56 +01:00
let application_state_extractor = crate_application_state_extractor(argument_names.clone());
let application_state_import = import_main_application_state(argument_names.clone());
2024-11-11 21:52:22 +01:00
quote! {
2024-11-17 16:28:56 +01:00
#application_state_import
2024-05-18 20:45:35 +02:00
#item
2024-06-02 20:32:53 +02:00
pub async fn route(
2024-11-11 21:52:22 +01:00
#axum_arguments
) -> impl tuono_lib::axum::response::IntoResponse {
2024-11-17 16:28:56 +01:00
#application_state_extractor
2024-07-09 19:00:48 +02:00
let pathname = request.uri();
let headers = request.headers();
2024-07-09 19:00:48 +02:00
let req = tuono_lib::Request::new(pathname.to_owned(), headers.to_owned(), params);
2024-11-11 21:52:22 +01:00
#fn_name(req.clone(), #argument_names).await.render_to_string(req)
2024-05-20 21:07:37 +02:00
}
2024-06-02 20:32:53 +02:00
pub async fn api(
2024-11-11 21:52:22 +01:00
#axum_arguments
) -> impl tuono_lib::axum::response::IntoResponse {
2024-11-17 16:28:56 +01:00
#application_state_extractor
2024-07-09 19:00:48 +02:00
let pathname = request.uri();
let headers = request.headers();
2024-05-20 21:07:37 +02:00
2024-07-09 19:00:48 +02:00
let req = tuono_lib::Request::new(pathname.to_owned(), headers.to_owned(), params);
2024-05-20 21:07:37 +02:00
2024-11-11 21:52:22 +01:00
#fn_name(req.clone(), #argument_names).await.json()
2024-05-18 20:45:35 +02:00
}
}
.into()
}