1use salvo::cors::{Cors, CorsHandler};
2use salvo::http::Method;
3
4#[doc(hidden)]
5#[macro_export]
6macro_rules! gen_curly_brace {
7 ($i:ident) => {
8 "{}{}"
9 };
10 () => {
11 "{}"
12 };
13 (@ $i:ident) => {
14 "{}{}{}"
15 };
16 (@) => {
17 "{}{}"
18 };
19}
20
21#[doc(hidden)]
22#[macro_export]
23macro_rules! stringlize_path {
24 ($id:ident {$($t:tt)*})=>{
25 concat!($($t)* stringify!($id))
26 };
27 ($id:ident $($rest:ident)* {$($t:tt)*}) => {
28 $crate::stringlize_path!($($rest)* {$($t)* stringify!($id),"/", })
29 };
30}
31
32#[macro_export]
42macro_rules! router {
43 ([$($method:ident),+] => @ $($m:ident)::* $(/<**$rest:ident>)?) => {
44 $crate::router!(IN Router::with_path(format!($crate::gen_curly_brace!($($rest)?),$crate::acquire_last_ident!($($m)*),$(format!("/<**{}>",stringify!($rest)))?)), $($m)::* , $($method),+)
46 };
47 ([$($method:ident),+] => ... @ $($m:ident)::* $(/<**$rest:ident>)?) => {
48 $crate::router!(IN Router::with_path(format!($crate::gen_curly_brace!($($rest)?),concat!($crate::stringlize_path!($($m)* {})),$(format!("/<**{}>",stringify!($rest)))?)), $($m)::* , $($method),+)
49 };
50 ([$($method:ident),+] => $(/)? $($prefix:ident)/+ / @ $($m:ident)::* $(/<**$rest:ident>)?)=>{
51 $crate::router!(IN Router::with_path(format!($crate::gen_curly_brace!(@ $($rest)?),concat!($(stringify!($prefix),stringify!(/)),+),$crate::acquire_last_ident!($($m)*), $(format!("/<**{}>",stringify!($rest)))?)), $($m)::* , $($method),+)
53 };
54 (IN $e:expr, $m:path , $($method:ident),+)=>{
55 $e $(.$method($m))+
56 };
57}
58
59#[doc(hidden)]
60#[macro_export]
61macro_rules! acquire_last_ident {
62 ($ide:ident $($ids:ident)+) => {
63 $crate::acquire_last_ident!($($ids)+)
64 };
65 ($ide:ident)=>{
66 stringify!($ide).trim()
67 }
68}
69
70#[allow(dead_code)]
72pub fn build_cros(allow_origin: &str) -> CorsHandler {
73 Cors::new()
74 .allow_origin(allow_origin)
75 .allow_methods(vec![
76 Method::GET,
77 Method::POST,
78 Method::DELETE,
79 Method::PUT,
80 Method::PATCH,
81 ])
82 .into_handler()
83}
84
85#[macro_export]
87macro_rules! serve_routes {
88 ($c:expr => [$($e:expr),* $(,)?] $(& [$($hoop:expr),+ $(,)?])?) => {
89 {
90 use $crate::salvo::prelude::*;
91 let router = Router::new() $($(.hoop($hoop))+)? $(.push($e))*;
92 $crate::serve($c, router).await.unwrap();
93 }
94 };
95}