PathRouter

Struct PathRouter 

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

A path-based http request router

A Handler that matches the URL of each incoming request against a list of registered patterns and calls the matching handler, if one exists. If no matching path is found, the handler returns a 404 Not Found.

Implementations§

Source§

impl PathRouter

Source

pub fn new() -> Self

Creates a new path router instance

Source

pub fn register<H>(&mut self, path: &str, handler: H)
where H: Handler + 'static,

Registers a handler that is invoked on incoming requests path.

Paths support basic segment matching. Matched path values are included in the request object

§Path Matching Syntax

Matching an exact path

This will only match requests where the path is “/a/b”. This is not a prefix match.

use claro::{handler::PathRouter, Request, Response};

PathRouter::new().register("/a/b", |_: &mut Request, _: &mut Response| {});

Matching a segment within a path

This will match requests like /a/b/c and /a/blahwhatever/c.

use claro::{handler::PathRouter, Request, Response};

PathRouter::new().register("/a/{next}/c", |_: &mut Request, _: &mut Response| {});

Prefix matching

This will match requests like /a/whatever and /a/blah/blah/blah

use claro::{handler::PathRouter, Request, Response};

PathRouter::new().register("/a/{*rest}", |_: &mut Request, _: &mut Response| {});

Trait Implementations§

Source§

impl Default for PathRouter

Source§

fn default() -> PathRouter

Returns the “default value” for a type. Read more
Source§

impl Handler for PathRouter

Source§

fn serve(&self, req: &mut Request<'_>, res: &mut Response<'_>)

Respond to an incoming HTTP request

Auto Trait Implementations§

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.