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;
|
|
|
|
|
use syn::{parse2, parse_macro_input, FnArg, ItemFn, Pat, Type};
|
2024-05-18 20:45:35 +02:00
|
|
|
|
2024-11-11 21:52:22 +01:00
|
|
|
fn create_struct_fn_arg(arg_name: Pat, arg_type: Type) -> FnArg {
|
|
|
|
|
parse2(quote! {
|
|
|
|
|
tuono_lib::axum::extract::State(#arg_name): tuono_lib::axum::extract::State<#arg_type>
|
|
|
|
|
})
|
|
|
|
|
.unwrap()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn params_argument() -> FnArg {
|
|
|
|
|
parse2(quote! {
|
|
|
|
|
tuono_lib::axum::extract::Path(params): tuono_lib::axum::extract::Path<
|
|
|
|
|
std::collections::HashMap<String, String>
|
|
|
|
|
>
|
|
|
|
|
})
|
|
|
|
|
.unwrap()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn request_argument() -> FnArg {
|
|
|
|
|
parse2(quote! {
|
|
|
|
|
request: tuono_lib::axum::extract::Request
|
|
|
|
|
})
|
|
|
|
|
.unwrap()
|
|
|
|
|
}
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if let FnArg::Typed(pat_type) = arg {
|
|
|
|
|
let index = i - 1;
|
|
|
|
|
let argument_name = *pat_type.pat.clone();
|
|
|
|
|
let argument_type = *pat_type.ty.clone();
|
|
|
|
|
argument_names.insert(index, argument_name.clone());
|
|
|
|
|
axum_arguments.insert(index, create_struct_fn_arg(argument_name, argument_type))
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-05-18 20:45:35 +02:00
|
|
|
|
2024-11-11 21:52:22 +01:00
|
|
|
axum_arguments.insert(axum_arguments.len(), request_argument());
|
|
|
|
|
|
|
|
|
|
quote! {
|
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-07-09 19:00:48 +02:00
|
|
|
let pathname = request.uri();
|
|
|
|
|
let headers = request.headers();
|
2024-05-19 12:22:56 +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-19 12:22:56 +02:00
|
|
|
|
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-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()
|
|
|
|
|
}
|