Skip to main content

Router

Struct Router 

Source
pub struct Router { /* private fields */ }
Expand description

Router.

Routers allow to scope specific actions to a combination of HTTP methods and path patterns, making them essentially a specialization of Stack. Additionally, routers allow for the addition of middlewares, which are grouped into stacks, and can be defined before and after routes.

Implementations§

Source§

impl Router

Source

pub fn new<P>(path: P) -> Self
where P: Into<String>,

Creates a router.

The given path is prepended to all routes that are created as part of the router. Using Router::default is equivalent to passing /.

§Examples
use zapient::router::Router;

// Create router
let router = Router::new("/");
Source

pub fn get<P, A>(self, path: P, action: A) -> Self
where P: Into<String>, A: Action,

Adds a GET route to the router.

§Examples
use zapient::http::{Request, Response};
use zapient::router::{Router, Params};

// Create router and add route
let router = Router::default()
    .get("/", |req: Request, params: Params| {
        Response::default()
    });
Source

pub fn post<P, A>(self, path: P, action: A) -> Self
where P: Into<String>, A: Action,

Adds a POST route to the router.

§Examples
use zapient::http::{Request, Response};
use zapient::router::{Router, Params};

// Create router and add route
let router = Router::default()
    .post("/", |req: Request, params: Params| {
        Response::default()
    });
Source

pub fn put<P, A>(self, path: P, action: A) -> Self
where P: Into<String>, A: Action,

Adds a PUT route to the router.

§Examples
use zapient::http::{Request, Response};
use zapient::router::{Router, Params};

// Create router and add route
let router = Router::default()
    .put("/", |req: Request, params: Params| {
        Response::default()
    });
Source

pub fn delete<P, A>(self, path: P, action: A) -> Self
where P: Into<String>, A: Action,

Adds a DELETE route to the router.

§Examples
use zapient::http::{Request, Response};
use zapient::router::{Router, Params};

// Create router and add route
let router = Router::default()
    .delete("/", |req: Request, params: Params| {
        Response::default()
    });
Source

pub fn patch<P, A>(self, path: P, action: A) -> Self
where P: Into<String>, A: Action,

Adds a PATCH route to the router.

§Examples
use zapient::http::{Request, Response};
use zapient::router::{Router, Params};

// Create router and add route
let router = Router::default()
    .patch("/", |req: Request, params: Params| {
        Response::default()
    });
Source

pub fn head<P, A>(self, path: P, action: A) -> Self
where P: Into<String>, A: Action,

Adds a HEAD route to the router.

§Examples
use zapient::http::{Request, Response};
use zapient::router::{Router, Params};

// Create router and add route
let router = Router::default()
    .head("/", |req: Request, params: Params| {
        Response::default()
    });
Source

pub fn options<P, A>(self, path: P, action: A) -> Self
where P: Into<String>, A: Action,

Adds a OPTIONS route to the router.

§Examples
use zapient::http::{Request, Response};
use zapient::router::{Router, Params};

// Create router and add route
let router = Router::default()
    .options("/", |req: Request, params: Params| {
        Response::default()
    });
Source

pub fn trace<P, A>(self, path: P, action: A) -> Self
where P: Into<String>, A: Action,

Adds a TRACE route to the router.

§Examples
use zapient::http::{Request, Response};
use zapient::router::{Router, Params};

// Create router and add route
let router = Router::default()
    .trace("/", |req: Request, params: Params| {
        Response::default()
    });
Source

pub fn with<M>(self, middleware: M) -> Self

Adds a middleware to the router.

Middlewares can be added at any point in the router stack, including before or after routes. This allows for flexible routing and middleware combinations, as routes are themselves combines into middlewares, when the router is converted into a middleware.

Anything that can be converted into a Middleware can be added to the stack, including middlewares, routers, stacks and closures.

§Errors

Errors returned by TryIntoMiddleware are passed through.

§Examples
use zapient::handler::Handler;
use zapient::http::{Method, Request, Response, Status};
use zapient::router::Router;

// Create router with middleware
let stack = Router::default()
    .with(|req: Request, next: &dyn Handler| {
        if req.method == Method::Get && req.uri.path == "/coffee" {
            Response::new().status(Status::ImATeapot)
        } else {
            next.handle(req)
        }
    });

Trait Implementations§

Source§

impl Debug for Router

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Router

Source§

fn default() -> Self

Creates a default router.

§Examples
use zapient::router::Router;

// Create router
let router = Router::default();
Source§

impl TryIntoHandler for Router

Source§

fn try_into_handler(self) -> Result<Self::Output>

Attempts to convert the router into a handler.

This method is equivalent to calling Router::try_into_middleware with Scope::default, scoping all middlewares and routes to /.

§Errors

In case conversion fails, an Error is returned.

§Examples
use zapient::handler::TryIntoHandler;
use zapient::http::{Request, Response, Status};
use zapient::router::{Router, Params};

// Create router and convert into handler
let router = Router::default()
    .get("/coffee", |req: Request, params: Params| {
        Response::new().status(Status::ImATeapot)
    })
    .try_into_handler()?;
Source§

type Output = Stack

Output type of conversion.
Source§

impl TryIntoMiddleware for Router

Source§

fn try_into_middleware(self, scope: &Scope) -> Result<Self::Output>

Attempts to convert the router into a middleware.

§Errors

In case conversion fails, an Error is returned.

§Examples
use zapient::handler::Scope;
use zapient::http::{Request, Response, Status};
use zapient::middleware::TryIntoMiddleware;
use zapient::router::{Router, Params};

// Create scope
let scope = Scope::default();

// Create router and convert into middleware
let router = Router::default()
    .get("/coffee", |req: Request, params: Params| {
        Response::new().status(Status::ImATeapot)
    })
    .try_into_middleware(&scope)?;
Source§

type Output = Stack

Output type of conversion.

Auto Trait Implementations§

§

impl !RefUnwindSafe for Router

§

impl !Send for Router

§

impl !Sync for Router

§

impl !UnwindSafe for Router

§

impl Freeze for Router

§

impl Unpin for Router

§

impl UnsafeUnpin for Router

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.