use core::fmt;
use http::Extensions;
use std::future::Future;
use super::RouteMethod;
use crate::{
app::{Handler, PageHandler},
web::{FromRequest, IntoResponse},
};
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum HandlerKind {
Action,
Page,
}
pub struct Route {
path: String,
method: RouteMethod,
handler: PageHandler,
extensions: Extensions,
}
impl Route {
pub fn new<H, Args>(path: &str, method: RouteMethod, handler: H) -> Self
where
Args: FromRequest + Send + 'static,
H: Handler<Args> + Sync + Send,
H::Future: Future + Send + 'static,
H::Output: IntoResponse,
<Args as FromRequest>::Fut: Send,
{
Route {
path: path.to_owned(),
handler: PageHandler::new(handler),
method,
extensions: Default::default(),
}
}
pub fn with_path(self, new_path: impl Into<String>) -> Self {
Route {
path: new_path.into(),
handler: self.handler,
method: self.method,
extensions: self.extensions,
}
}
pub fn any<H, Args>(path: &str, handler: H) -> Self
where
Args: FromRequest + Send + 'static,
H: Handler<Args> + Sync + Send,
H::Future: Future + Send + 'static,
H::Output: IntoResponse,
<Args as FromRequest>::Fut: Send,
{
Self::new(path, RouteMethod::all(), handler)
}
pub fn post<H, Args>(path: &str, handler: H) -> Self
where
Args: FromRequest + Send + 'static,
H: Handler<Args> + Sync + Send,
H::Future: Future + Send + 'static,
H::Output: IntoResponse,
<Args as FromRequest>::Fut: Send,
{
Self::new(path, RouteMethod::POST, handler)
}
pub fn get<H, Args>(path: &str, handler: H) -> Self
where
Args: FromRequest + Send + 'static,
H: Handler<Args> + Sync + Send,
H::Future: Future + Send + 'static,
H::Output: IntoResponse,
<Args as FromRequest>::Fut: Send,
{
Self::new(path, RouteMethod::GET, handler)
}
pub fn head<H, Args>(path: &str, handler: H) -> Self
where
Args: FromRequest + Send + 'static,
H: Handler<Args> + Sync + Send,
H::Future: Future + Send + 'static,
H::Output: IntoResponse,
<Args as FromRequest>::Fut: Send,
{
Self::new(path, RouteMethod::HEAD, handler)
}
pub fn put<H, Args>(path: &str, handler: H) -> Self
where
Args: FromRequest + Send + 'static,
H: Handler<Args> + Sync + Send,
H::Future: Future + Send + 'static,
H::Output: IntoResponse,
<Args as FromRequest>::Fut: Send,
{
Self::new(path, RouteMethod::PUT, handler)
}
pub fn delete<H, Args>(path: &str, handler: H) -> Self
where
Args: FromRequest + Send + 'static,
H: Handler<Args> + Sync + Send,
H::Future: Future + Send + 'static,
H::Output: IntoResponse,
<Args as FromRequest>::Fut: Send,
{
Self::new(path, RouteMethod::DELETE, handler)
}
pub fn options<H, Args>(path: &str, handler: H) -> Self
where
Args: FromRequest + Send + 'static,
H: Handler<Args> + Sync + Send,
H::Future: Future + Send + 'static,
H::Output: IntoResponse,
<Args as FromRequest>::Fut: Send,
{
Self::new(path, RouteMethod::OPTIONS, handler)
}
pub fn patch<H, Args>(path: &str, handler: H) -> Self
where
Args: FromRequest + Send + 'static,
H: Handler<Args> + Sync + Send,
H::Future: Future + Send + 'static,
H::Output: IntoResponse,
<Args as FromRequest>::Fut: Send,
{
Self::new(path, RouteMethod::PATCH, handler)
}
pub fn path(&self) -> &str {
&self.path
}
pub fn method(&self) -> RouteMethod {
self.method
}
pub fn handler(&self) -> &PageHandler {
&self.handler
}
pub fn extensions(&self) -> &Extensions {
&self.extensions
}
pub fn extensions_mut(&mut self) -> &mut Extensions {
&mut self.extensions
}
}
impl fmt::Debug for Route {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Route")
.field("path", &self.path)
.field("method", &self.method)
.finish()
}
}