use crate::{router::Router, Endpoint};
#[allow(missing_debug_implementations)]
pub struct Route<'a, State> {
router: &'a mut Router<State>,
path: String,
}
impl<'a, State: 'static> Route<'a, State> {
pub(crate) fn new(router: &'a mut Router<State>, path: String) -> Route<'a, State> {
Route { router, path }
}
pub fn at<'b>(&'b mut self, path: &str) -> Route<'b, State> {
let mut p = self.path.clone();
if !p.ends_with('/') && !path.starts_with('/') {
p.push_str("/");
}
if path != "/" {
p.push_str(path);
}
Route {
router: &mut self.router,
path: p,
}
}
pub fn nest(&mut self, f: impl FnOnce(&mut Route<'a, State>)) -> &mut Self {
f(self);
self
}
pub fn method(&mut self, method: http::Method, ep: impl Endpoint<State>) -> &mut Self {
self.router.add(&self.path, method, ep);
self
}
pub fn get(&mut self, ep: impl Endpoint<State>) -> &mut Self {
self.method(http::Method::GET, ep);
self
}
pub fn head(&mut self, ep: impl Endpoint<State>) -> &mut Self {
self.method(http::Method::HEAD, ep);
self
}
pub fn put(&mut self, ep: impl Endpoint<State>) -> &mut Self {
self.method(http::Method::PUT, ep);
self
}
pub fn post(&mut self, ep: impl Endpoint<State>) -> &mut Self {
self.method(http::Method::POST, ep);
self
}
pub fn delete(&mut self, ep: impl Endpoint<State>) -> &mut Self {
self.method(http::Method::DELETE, ep);
self
}
pub fn options(&mut self, ep: impl Endpoint<State>) -> &mut Self {
self.method(http::Method::OPTIONS, ep);
self
}
pub fn connect(&mut self, ep: impl Endpoint<State>) -> &mut Self {
self.method(http::Method::CONNECT, ep);
self
}
pub fn patch(&mut self, ep: impl Endpoint<State>) -> &mut Self {
self.method(http::Method::PATCH, ep);
self
}
pub fn trace(&mut self, ep: impl Endpoint<State>) -> &mut Self {
self.method(http::Method::TRACE, ep);
self
}
}