1use proc_macro2::TokenStream;
4use quote::{format_ident, quote};
5
6use crate::openapi::HttpMethod;
7
8pub struct RouterGenerator;
10
11impl RouterGenerator {
12 pub fn generate_map_routes(
17 &self,
18 methods: &[(syn::Ident, HttpMethod, String)],
19 spec_method: Option<(syn::Ident, String)>,
20 ) -> TokenStream {
21 let routes = methods.iter().map(|(method_name, http_method, path)| {
22 let method_fn = http_method_to_axum_fn(*http_method);
24
25 quote! {
26 .route(#path, ::axum::routing::#method_fn(Self::#method_name))
27 }
28 });
29
30 let spec_route = spec_method.map(|(method_name, path)| {
31 quote! {
32 .route(#path, ::axum::routing::get(|| async { Self::#method_name() }))
33 }
34 });
35
36 quote! {
37 router
38 #(#routes)*
39 #spec_route
40 }
41 }
42}
43
44fn http_method_to_axum_fn(method: HttpMethod) -> syn::Ident {
46 format_ident!("{}", method.as_str())
47}