Skip to main content

commit_bridge/domain/
accept_header.rs

1//! Domain type to represent an HTTP Accept header.
2
3use crate::error::ValidationError;
4use http::header::HeaderValue;
5use serde::{Deserialize, Serialize};
6
7/// The HTTP Accept header value.
8#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
9#[serde(try_from = "String", into = "String")]
10pub struct AcceptHeader(String);
11
12impl AcceptHeader {
13    /// Create a new `AcceptHeader`.
14    pub fn new(value: String) -> Result<Self, ValidationError> {
15        Self::try_from(value)
16    }
17
18    /// Get the inner string.
19    pub fn into_inner(self) -> String {
20        self.0
21    }
22}
23
24impl TryFrom<String> for AcceptHeader {
25    type Error = ValidationError;
26
27    fn try_from(value: String) -> Result<Self, Self::Error> {
28        if let Err(e) = HeaderValue::from_str(&value) {
29            tracing::warn!(
30                "Validation failed for AcceptHeader: {}. Error: {}",
31                value,
32                e
33            );
34            return Err(ValidationError::InvalidValue(format!(
35                "Invalid Accept header format: {}",
36                value
37            )));
38        }
39
40        Ok(AcceptHeader(value))
41    }
42}
43
44impl From<AcceptHeader> for String {
45    fn from(val: AcceptHeader) -> Self {
46        val.0
47    }
48}
49
50impl std::fmt::Display for AcceptHeader {
51    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
52        write!(f, "{}", self.0)
53    }
54}