Skip to content

Commit

Permalink
[ISSUES#24] feat handler as macro #part 5 genrate code test.
Browse files Browse the repository at this point in the history
  • Loading branch information
847850277 committed Oct 3, 2024
1 parent c6e7c88 commit 04a11b6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
26 changes: 18 additions & 8 deletions mario-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,33 @@ pub fn handler(
}

fn generate_handler(_args: TokenStream, input: TokenStream) -> syn::Result<TokenStream> {

let item_fn = syn::parse::<ItemFn>(input)?;
let vis = &item_fn.vis;
let ident = &item_fn.sig.ident;


let expanded = quote! {

#[derive(Debug)]
struct example_2;
impl example_2 {
#vis struct #ident;

impl #ident {
pub fn new() -> Self {
Self
}
}
impl Endpoint for example_2 {

impl Endpoint for #ident {
fn handler(&self, req: &mario_core::route::request::Request) -> Result<Response, Error> {
// Your implementation here
//Ok(Response::new("run example handler"))
async fn example_1() -> String {
//Ok(Response::new("run example_1"))
"run example_2".to_string()
}
let fut = example_1();
// async fn example_2() -> String {
// //Ok(Response::new("run example_1"))
// "run example_2".to_string()
// }
#item_fn
let fut = #ident();
let response = executor::block_on(fut);
Ok(Response::new(&response))
}
Expand Down
6 changes: 3 additions & 3 deletions mario-rs-examples/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ impl Endpoint for ExampleHandler {
// }

#[handler]
async fn example_2() -> String {
async fn example_999() -> String {
//Ok(Response::new("run example_1"))
"run example_2".to_string()
"run example_99999".to_string()
}

#[tokio::main]
Expand All @@ -98,7 +98,7 @@ pub async fn main() {
//let handler = create_handler!(ExampleHandler);
let route = Route::new(http::Method::GET, "/hello_world".to_string(), handler);

let handler_1 = create_handler!(example_2);
let handler_1 = create_handler!(example_999);
let route_1 = Route::new(http::Method::GET, "/hello_world_2".to_string(), handler_1);

server.server.bind_route(route);
Expand Down

0 comments on commit 04a11b6

Please sign in to comment.