Skip to main content

conn_unwrap

Macro conn_unwrap 

Source
macro_rules! conn_unwrap {
    ($option:expr, $conn:expr $(,)?) => { ... };
}
Expand description

ยงUnwraps an Option::Some or returns the Conn.

This is useful for gracefully exiting a Handler without returning an error.

use trillium::{Conn, State, conn_unwrap};
use trillium_testing::TestServer;

#[derive(Copy, Clone)]
struct MyState(&'static str);
let handler = |conn: trillium::Conn| async move {
    let important_state: MyState = *conn_unwrap!(conn.state(), conn);
    conn.ok(important_state.0)
};

let app = TestServer::new(handler).await;
app.get("/").await.assert_status(404); // we never reached the conn.ok line.

let app2 = TestServer::new((State::new(MyState("hi")), handler)).await;
app2.get("/").await.assert_ok().assert_body("hi");