Macro trillium::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_testing::prelude::*;
use trillium::{Conn, conn_unwrap, State};

#[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)
};

assert_not_handled!(get("/").on(&handler)); // we never reached the conn.ok line.

assert_ok!(
    get("/").on(&(State::new(MyState("hi")), handler)),
    "hi"
);