Skip to main content

wyrd_runtime/
request_id.rs

1//! Request ID middleware shell.
2
3use wyrd_spec::request_id::RequestId;
4
5/// Header name used for Wyrd request IDs.
6pub const REQUEST_ID_HEADER: &str = "x-request-id";
7
8/// Mint a fresh UUID v7 request ID.
9///
10/// # Panics
11///
12/// Panics if a generated UUIDv7 fails `RequestId` validation, which would be a validator bug.
13#[must_use]
14pub fn mint() -> RequestId {
15    let raw = uuid::Uuid::now_v7().to_string();
16    RequestId::parse(&raw)
17        .unwrap_or_else(|error| panic!("generated UUIDv7 did not validate as RequestId: {error}"))
18}
19
20/// Request ID propagation decision.
21#[derive(Debug, Clone, PartialEq, Eq)]
22pub struct RequestIdPropagation {
23    /// Request ID value.
24    pub request_id: Option<RequestId>,
25    /// Whether the server should mint a fresh ID.
26    pub should_generate: bool,
27}
28
29/// Inspect an optional request-id header.
30#[must_use]
31pub fn inspect_header(value: Option<&str>) -> RequestIdPropagation {
32    match value.and_then(|value| RequestId::parse(value).ok()) {
33        Some(request_id) => RequestIdPropagation {
34            request_id: Some(request_id),
35            should_generate: false,
36        },
37        None => RequestIdPropagation {
38            request_id: None,
39            should_generate: true,
40        },
41    }
42}