use http::{Extensions, HeaderMap};
#[derive(Debug, Clone)]
pub struct RequestId(pub String);
impl RequestId {
#[must_use]
pub fn from_headers(headers: &HeaderMap) -> Self {
Self(
headers
.get("x-request-id")
.and_then(|value| value.to_str().ok())
.map(str::to_owned)
.unwrap_or_else(|| uuid::Uuid::new_v4().to_string()),
)
}
}
#[derive(Debug, Clone)]
pub struct CorrelationId(pub String);
impl CorrelationId {
#[must_use]
pub fn from_headers(headers: &HeaderMap) -> Self {
Self(
headers
.get("x-correlation-id")
.and_then(|value| value.to_str().ok())
.map(str::to_owned)
.unwrap_or_else(|| uuid::Uuid::new_v4().to_string()),
)
}
}
pub fn set_request_id(extensions: &mut Extensions, request_id: impl Into<String>) {
extensions.insert(RequestId(request_id.into()));
}
#[must_use]
pub fn request_id_from_extensions(extensions: &Extensions) -> Option<&RequestId> {
extensions.get::<RequestId>()
}
pub fn set_correlation_id(extensions: &mut Extensions, correlation_id: impl Into<String>) {
extensions.insert(CorrelationId(correlation_id.into()));
}
#[must_use]
pub fn correlation_id_from_extensions(extensions: &Extensions) -> Option<&CorrelationId> {
extensions.get::<CorrelationId>()
}