http1/headers/
value.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
use std::{borrow::Cow, convert::Infallible, fmt::Display};

#[derive(Debug)]
pub struct InvalidHeaderValue(String);

impl std::error::Error for InvalidHeaderValue {}

impl Display for InvalidHeaderValue {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "invalid header value, expected ascii string: {:?}",
            self.0
        )
    }
}

impl From<Infallible> for InvalidHeaderValue {
    fn from(_: Infallible) -> Self {
        unreachable!()
    }
}

#[derive(Default, Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct HeaderValue(Cow<'static, str>);

impl HeaderValue {
    pub fn from_static(s: &'static str) -> Self {
        Self::from_checked_static(s).unwrap()
    }

    pub fn from_string(s: String) -> Self {
        Self::from_checked_string(s).unwrap()
    }

    pub fn from_checked_static(s: &'static str) -> Result<Self, InvalidHeaderValue> {
        if !s.is_ascii() {
            return Err(InvalidHeaderValue(s.to_owned()));
        }

        Ok(HeaderValue(Cow::Borrowed(s)))
    }

    pub fn from_checked_string(s: String) -> Result<Self, InvalidHeaderValue> {
        if !s.is_ascii() {
            return Err(InvalidHeaderValue(s));
        }

        Ok(HeaderValue(Cow::Owned(s)))
    }

    pub fn as_str(&self) -> &str {
        &self.0
    }
}

impl Display for HeaderValue {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.as_str())
    }
}

impl AsRef<str> for HeaderValue {
    fn as_ref(&self) -> &str {
        self.as_str()
    }
}

impl<'a> PartialEq<&'a str> for HeaderValue {
    fn eq(&self, other: &&'a str) -> bool {
        self.as_str() == *other
    }
}

impl<'a> PartialEq<&'a str> for &'a HeaderValue {
    fn eq(&self, other: &&'a str) -> bool {
        self.as_str() == *other
    }
}

macro_rules! impl_from_value {
    ($($type:ty),+ $(,)?) => {
        $(
            impl From<$type> for HeaderValue {
                fn from(value: $type) -> Self {
                    HeaderValue(Cow::Owned(value.to_string()))
                }
            }
        )*
    };
}

impl_from_value! { u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize, f32, f64 }

impl TryFrom<String> for HeaderValue {
    type Error = InvalidHeaderValue;

    fn try_from(value: String) -> Result<Self, Self::Error> {
        HeaderValue::from_checked_string(value)
    }
}

impl TryFrom<&'static str> for HeaderValue {
    type Error = InvalidHeaderValue;

    fn try_from(value: &'static str) -> Result<Self, Self::Error> {
        HeaderValue::from_checked_static(value)
    }
}

impl TryFrom<Cow<'static, str>> for HeaderValue {
    type Error = InvalidHeaderValue;

    fn try_from(value: Cow<'static, str>) -> Result<Self, Self::Error> {
        match value {
            Cow::Borrowed(s) => HeaderValue::from_checked_static(s),
            Cow::Owned(s) => HeaderValue::from_checked_string(s),
        }
    }
}