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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
use crate::HeaderValue;
use smallvec::{smallvec, SmallVec};
use smartcow::SmartCow;
use std::{
    borrow::Cow,
    fmt::{Debug, Formatter, Result},
    iter::FromIterator,
    ops::{Deref, DerefMut},
};

/// A header value is a collection of one or more [`HeaderValue`]. It
/// has been optimized for the "one [`HeaderValue`]" case, but can
/// accomodate more than one value.
#[derive(Clone, Eq, PartialEq)]
pub struct HeaderValues(SmallVec<[HeaderValue; 1]>);
impl Deref for HeaderValues {
    type Target = [HeaderValue];

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

#[cfg(feature = "serde")]
impl serde::Serialize for HeaderValues {
    fn serialize<S>(&self, serializer: S) -> std::prelude::v1::Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        match self.one() {
            Some(one) => one.serialize(serializer),
            None => self.0.serialize(serializer),
        }
    }
}

impl Default for HeaderValues {
    fn default() -> Self {
        Self(SmallVec::with_capacity(1))
    }
}

impl DerefMut for HeaderValues {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

impl Debug for HeaderValues {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
        match self.one() {
            Some(one) => Debug::fmt(one, f),
            None => f.debug_list().entries(&self.0).finish(),
        }
    }
}

impl PartialEq<[&str]> for HeaderValues {
    fn eq(&self, other: &[&str]) -> bool {
        &**self == other
    }
}

impl IntoIterator for HeaderValues {
    type Item = HeaderValue;

    type IntoIter = smallvec::IntoIter<[HeaderValue; 1]>;

    fn into_iter(self) -> Self::IntoIter {
        self.0.into_iter()
    }
}

impl<'a> IntoIterator for &'a HeaderValues {
    type Item = &'a HeaderValue;

    type IntoIter = std::slice::Iter<'a, HeaderValue>;

    fn into_iter(self) -> Self::IntoIter {
        self.0.iter()
    }
}

impl<I> FromIterator<I> for HeaderValues
where
    I: Into<HeaderValue>,
{
    fn from_iter<T: IntoIterator<Item = I>>(iter: T) -> Self {
        Self(iter.into_iter().map(Into::into).collect())
    }
}

impl HeaderValues {
    /// Builds an empty `HeaderValues`. This is not generally necessary
    /// in application code. Using a `From` implementation is preferable.
    #[must_use]
    pub fn new() -> Self {
        Self(SmallVec::with_capacity(1))
    }

    /// If there is only a single value, returns that header as a
    /// borrowed string slice if it is utf8. If there are more than
    /// one header value, or if the singular header value is not utf8,
    /// `as_str` returns None.
    pub fn as_str(&self) -> Option<&str> {
        self.one().and_then(HeaderValue::as_str)
    }

    pub(crate) fn as_lower(&self) -> Option<SmartCow<'_>> {
        self.one().and_then(HeaderValue::as_lower)
    }

    /// If there is only a single `HeaderValue` inside this
    /// `HeaderValues`, `one` returns a reference to that value. If
    /// there are more than one header value inside this
    /// `HeaderValues`, `one` returns None.
    pub fn one(&self) -> Option<&HeaderValue> {
        if self.len() == 1 {
            self.0.first()
        } else {
            None
        }
    }

    /// Add another header value to this `HeaderValues`.
    pub fn append(&mut self, value: impl Into<HeaderValue>) {
        self.0.push(value.into());
    }

    /// Adds any number of other header values to this `HeaderValues`.
    pub fn extend(&mut self, values: impl Into<HeaderValues>) {
        let values = values.into();
        self.0.extend(values);
    }
}

// impl AsRef<[u8]> for HeaderValues {
//     fn as_ref(&self) -> &[u8] {
//         self.one().as_ref()
//     }
// }

impl From<&'static [u8]> for HeaderValues {
    fn from(value: &'static [u8]) -> Self {
        HeaderValue::from(value).into()
    }
}

impl From<Vec<u8>> for HeaderValues {
    fn from(value: Vec<u8>) -> Self {
        HeaderValue::from(value).into()
    }
}

impl From<String> for HeaderValues {
    fn from(value: String) -> Self {
        HeaderValue::from(value).into()
    }
}

impl From<&'static str> for HeaderValues {
    fn from(value: &'static str) -> Self {
        HeaderValue::from(value).into()
    }
}

impl From<Cow<'static, str>> for HeaderValues {
    fn from(value: Cow<'static, str>) -> Self {
        HeaderValue::from(value).into()
    }
}

impl From<HeaderValue> for HeaderValues {
    fn from(v: HeaderValue) -> Self {
        Self(smallvec![v])
    }
}

impl<HV> From<Vec<HV>> for HeaderValues
where
    HV: Into<HeaderValue>,
{
    fn from(v: Vec<HV>) -> Self {
        Self(v.into_iter().map(Into::into).collect())
    }
}