Struct trillium_router::RouterRef[][src]

pub struct RouterRef<'r>(_);
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

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));

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));

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));

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));

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));

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));

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));

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??");

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

Formats the value using the given formatter. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.