checkpoint

This commit is contained in:
David Pedersen
2022-02-13 14:41:49 +01:00
parent 47f5954903
commit 4f087190a5
3 changed files with 120 additions and 61 deletions
+49 -10
View File
@@ -1,8 +1,9 @@
#![allow(missing_docs)]
#![allow(missing_docs, missing_debug_implementations)]
use axum::extract::{FromRequest, Path};
use serde::de::DeserializeOwned;
use std::borrow::Cow;
use axum::{body::HttpBody, handler::Handler, routing, Router};
use std::{borrow::Cow, marker::PhantomData};
use super::HasRoutes;
/// ```rust
/// use axum_macros::TypedPath;
@@ -13,13 +14,51 @@ use std::borrow::Cow;
/// id: u32,
/// }
/// ```
pub trait TypedPath<B>: FromRequest<B> + DeserializeOwned {
pub trait TypedPath {
const PATH: &'static str;
fn path(&self) -> Cow<'static, str>;
}
// pub trait FirstElementIsPath {}
// impl<P> FirstElementIsPath for (Path<P>,) {}
// impl<P, T1> FirstElementIsPath for (Path<P>, T1) {}
// impl<P, T1, T2> FirstElementIsPath for (Path<P>, T1, T2) {}
pub fn get<H, B, T, P>(handler: H) -> TypedPathRouter<P, B>
where
H: Handler<T, B>,
P: TypedPath,
T: FirstElementIs<P> + 'static,
B: HttpBody + Send + 'static,
{
TypedPathRouter {
router: Router::new().route(P::PATH, routing::get(handler)),
_path: PhantomData,
}
}
pub struct TypedPathRouter<P, B> {
router: Router<B>,
_path: PhantomData<P>,
}
impl<P, B> TypedPathRouter<P, B>
where
B: HttpBody + Send + 'static,
P: TypedPath,
{
pub fn post<H, T>(mut self, handler: H) -> Self
where
H: Handler<T, B>,
T: FirstElementIs<P> + 'static,
{
self.router = self.router.route(P::PATH, routing::post(handler));
self
}
}
impl<P, B> HasRoutes<B> for TypedPathRouter<P, B> {
fn routes(self) -> Router<B> {
self.router
}
}
pub trait FirstElementIs<P> {}
impl<P> FirstElementIs<P> for (P,) {}
impl<P, T1> FirstElementIs<P> for (P, T1) {}
impl<P, T1, T2> FirstElementIs<P> for (P, T1, T2) {}