http_types/headers/
header_values.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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
use crate::headers::{HeaderValue, Values};

use std::fmt::{self, Debug, Display};
use std::iter::FromIterator;
use std::ops::{Deref, DerefMut, Index};
use std::slice::SliceIndex;

/// A list of `HeaderValue`s.
///
/// This always contains at least one header value.
#[derive(Clone)]
pub struct HeaderValues {
    pub(crate) inner: Vec<HeaderValue>,
}

impl HeaderValues {
    /// Move all values from `other` into `self`, leaving `other` empty.
    pub fn append(&mut self, other: &mut Self) {
        self.inner.append(&mut other.inner)
    }

    /// Returns a reference or a value depending on the type of index.
    pub fn get(&self, index: usize) -> Option<&HeaderValue> {
        self.inner.get(index)
    }

    /// Returns a mutable reference or a value depending on the type of index.
    pub fn get_mut(&mut self, index: usize) -> Option<&mut HeaderValue> {
        self.inner.get_mut(index)
    }

    /// Returns `true` if there is a value corresponding to the specified `HeaderValue` in the list,
    /// `false` otherwise.
    pub fn contains(&self, value: &HeaderValue) -> bool {
        self.inner.contains(value)
    }

    /// Returns the last `HeaderValue`.
    pub fn last(&self) -> &HeaderValue {
        self.inner
            .last()
            .expect("HeaderValues must always contain at least one value")
    }

    /// An iterator visiting all header values in arbitrary order.
    pub fn iter(&self) -> Values<'_> {
        Values::new_values(self)
    }

    // /// An iterator visiting all header values in arbitrary order, with mutable
    // /// references to the values.
    // pub fn iter_mut(&mut self) -> ValuesMut<'_> {
    //     ValuesMut {
    //         inner: self.headers.iter_mut(),
    //     }
    // }
}

impl<I: SliceIndex<[HeaderValue]>> Index<I> for HeaderValues {
    type Output = I::Output;

    #[inline]
    fn index(&self, index: I) -> &Self::Output {
        Index::index(&self.inner, index)
    }
}

impl FromIterator<HeaderValue> for HeaderValues {
    fn from_iter<I>(iter: I) -> HeaderValues
    where
        I: IntoIterator<Item = HeaderValue>,
    {
        let iter = iter.into_iter();
        let mut output = Vec::with_capacity(iter.size_hint().0);
        for v in iter {
            output.push(v);
        }
        HeaderValues { inner: output }
    }
}

impl Debug for HeaderValues {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if self.inner.len() == 1 {
            write!(f, "{:?}", self.inner[0])
        } else {
            f.debug_list().entries(self.inner.iter()).finish()
        }
    }
}

impl Display for HeaderValues {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut list = f.debug_list();
        for v in &self.inner {
            list.entry(&v);
        }
        list.finish()
    }
}

impl PartialEq<str> for HeaderValues {
    fn eq(&self, other: &str) -> bool {
        self.inner.len() == 1 && self.inner[0] == other
    }
}

impl<'a> PartialEq<&'a str> for HeaderValues {
    fn eq(&self, other: &&'a str) -> bool {
        self.inner.len() == 1 && &self.inner[0] == other
    }
}

impl<'a> PartialEq<[&'a str]> for HeaderValues {
    fn eq(&self, other: &[&'a str]) -> bool {
        self.inner.iter().eq(other.iter())
    }
}

impl PartialEq<String> for HeaderValues {
    fn eq(&self, other: &String) -> bool {
        self.inner.len() == 1 && self.inner[0] == *other
    }
}

impl<'a> PartialEq<&String> for HeaderValues {
    fn eq(&self, other: &&String) -> bool {
        self.inner.len() == 1 && self.inner[0] == **other
    }
}

impl From<HeaderValue> for HeaderValues {
    fn from(other: HeaderValue) -> Self {
        Self { inner: vec![other] }
    }
}

impl AsRef<HeaderValue> for HeaderValues {
    fn as_ref(&self) -> &HeaderValue {
        &self.inner[0]
    }
}

impl AsMut<HeaderValue> for HeaderValues {
    fn as_mut(&mut self) -> &mut HeaderValue {
        &mut self.inner[0]
    }
}

impl Deref for HeaderValues {
    type Target = HeaderValue;

    fn deref(&self) -> &HeaderValue {
        &self.inner[0]
    }
}

impl DerefMut for HeaderValues {
    fn deref_mut(&mut self) -> &mut HeaderValue {
        &mut self.inner[0]
    }
}

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

    #[inline]
    fn into_iter(self) -> Self::IntoIter {
        self.iter()
    }
}

impl From<Vec<HeaderValue>> for HeaderValues {
    fn from(headers: Vec<HeaderValue>) -> Self {
        Self { inner: headers }
    }
}

impl IntoIterator for HeaderValues {
    type Item = HeaderValue;
    type IntoIter = std::vec::IntoIter<HeaderValue>;

    #[inline]
    fn into_iter(self) -> Self::IntoIter {
        self.inner.into_iter()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_debug_single() {
        let header_values = HeaderValues {
            inner: vec!["foo0".parse().unwrap()],
        };
        assert_eq!(format!("{:?}", header_values), "\"foo0\"");
    }
    #[test]
    fn test_debug_multiple() {
        let header_values = HeaderValues {
            inner: vec!["foo0".parse().unwrap(), "foo1".parse().unwrap()],
        };
        assert_eq!(format!("{:?}", header_values), r#"["foo0", "foo1"]"#);
    }
}