use axum::routing::{on, MethodFilter};
pub fn method_filter_from_str(method: &str) -> MethodFilter {
match method {
"GET" => MethodFilter::GET,
"POST" => MethodFilter::POST,
"PUT" => MethodFilter::PUT,
"DELETE" => MethodFilter::DELETE,
"PATCH" => MethodFilter::PATCH,
other => panic!(
"Unknown HTTP method '{}' in route annotation. \
Use #[get], #[post], #[put], #[delete], or #[patch].",
other
),
}
}
pub type Router<S = ()> = axum::Router<S>;
pub fn build() -> Router<()> {
axum::Router::new()
}
pub trait ApiRoute<S>
where
S: Clone + Send + Sync + 'static,
{
fn api_route<H, T>(self, route_info: (&'static str, &'static str), handler: H) -> Self
where
H: axum::handler::Handler<T, S>,
T: 'static;
}
impl<S> ApiRoute<S> for Router<S>
where
S: Clone + Send + Sync + 'static,
{
fn api_route<H, T>(self, route_info: (&'static str, &'static str), handler: H) -> Self
where
H: axum::handler::Handler<T, S>,
T: 'static,
{
let (path, method) = route_info;
let filter = match method {
"GET" => MethodFilter::GET,
"POST" => MethodFilter::POST,
"PUT" => MethodFilter::PUT,
"DELETE" => MethodFilter::DELETE,
"PATCH" => MethodFilter::PATCH,
other => panic!(
"Unknown HTTP method '{}' from route annotation. \
Use #[get], #[post], #[put], #[delete], or #[patch].",
other
),
};
self.route(path, on(filter, handler))
}
}
pub trait RouterExt<S> {
fn finish(self) -> Router<S>;
}
impl<S> RouterExt<S> for Router<S> {
fn finish(self) -> Router<S> {
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_router_creation() {
let _router = build();
}
#[test]
fn test_router_finish() {
let _router = build().finish();
}
}