pub trait HeaderId {
const HEADER_NAME: &'static str;
// Required method
fn as_str(&self) -> Cow<'_, str>;
}Expand description
A type that is transported as a single HTTP header value.
Implementors provide:
HEADER_NAME— the canonical header name exactly as it appears in an HTTP request or response, e.g."X-Request-Id".as_str— the string representation of the value, borrowed when the value is already stored as aString, or owned when derived on the fly (e.g. for UUID-backed or structured types).
§Generic middleware
The trait enables writing helpers that work uniformly over any header-ID type without duplicating the header-lookup logic:
use api_bones::header_id::HeaderId;
fn log_header<T: HeaderId>(value: &T) {
println!("{}: {}", T::HEADER_NAME, value.as_str());
}In axum middleware you can use T::HEADER_NAME to look up the right header
for any T: HeaderId + FromStr:
fn extract_header<T>(parts: &axum::http::request::Parts)
-> Result<T, api_bones::ApiError>
where
T: HeaderId + core::str::FromStr,
T::Err: core::fmt::Display,
{
let raw = parts
.headers
.get(T::HEADER_NAME)
.ok_or_else(|| api_bones::ApiError::bad_request(
format!("missing required header: {}", T::HEADER_NAME)
))?
.to_str()
.map_err(|_| api_bones::ApiError::bad_request(
format!("header {} contains non-UTF-8 bytes", T::HEADER_NAME)
))?;
raw.parse::<T>()
.map_err(|e| api_bones::ApiError::bad_request(
format!("invalid {}: {e}", T::HEADER_NAME)
))
}Required Associated Constants§
Sourceconst HEADER_NAME: &'static str
const HEADER_NAME: &'static str
The canonical HTTP header name for this type.
Examples: "X-Request-Id", "X-Correlation-Id",
"Idempotency-Key", "traceparent".
Required Methods§
Sourcefn as_str(&self) -> Cow<'_, str>
fn as_str(&self) -> Cow<'_, str>
Return the string representation of this header value.
Returns a Cow::Borrowed slice when the value is already stored as a
String, and a Cow::Owned string when the representation must be
computed (e.g. for UUID-backed or structured types).
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.
Implementors§
Source§impl HeaderId for CorrelationId
Available on crate feature std only.
impl HeaderId for CorrelationId
std only.const HEADER_NAME: &'static str = "X-Correlation-Id"
Source§impl HeaderId for IdempotencyKey
Available on crate feature std only.
impl HeaderId for IdempotencyKey
std only.const HEADER_NAME: &'static str = "Idempotency-Key"
Source§impl HeaderId for OrgId
Available on crate feature std only.
impl HeaderId for OrgId
std only.const HEADER_NAME: &'static str = "X-Org-Id"
Source§impl HeaderId for OrgPath
Available on crate feature std only.
impl HeaderId for OrgPath
std only.const HEADER_NAME: &'static str = "X-Org-Path"
Source§impl HeaderId for RequestId
Available on crate feature std only.
impl HeaderId for RequestId
std only.const HEADER_NAME: &'static str = "X-Request-Id"
Source§impl HeaderId for TraceContext
Available on crate feature std only.
impl HeaderId for TraceContext
std only.