Skip to main content

systemprompt_api/services/middleware/context/sources/
headers.rs

1use axum::http::HeaderMap;
2use systemprompt_models::execution::ContextExtractionError;
3
4#[derive(Debug, Clone, Copy)]
5pub struct HeaderSource;
6
7impl HeaderSource {
8    pub fn extract_required(
9        headers: &HeaderMap,
10        name: &str,
11    ) -> Result<String, ContextExtractionError> {
12        headers
13            .get(name)
14            .ok_or_else(|| ContextExtractionError::MissingHeader(name.to_string()))?
15            .to_str()
16            .map(ToString::to_string)
17            .map_err(|e| ContextExtractionError::InvalidHeaderValue {
18                header: name.to_string(),
19                reason: e.to_string(),
20            })
21    }
22
23    pub fn extract_optional(headers: &HeaderMap, name: &str) -> Option<String> {
24        headers
25            .get(name)
26            .and_then(|v| v.to_str().ok())
27            .map(ToString::to_string)
28    }
29}