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
use super::{error::*, media_type::*, parse::*};

/// A comma-separated list of `MediaType`s used in HTTP `Accept` header. ([RFC 7231](https://www.rfc-editor.org/rfc/rfc7231#section-5.3.2))
///
/// ```
/// use mediatype::{MediaType, MediaTypeList};
///
/// let mut list = MediaTypeList::new(
///     "text/html, application/xhtml+xml, application/xml;q=0.9, */*;q=0.8",
/// );
/// assert_eq!(list.next(), Some(MediaType::parse("text/html")));
/// assert_eq!(list.next(), Some(MediaType::parse("application/xhtml+xml")));
/// assert_eq!(list.next(), Some(MediaType::parse("application/xml;q=0.9")));
/// assert_eq!(list.next(), Some(MediaType::parse("*/*;q=0.8")));
/// assert_eq!(list.next(), None);
///
/// // A comma can be used in a quoted string.
/// let mut list = MediaTypeList::new("text/html; message=\"Hello, world!\"");
/// assert_eq!(list.next(), Some(MediaType::parse("text/html; message=\"Hello, world!\"")));
/// assert_eq!(list.next(), None);
/// ```
pub struct MediaTypeList<'a>(&'a str);

impl<'a> MediaTypeList<'a> {
    /// Constructs a `MediaTypeList`.
    pub fn new(s: &'a str) -> Self {
        Self(s)
    }
}

impl<'a> Iterator for MediaTypeList<'a> {
    type Item = Result<MediaType<'a>, MediaTypeError>;

    fn next(&mut self) -> Option<Self::Item> {
        if let Some(index) = self.0.find(|c| !is_ows(c)) {
            self.0 = &self.0[index..];
        } else {
            return None;
        }
        if self.0.is_empty() {
            return None;
        }
        let mut end = 0;
        let mut quoted = false;
        while let Some(c) = self.0.as_bytes().get(end) {
            match c {
                b'"' => quoted = !quoted,
                b',' if !quoted => break,
                _ => (),
            }
            end += 1;
        }
        let madia_type = MediaType::parse(&self.0[..end]);
        let end = self.0.as_bytes().len().min(end + 1);
        self.0 = &self.0[end..];
        Some(madia_type)
    }
}

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

    #[test]
    fn empty() {
        let mut list = MediaTypeList::new("");
        assert_eq!(list.next(), None);
        let mut list = MediaTypeList::new("   \t   ");
        assert_eq!(list.next(), None);
    }

    #[test]
    fn invalid() {
        let mut list = MediaTypeList::new(",,,");
        assert_eq!(list.next(), Some(MediaType::parse("")));
        assert_eq!(list.next(), Some(MediaType::parse("")));
        assert_eq!(list.next(), Some(MediaType::parse("")));
        assert_eq!(list.next(), None);
    }

    #[test]
    fn simple() {
        let mut list = MediaTypeList::new("text/html");
        assert_eq!(list.next(), Some(MediaType::parse("text/html")));
        assert_eq!(list.next(), None);
        let mut list = MediaTypeList::new("image/*");
        assert_eq!(list.next(), Some(MediaType::parse("image/*")));
        assert_eq!(list.next(), None);
        let mut list = MediaTypeList::new("*/*");
        assert_eq!(list.next(), Some(MediaType::parse("*/*")));
        assert_eq!(list.next(), None);
    }

    #[test]
    fn list() {
        let mut list = MediaTypeList::new("text/html, image/*, */*");
        assert_eq!(list.next(), Some(MediaType::parse("text/html")));
        assert_eq!(list.next(), Some(MediaType::parse("image/*")));
        assert_eq!(list.next(), Some(MediaType::parse("*/*")));
        assert_eq!(list.next(), None);
    }

    #[test]
    fn params() {
        let mut list = MediaTypeList::new(
            "text/html, application/xhtml+xml, application/xml;q=0.9, */*;q=0.8",
        );
        assert_eq!(list.next(), Some(MediaType::parse("text/html")));
        assert_eq!(list.next(), Some(MediaType::parse("application/xhtml+xml")));
        assert_eq!(list.next(), Some(MediaType::parse("application/xml;q=0.9")));
        assert_eq!(list.next(), Some(MediaType::parse("*/*;q=0.8")));
        assert_eq!(list.next(), None);
    }

    #[test]
    fn quoted_params() {
        let mut list = MediaTypeList::new("text/html; message=\"Hello, world!\", application/xhtml+xml; message=\"Hello, world?\"");

        let media_type = list.next();
        assert_eq!(
            media_type,
            Some(MediaType::parse("text/html; message=\"Hello, world!\""))
        );
        assert_eq!(
            media_type
                .unwrap()
                .unwrap()
                .params()
                .next()
                .unwrap()
                .1
                .unquoted_str(),
            "Hello, world!"
        );

        let media_type = list.next();
        assert_eq!(
            media_type,
            Some(MediaType::parse(
                "application/xhtml+xml; message=\"Hello, world?\""
            ))
        );
        assert_eq!(
            media_type
                .unwrap()
                .unwrap()
                .params()
                .next()
                .unwrap()
                .1
                .unquoted_str(),
            "Hello, world?"
        );

        assert_eq!(list.next(), None);
    }
}