1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
use salvo::cors::{Cors, CorsHandler};
use salvo::http::Method;

#[macro_export]
macro_rules! gen_curly_brace {
    ($i:ident) => {
        "{}{}"
    };
    () => {
        "{}"
    };
    (@ $i:ident) => {
        "{}{}{}"
    };
    (@) => {
        "{}{}"
    };
}

#[macro_export]
macro_rules! stringlize_path {
	($id:ident {$($t:tt)*})=>{
		concat!($($t)* stringify!($id))
	};
	($id:ident $($rest:ident)* {$($t:tt)*}) => {
		$crate::stringlize_path!($($rest)* {$($t)* stringify!($id),"/", })
	};
}

// #[macro_export]
// macro_rules! debug_route {
// 	([$($method:ident),+] => ... @ $($m:ident)::* $(/<**$rest:ident>)?) => {
// 		concat!($crate::stringlize_path!($($m)* {}))
// 	};
// }

#[macro_export]
macro_rules! router {
	([$($method:ident),+] => @ $($m:ident)::* $(/<**$rest:ident>)?) => {
		//Router::with_path(acquire_last_ident!($($m)*)).$method($($m)::*)
		$crate::router!(IN Router::with_path(format!($crate::gen_curly_brace!($($rest)?),$crate::acquire_last_ident!($($m)*),$(format!("/<**{}>",stringify!($rest)))?)), $($m)::* , $($method),+)
	};
	([$($method:ident),+] => ... @ $($m:ident)::* $(/<**$rest:ident>)?) => {
		$crate::router!(IN Router::with_path(format!($crate::gen_curly_brace!($($rest)?),concat!($crate::stringlize_path!($($m)* {})),$(format!("/<**{}>",stringify!($rest)))?)), $($m)::* , $($method),+)
	};
	([$($method:ident),+] => $(/)? $($prefix:ident)/+ / @ $($m:ident)::* $(/<**$rest:ident>)?)=>{
		//Router::with_path(format!("{}{}",$prefix,acquire_last_ident!($($m)*))) $(. $method( $($m)::*  ))+
		$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),+)
	};
	(IN $e:expr, $m:path , $($method:ident),+)=>{
		$e $(.$method($m))+
	};
}

#[macro_export]
macro_rules! acquire_last_ident {
	($ide:ident $($ids:ident)+) => {
		$crate::acquire_last_ident!($($ids)+)
	};
	($ide:ident)=>{
		stringify!($ide).trim()
	}
}

#[allow(dead_code)]
pub fn build_cros(allow_origin: &str) -> CorsHandler {
    Cors::new()
        .allow_origin(allow_origin)
        .allow_methods(vec![
            Method::GET,
            Method::POST,
            Method::DELETE,
            Method::PUT,
            Method::PATCH,
        ])
        .into_handler()
}

#[macro_export]
macro_rules! serve_routes {
	($c:expr => [$($e:expr),* $(,)?]) => {
		{
			use $crate::salvo::prelude::*;
			let router = Router::new() $(.push($e))*;
			$crate::serve($c, router).await.unwrap();
		}
	};
}