pub trait RouterConnExt {
// Required methods
fn param<'a>(&'a self, param: &str) -> Option<&'a str>;
fn wildcard(&self) -> Option<&str>;
fn route(&self) -> Option<&str>;
}Expand description
Extends trillium::Conn with accessors for router params.
Required Methods§
Sourcefn param<'a>(&'a self, param: &str) -> Option<&'a str>
fn param<'a>(&'a self, param: &str) -> Option<&'a str>
Retrieves a captured param from the conn. Note that this will only be Some if the exact param is present in the matched route.
Routefinder params are defined starting with a colon, but the colon is not needed when fetching the param.
use trillium::{Conn, conn_unwrap};
use trillium_router::{Router, RouterConnExt};
use trillium_testing::TestServer;
let router = Router::new().get("/pages/:page_name", |conn: Conn| async move {
let page_name = conn_unwrap!(conn.param("page_name"), conn);
let content = format!("you have reached the page named {}", page_name);
conn.ok(content)
});
let app = TestServer::new(router).await;
app.get("/pages/trillium")
.await
.assert_ok()
.assert_body("you have reached the page named trillium");Sourcefn wildcard(&self) -> Option<&str>
fn wildcard(&self) -> Option<&str>
Retrieves the wildcard match from the conn. Note that this will only be Some if the matched route contains a wildcard, as expressed by a “*” in the routefinder route spec.
use trillium::{Conn, conn_unwrap};
use trillium_router::{Router, RouterConnExt};
use trillium_testing::TestServer;
let router = Router::new().get("/pages/*", |conn: Conn| async move {
let wildcard = conn_unwrap!(conn.wildcard(), conn);
let content = format!("the wildcard matched {}", wildcard);
conn.ok(content)
});
let app = TestServer::new(router).await;
app.get("/pages/this/is/a/wildcard/match")
.await
.assert_ok()
.assert_body("the wildcard matched this/is/a/wildcard/match");Sourcefn route(&self) -> Option<&str>
fn route(&self) -> Option<&str>
Retrieves the matched route specification
use trillium::{Conn, conn_unwrap};
use trillium_router::{Router, RouterConnExt};
use trillium_testing::TestServer;
let router = Router::new().get("/pages/:page_id", |conn: Conn| async move {
let route = conn_unwrap!(conn.route(), conn).to_string();
conn.ok(format!("route was {route}"))
});
let app = TestServer::new(router).await;
app.get("/pages/12345")
.await
.assert_ok()
.assert_body("route was /pages/:page_id");