Debian Codegraph @

src/media_type_list.rs

raw symbols SCIP
1 use super::{error::*, media_type::*, parse::*};
2
3 /// A comma-separated list of `MediaType`s used in the HTTP `Accept` header. ([RFC 7231](https://www.rfc-editor.org/rfc/rfc7231#section-5.3.2))
4 ///
5 /// ```
6 /// use mediatype::{MediaType, MediaTypeList};
7 ///
8 /// let mut list = MediaTypeList::new(
9 /// "text/html, application/xhtml+xml, application/xml;q=0.9, */*;q=0.8",
10 /// );
11 /// assert_eq!(list.next(), Some(MediaType::parse("text/html")));
12 /// assert_eq!(list.next(), Some(MediaType::parse("application/xhtml+xml")));
13 /// assert_eq!(list.next(), Some(MediaType::parse("application/xml;q=0.9")));
14 /// assert_eq!(list.next(), Some(MediaType::parse("*/*;q=0.8")));
15 /// assert_eq!(list.next(), None);
16 ///
17 /// // Commas can be used in a quoted string.
18 /// let mut list = MediaTypeList::new("text/html; message=\"Hello, world!\"");
19 /// assert_eq!(list.next(), Some(MediaType::parse("text/html; message=\"Hello, world!\"")));
20 /// assert_eq!(list.next(), None);
21 /// ```
22 pub struct MediaTypeList<'a>(&'a str);
23
24 impl<'a> MediaTypeList<'a> {
25 /// Constructs a `MediaTypeList`.
26 pub fn new(s: &'a str) -> Self {
27 Self(s)
28 }
29 }
30
31 impl<'a> Iterator for MediaTypeList<'a> {
32 type Item = Result<MediaType<'a>, MediaTypeError>;
33
34 fn next(&mut self) -> Option<Self::Item> {
35 {
36 let index = self.0.find(|c| !is_ows(c))?;
37 self.0 = &self.0[index..];
38 }
39 if self.0.is_empty() {
40 return None;
41 }
42 let mut end = 0;
43 let mut quoted = false;
44 let mut escaped = false;
45 while let Some(c) = self.0.as_bytes().get(end) {
46 if escaped {
47 escaped = false;
48 } else {
49 match c {
50 b'"' => quoted = !quoted,
51 b'\\' if quoted => escaped = true,
52 b',' if !quoted => break,
53 _ => (),
54 }
55 }
56 end += 1;
57 }
58 let madia_type = MediaType::parse(&self.0[..end]);
59 let end = self.0.len().min(end + 1);
60 self.0 = &self.0[end..];
61 Some(madia_type)
62 }
63
64 fn size_hint(&self) -> (usize, Option<usize>) {
65 (0, Some(self.0.matches(',').count() + 1))
66 }
67 }
68
69 #[cfg(test)]
70 mod tests {
71 use super::*;
72 use crate::params::ReadParams;
73
74 #[test]
75 fn empty() {
76 let mut list = MediaTypeList::new("");
77 assert_eq!(list.next(), None);
78 let mut list = MediaTypeList::new(" \t ");
79 assert_eq!(list.next(), None);
80 }
81
82 #[test]
83 fn invalid() {
84 let mut list = MediaTypeList::new(",,,");
85 assert_eq!(list.next(), Some(MediaType::parse("")));
86 assert_eq!(list.next(), Some(MediaType::parse("")));
87 assert_eq!(list.next(), Some(MediaType::parse("")));
88 assert_eq!(list.next(), None);
89 }
90
91 #[test]
92 fn simple() {
93 let mut list = MediaTypeList::new("text/html");
94 assert_eq!(list.next(), Some(MediaType::parse("text/html")));
95 assert_eq!(list.next(), None);
96 let mut list = MediaTypeList::new("image/*");
97 assert_eq!(list.next(), Some(MediaType::parse("image/*")));
98 assert_eq!(list.next(), None);
99 let mut list = MediaTypeList::new("*/*");
100 assert_eq!(list.next(), Some(MediaType::parse("*/*")));
101 assert_eq!(list.next(), None);
102 }
103
104 #[test]
105 fn list() {
106 let mut list = MediaTypeList::new("text/html, image/*, */*");
107 assert_eq!(list.next(), Some(MediaType::parse("text/html")));
108 assert_eq!(list.next(), Some(MediaType::parse("image/*")));
109 assert_eq!(list.next(), Some(MediaType::parse("*/*")));
110 assert_eq!(list.next(), None);
111 }
112
113 #[test]
114 fn params() {
115 let mut list = MediaTypeList::new(
116 "text/html, application/xhtml+xml, application/xml;q=0.9, */*;q=0.8",
117 );
118 assert_eq!(list.next(), Some(MediaType::parse("text/html")));
119 assert_eq!(list.next(), Some(MediaType::parse("application/xhtml+xml")));
120 assert_eq!(list.next(), Some(MediaType::parse("application/xml;q=0.9")));
121 assert_eq!(list.next(), Some(MediaType::parse("*/*;q=0.8")));
122 assert_eq!(list.next(), None);
123 }
124
125 #[test]
126 fn quoted_params() {
127 let mut list = MediaTypeList::new("text/html; message=\"Hello, world!\", application/xhtml+xml; message=\"Hello, world?\"");
128
129 let media_type = list.next();
130 assert_eq!(
131 media_type,
132 Some(MediaType::parse("text/html; message=\"Hello, world!\""))
133 );
134 assert_eq!(
135 media_type
136 .unwrap()
137 .unwrap()
138 .params()
139 .next()
140 .unwrap()
141 .1
142 .unquoted_str(),
143 "Hello, world!"
144 );
145
146 let media_type = list.next();
147 assert_eq!(
148 media_type,
149 Some(MediaType::parse(
150 "application/xhtml+xml; message=\"Hello, world?\""
151 ))
152 );
153 assert_eq!(
154 media_type
155 .unwrap()
156 .unwrap()
157 .params()
158 .next()
159 .unwrap()
160 .1
161 .unquoted_str(),
162 "Hello, world?"
163 );
164
165 assert_eq!(list.next(), None);
166 }
167
168 #[test]
169 fn escaped_params() {
170 let mut list = MediaTypeList::new("image/svg+xml; charset=\"UT\\\"F-8\"");
171 assert_eq!(
172 list.next(),
173 Some(MediaType::parse("image/svg+xml; charset=\"UT\\\"F-8\""))
174 );
175 assert_eq!(list.next(), None);
176 }
177
178 #[test]
179 fn size_hint() {
180 let list = MediaTypeList::new("");
181 assert_eq!(list.size_hint(), (0, Some(1)));
182
183 let list = MediaTypeList::new(
184 "text/html, application/xhtml+xml, application/xml;q=0.9, */*;q=0.8",
185 );
186 assert_eq!(list.size_hint(), (0, Some(4)));
187
188 let list = MediaTypeList::new("text/html; message=\"Hello, world!\"");
189 assert_eq!(list.size_hint(), (0, Some(2)));
190 }
191 }