Struct trillium_router::RouterRef

source ·
pub struct RouterRef<'r>(/* private fields */);
Expand description

§A &mut Router for use with Router::build

A wrapper around a &mut Router that supports imperative route registration. See Router::build for further documentation.

Implementations§

source§

impl<'r> RouterRef<'r>

source

pub fn get<R>(&mut self, path: R, handler: impl Handler)
where R: TryInto<RouteSpec>, R::Error: Debug,

Registers a handler for the get http method.

let router = Router::build(|mut router| {
    router.get("/some/route", |conn: Conn| async move {
        conn.ok("success")
    });
});

use trillium_testing::{methods::get, assert_ok, assert_not_handled};
assert_ok!(get("/some/route").on(&router), "success");
assert_not_handled!(get("/other/route").on(&router));
source

pub fn post<R>(&mut self, path: R, handler: impl Handler)
where R: TryInto<RouteSpec>, R::Error: Debug,

Registers a handler for the post http method.

let router = Router::build(|mut router| {
    router.post("/some/route", |conn: Conn| async move {
        conn.ok("success")
    });
});

use trillium_testing::{methods::post, assert_ok, assert_not_handled};
assert_ok!(post("/some/route").on(&router), "success");
assert_not_handled!(post("/other/route").on(&router));
source

pub fn put<R>(&mut self, path: R, handler: impl Handler)
where R: TryInto<RouteSpec>, R::Error: Debug,

Registers a handler for the put http method.

let router = Router::build(|mut router| {
    router.put("/some/route", |conn: Conn| async move {
        conn.ok("success")
    });
});

use trillium_testing::{methods::put, assert_ok, assert_not_handled};
assert_ok!(put("/some/route").on(&router), "success");
assert_not_handled!(put("/other/route").on(&router));
source

pub fn delete<R>(&mut self, path: R, handler: impl Handler)
where R: TryInto<RouteSpec>, R::Error: Debug,

Registers a handler for the delete http method.

let router = Router::build(|mut router| {
    router.delete("/some/route", |conn: Conn| async move {
        conn.ok("success")
    });
});

use trillium_testing::{methods::delete, assert_ok, assert_not_handled};
assert_ok!(delete("/some/route").on(&router), "success");
assert_not_handled!(delete("/other/route").on(&router));
source

pub fn patch<R>(&mut self, path: R, handler: impl Handler)
where R: TryInto<RouteSpec>, R::Error: Debug,

Registers a handler for the patch http method.

let router = Router::build(|mut router| {
    router.patch("/some/route", |conn: Conn| async move {
        conn.ok("success")
    });
});

use trillium_testing::{methods::patch, assert_ok, assert_not_handled};
assert_ok!(patch("/some/route").on(&router), "success");
assert_not_handled!(patch("/other/route").on(&router));
source

pub fn all<R>(&mut self, path: R, handler: impl Handler)
where R: TryInto<RouteSpec>, R::Error: Debug,

Appends the handler to all (get, post, put, delete, and patch) methods.

let router = Router::build(|mut router| {
    router.all("/any", |conn: Conn| async move {
        let response = format!("you made a {} request to /any", conn.method());
        conn.ok(response)
    });
});

use trillium_testing::prelude::*;
assert_ok!(get("/any").on(&router), "you made a GET request to /any");
assert_ok!(post("/any").on(&router), "you made a POST request to /any");
assert_ok!(delete("/any").on(&router), "you made a DELETE request to /any");
assert_ok!(patch("/any").on(&router), "you made a PATCH request to /any");
assert_ok!(put("/any").on(&router), "you made a PUT request to /any");

assert_not_handled!(get("/").on(&router));
source

pub fn any<IntoMethod, R>( &mut self, methods: &[IntoMethod], path: R, handler: impl Handler, )
where R: TryInto<RouteSpec>, R::Error: Debug, IntoMethod: TryInto<Method> + Clone, <IntoMethod as TryInto<Method>>::Error: Debug,

Appends the handler to each of the provided http methods.

let router = Router::build(|mut router|{
    router.any(&["get", "post"], "/get_or_post", |conn: Conn| async move {
        let response = format!("you made a {} request to /get_or_post", conn.method());
        conn.ok(response)
    });
});

use trillium_testing::prelude::*;
assert_ok!(get("/get_or_post").on(&router), "you made a GET request to /get_or_post");
assert_ok!(post("/get_or_post").on(&router), "you made a POST request to /get_or_post");
assert_not_handled!(delete("/any").on(&router));
assert_not_handled!(patch("/any").on(&router));
assert_not_handled!(put("/any").on(&router));
assert_not_handled!(get("/").on(&router));
source

pub fn add_route<M, R>(&mut self, method: M, path: R, handler: impl Handler)
where M: TryInto<Method>, <M as TryInto<Method>>::Error: Debug, R: TryInto<RouteSpec>, R::Error: Debug,

Registers a handler for a method other than get, put, post, patch, or delete.

let router = Router::build(|mut router| {
    router.add_route("OPTIONS", "/some/route", |conn: Conn| async move {
        conn.ok("directly handling options")
    });

    router.add_route(Method::Checkin, "/some/route", |conn: Conn| async move {
        conn.ok("checkin??")
    });
});

use trillium_testing::{prelude::*, TestConn};
assert_ok!(TestConn::build(Method::Options, "/some/route", ()).on(&router), "directly handling options");
assert_ok!(TestConn::build("checkin", "/some/route", ()).on(&router), "checkin??");
source

pub fn set_options_handling(&mut self, options_enabled: bool)

enable or disable the router’s behavior of responding to OPTIONS requests with the supported methods at given path.

default: enabled

see crate-level docs for further explanation of the default behavior.

Trait Implementations§

source§

impl<'r> Debug for RouterRef<'r>

source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'r> Freeze for RouterRef<'r>

§

impl<'r> !RefUnwindSafe for RouterRef<'r>

§

impl<'r> Send for RouterRef<'r>

§

impl<'r> Sync for RouterRef<'r>

§

impl<'r> Unpin for RouterRef<'r>

§

impl<'r> !UnwindSafe for RouterRef<'r>

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>,

§

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>,

§

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.