pub struct Header<T, N: HeaderName>(pub T, pub PhantomData<N>);Expand description
Extracts a single named header, parsed into T.
The name is supplied by a type implementing HeaderName, so the header a
handler reads is part of its signature rather than a string repeated at the
call site.
use churust_core::{Churust, Header, HeaderName, TestClient};
struct ApiVersion;
impl HeaderName for ApiVersion {
const NAME: &'static str = "x-api-version";
}
let app = Churust::server()
.routing(|r| {
r.get("/", |Header(v, _): Header<u32, ApiVersion>| async move {
format!("v{v}")
});
})
.build();
let res = TestClient::new(app).get("/").header("x-api-version", "2").send().await;
assert_eq!(res.text(), "v2");Fails with 400 Bad Request when the header is absent or does not parse
into T. For an optional header, use Header<Option<T>, N>, or read it
directly with Call::header.
§Why a marker type
The v1 design named Header<T> without saying which header it reads. A
const string parameter is not expressible on stable Rust, deserializing the
whole header map would silently accept junk, and a headers-crate typed
trait would pull in a dependency for a small win. A marker type keeps the
name in the type system, costs nothing at runtime, and needs no dependency.
Tuple Fields§
§0: TThe parsed header value.
1: PhantomData<N>Zero-sized marker naming the header. Ignore it.
Trait Implementations§
Source§impl<T, N> FromCallParts for Header<T, N>
impl<T, N> FromCallParts for Header<T, N>
Source§impl<T, N> OptionalFromCallParts for Header<T, N>
Absent means the header was not sent. A header that was sent but does not
parse into T is an error, since the client did state a value.
impl<T, N> OptionalFromCallParts for Header<T, N>
Absent means the header was not sent. A header that was sent but does not
parse into T is an error, since the client did state a value.
Source§fn from_call_parts_opt<'life0, 'async_trait>(
call: &'life0 mut Call,
) -> Pin<Box<dyn Future<Output = Result<Option<Self>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn from_call_parts_opt<'life0, 'async_trait>(
call: &'life0 mut Call,
) -> Pin<Box<dyn Future<Output = Result<Option<Self>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Self, or Ok(None) when the input is absent. Reserve Err for
input that was supplied and is wrong.