rama_http_headers/util/
value_string.rs

1use std::{
2    fmt,
3    str::{self, FromStr},
4};
5
6use bytes::Bytes;
7use rama_http_types::header::HeaderValue;
8
9use super::IterExt;
10use crate::Error;
11
12/// A value that is both a valid `HeaderValue` and `String`.
13#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
14pub(crate) struct HeaderValueString {
15    /// Care must be taken to only set this value when it is also
16    /// a valid `String`, since `as_str` will convert to a `&str`
17    /// in an unchecked manner.
18    value: HeaderValue,
19}
20
21impl HeaderValueString {
22    pub(crate) fn from_val(val: &HeaderValue) -> Result<Self, Error> {
23        if val.to_str().is_ok() {
24            Ok(HeaderValueString { value: val.clone() })
25        } else {
26            Err(Error::invalid())
27        }
28    }
29
30    #[expect(unused)]
31    pub(crate) fn from_string(src: String) -> Option<Self> {
32        // A valid `str` (the argument)...
33        let bytes = Bytes::from(src);
34        HeaderValue::from_maybe_shared(bytes)
35            .ok()
36            .map(|value| HeaderValueString { value })
37    }
38
39    pub(crate) fn from_static(src: &'static str) -> HeaderValueString {
40        // A valid `str` (the argument)...
41        HeaderValueString {
42            value: HeaderValue::from_static(src),
43        }
44    }
45
46    pub(crate) fn as_str(&self) -> &str {
47        // HeaderValueString is only created from HeaderValues
48        // that have validated they are also UTF-8 strings.
49        unsafe { str::from_utf8_unchecked(self.value.as_bytes()) }
50    }
51}
52
53impl fmt::Debug for HeaderValueString {
54    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
55        fmt::Debug::fmt(self.as_str(), f)
56    }
57}
58
59impl fmt::Display for HeaderValueString {
60    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
61        fmt::Display::fmt(self.as_str(), f)
62    }
63}
64
65impl super::TryFromValues for HeaderValueString {
66    fn try_from_values<'i, I>(values: &mut I) -> Result<Self, Error>
67    where
68        I: Iterator<Item = &'i HeaderValue>,
69    {
70        values
71            .just_one()
72            .map(HeaderValueString::from_val)
73            .unwrap_or_else(|| Err(Error::invalid()))
74    }
75}
76
77impl<'a> From<&'a HeaderValueString> for HeaderValue {
78    fn from(src: &'a HeaderValueString) -> HeaderValue {
79        src.value.clone()
80    }
81}
82
83#[derive(Debug)]
84pub(crate) struct FromStrError(());
85
86impl FromStr for HeaderValueString {
87    type Err = FromStrError;
88
89    fn from_str(src: &str) -> Result<Self, Self::Err> {
90        // A valid `str` (the argument)...
91        src.parse()
92            .map(|value| HeaderValueString { value })
93            .map_err(|_| FromStrError(()))
94    }
95}