use crate::Router;
use std::marker::PhantomData;
pub struct RouterBuilder<S = ()> {
router: Router<S>,
_marker: PhantomData<S>,
}
impl<S> RouterBuilder<S>
where
S: Clone + Send + Sync + 'static,
{
pub fn new() -> Self
where
S: Default,
{
Self::from_router(Router::with_state(S::default()))
}
pub fn from_router(router: Router<S>) -> Self {
Self { router, _marker: PhantomData }
}
pub fn nest<T>(mut self, prefix: &str, router: T) -> Self
where
T: Into<Router<S>>,
{
self.router = self.router.nest_service(prefix, router);
self
}
pub fn merge(mut self, other: Router<S>) -> Self {
self.router = self.router.merge(other);
self
}
pub fn get<H, T>(mut self, path: &str, handler: H) -> Self
where
H: Fn(T) -> crate::Response<crate::Body> + Clone + Send + Sync + 'static,
T: crate::extract::FromRequestParts<S, Error = crate::extract::ExtractorError> + 'static,
{
self.router.add_route(http::Method::GET, path, handler);
self
}
pub fn post<H, T>(mut self, path: &str, handler: H) -> Self
where
H: Fn(T) -> crate::Response<crate::Body> + Clone + Send + Sync + 'static,
T: crate::extract::FromRequestParts<S, Error = crate::extract::ExtractorError> + 'static,
{
self.router.add_route(http::Method::POST, path, handler);
self
}
pub fn put<H, T>(mut self, path: &str, handler: H) -> Self
where
H: Fn(T) -> crate::Response<crate::Body> + Clone + Send + Sync + 'static,
T: crate::extract::FromRequestParts<S, Error = crate::extract::ExtractorError> + 'static,
{
self.router.add_route(http::Method::PUT, path, handler);
self
}
pub fn delete<H, T>(mut self, path: &str, handler: H) -> Self
where
H: Fn(T) -> crate::Response<crate::Body> + Clone + Send + Sync + 'static,
T: crate::extract::FromRequestParts<S, Error = crate::extract::ExtractorError> + 'static,
{
self.router.add_route(http::Method::DELETE, path, handler);
self
}
pub fn patch<H, T>(mut self, path: &str, handler: H) -> Self
where
H: Fn(T) -> crate::Response<crate::Body> + Clone + Send + Sync + 'static,
T: crate::extract::FromRequestParts<S, Error = crate::extract::ExtractorError> + 'static,
{
self.router.add_route(http::Method::PATCH, path, handler);
self
}
pub fn options<H, T>(mut self, path: &str, handler: H) -> Self
where
H: Fn(T) -> crate::Response<crate::Body> + Clone + Send + Sync + 'static,
T: crate::extract::FromRequestParts<S, Error = crate::extract::ExtractorError> + 'static,
{
self.router.add_route(http::Method::OPTIONS, path, handler);
self
}
pub fn head<H, T>(mut self, path: &str, handler: H) -> Self
where
H: Fn(T) -> crate::Response<crate::Body> + Clone + Send + Sync + 'static,
T: crate::extract::FromRequestParts<S, Error = crate::extract::ExtractorError> + 'static,
{
self.router.add_route(http::Method::HEAD, path, handler);
self
}
pub fn trace<H, T>(mut self, path: &str, handler: H) -> Self
where
H: Fn(T) -> crate::Response<crate::Body> + Clone + Send + Sync + 'static,
T: crate::extract::FromRequestParts<S, Error = crate::extract::ExtractorError> + 'static,
{
self.router.add_route(http::Method::TRACE, path, handler);
self
}
pub fn build(self) -> Router<S> {
self.router
}
pub fn into_inner(self) -> Router<S> {
self.build()
}
}
impl<S> Default for RouterBuilder<S>
where
S: Clone + Send + Sync + 'static + Default,
{
fn default() -> Self {
Self::new()
}
}
impl<S> From<Router<S>> for RouterBuilder<S>
where
S: Clone + Send + Sync + 'static,
{
fn from(router: Router<S>) -> Self {
Self::from_router(router)
}
}
impl<S> From<RouterBuilder<S>> for Router<S>
where
S: Clone + Send + Sync + 'static,
{
fn from(builder: RouterBuilder<S>) -> Self {
builder.build()
}
}
pub struct MethodRouter<S = ()> {
_marker: PhantomData<S>,
}
impl<S> Clone for MethodRouter<S> {
fn clone(&self) -> Self {
Self { _marker: PhantomData }
}
}
impl<S> Default for MethodRouter<S> {
fn default() -> Self {
Self { _marker: PhantomData }
}
}
pub fn get<H, T, S>(handler: H) -> impl FnOnce(&mut Router<S>, &str)
where
H: Fn(T) -> crate::Response<crate::Body> + Clone + Send + Sync + 'static,
T: crate::extract::FromRequestParts<S, Error = crate::extract::ExtractorError> + 'static,
S: Clone + Send + Sync + 'static,
{
move |router, path| {
router.add_route(http::Method::GET, path, handler);
}
}
pub fn post<H, T, S>(handler: H) -> impl FnOnce(&mut Router<S>, &str)
where
H: Fn(T) -> crate::Response<crate::Body> + Clone + Send + Sync + 'static,
T: crate::extract::FromRequestParts<S, Error = crate::extract::ExtractorError> + 'static,
S: Clone + Send + Sync + 'static,
{
move |router, path| {
router.add_route(http::Method::POST, path, handler);
}
}
pub fn put<H, T, S>(handler: H) -> impl FnOnce(&mut Router<S>, &str)
where
H: Fn(T) -> crate::Response<crate::Body> + Clone + Send + Sync + 'static,
T: crate::extract::FromRequestParts<S, Error = crate::extract::ExtractorError> + 'static,
S: Clone + Send + Sync + 'static,
{
move |router, path| {
router.add_route(http::Method::PUT, path, handler);
}
}
pub fn delete<H, T, S>(handler: H) -> impl FnOnce(&mut Router<S>, &str)
where
H: Fn(T) -> crate::Response<crate::Body> + Clone + Send + Sync + 'static,
T: crate::extract::FromRequestParts<S, Error = crate::extract::ExtractorError> + 'static,
S: Clone + Send + Sync + 'static,
{
move |router, path| {
router.add_route(http::Method::DELETE, path, handler);
}
}
pub fn patch<H, T, S>(handler: H) -> impl FnOnce(&mut Router<S>, &str)
where
H: Fn(T) -> crate::Response<crate::Body> + Clone + Send + Sync + 'static,
T: crate::extract::FromRequestParts<S, Error = crate::extract::ExtractorError> + 'static,
S: Clone + Send + Sync + 'static,
{
move |router, path| {
router.add_route(http::Method::PATCH, path, handler);
}
}
pub fn options<H, T, S>(handler: H) -> impl FnOnce(&mut Router<S>, &str)
where
H: Fn(T) -> crate::Response<crate::Body> + Clone + Send + Sync + 'static,
T: crate::extract::FromRequestParts<S, Error = crate::extract::ExtractorError> + 'static,
S: Clone + Send + Sync + 'static,
{
move |router, path| {
router.add_route(http::Method::OPTIONS, path, handler);
}
}
pub fn head<H, T, S>(handler: H) -> impl FnOnce(&mut Router<S>, &str)
where
H: Fn(T) -> crate::Response<crate::Body> + Clone + Send + Sync + 'static,
T: crate::extract::FromRequestParts<S, Error = crate::extract::ExtractorError> + 'static,
S: Clone + Send + Sync + 'static,
{
move |router, path| {
router.add_route(http::Method::HEAD, path, handler);
}
}
pub fn trace<H, T, S>(handler: H) -> impl FnOnce(&mut Router<S>, &str)
where
H: Fn(T) -> crate::Response<crate::Body> + Clone + Send + Sync + 'static,
T: crate::extract::FromRequestParts<S, Error = crate::extract::ExtractorError> + 'static,
S: Clone + Send + Sync + 'static,
{
move |router, path| {
router.add_route(http::Method::TRACE, path, handler);
}
}
pub fn health_check_router() -> Router {
Router::new()
}