11use std:: convert:: TryFrom ;
2+
3+ use { ContentCoding , HeaderValue } ;
24use util:: { QualityValue , TryFromValues } ;
3- use HeaderValue ;
45
56/// `Accept-Encoding` header, defined in
67/// [RFC7231](https://tools.ietf.org/html/rfc7231#section-5.3.4)
@@ -68,55 +69,75 @@ impl AcceptEncoding {
6869 /// # Example
6970 ///
7071 /// ```
71- /// use headers::AcceptEncoding;
72+ /// use headers::{ AcceptEncoding, ContentCoding} ;
7273 ///
7374 /// let pairs = vec![("gzip", 1.0), ("deflate", 0.8)];
7475 /// let accept_enc = AcceptEncoding::from_quality_pairs(&mut pairs.into_iter()).unwrap();
7576 /// let mut encodings = accept_enc.sorted_encodings();
7677 ///
77- /// assert_eq!(accept_enc.prefered_encoding(), Some("gzip" ));
78+ /// assert_eq!(accept_enc.prefered_encoding(), Some(ContentCoding::GZIP ));
7879 /// ```
79- pub fn prefered_encoding ( & self ) -> Option < & str > {
80- self . 0 . iter ( ) . peekable ( ) . peek ( ) . map ( |s| * s )
80+ pub fn prefered_encoding ( & self ) -> Option < ContentCoding > {
81+ self . 0 . iter ( ) . peekable ( ) . peek ( ) . map ( |s| ContentCoding :: from_str ( * s ) )
8182 }
8283
83- /// Returns a quality sorted iterator of the accepted encodings
84+ /// Returns a quality sorted iterator of the `ContentCoding`
8485 ///
8586 /// # Example
8687 ///
8788 /// ```
88- /// use headers::{AcceptEncoding, HeaderValue};
89+ /// use headers::{AcceptEncoding, ContentCoding, HeaderValue};
8990 ///
9091 /// let val = HeaderValue::from_static("deflate, gzip;q=1.0, br;q=0.8");
9192 /// let accept_enc = AcceptEncoding(val.into());
9293 /// let mut encodings = accept_enc.sorted_encodings();
9394 ///
95+ /// assert_eq!(encodings.next(), Some(ContentCoding::DEFLATE));
96+ /// assert_eq!(encodings.next(), Some(ContentCoding::GZIP));
97+ /// assert_eq!(encodings.next(), Some(ContentCoding::BROTLI));
98+ /// assert_eq!(encodings.next(), None);
99+ /// ```
100+ pub fn sorted_encodings < ' a > ( & ' a self ) -> impl Iterator < Item = ContentCoding > + ' a {
101+ self . 0 . iter ( ) . map ( |s| ContentCoding :: from_str ( s) )
102+ }
103+
104+ /// Returns a quality sorted iterator of values
105+ ///
106+ /// # Example
107+ ///
108+ /// ```
109+ /// use headers::{AcceptEncoding, ContentCoding, HeaderValue};
110+ ///
111+ /// let val = HeaderValue::from_static("deflate, gzip;q=1.0, br;q=0.8");
112+ /// let accept_enc = AcceptEncoding(val.into());
113+ /// let mut encodings = accept_enc.sorted_values();
114+ ///
94115 /// assert_eq!(encodings.next(), Some("deflate"));
95116 /// assert_eq!(encodings.next(), Some("gzip"));
96117 /// assert_eq!(encodings.next(), Some("br"));
97118 /// assert_eq!(encodings.next(), None);
98119 /// ```
99- pub fn sorted_encodings ( & self ) -> impl Iterator < Item = & str > {
120+ pub fn sorted_values ( & self ) -> impl Iterator < Item = & str > {
100121 self . 0 . iter ( )
101122 }
102123}
103124
104125#[ cfg( test) ]
105126mod tests {
106127 use super :: * ;
107- use HeaderValue ;
128+ use { ContentCoding , HeaderValue } ;
108129
109130 #[ test]
110131 fn from_static ( ) {
111132 let val = HeaderValue :: from_static ( "deflate, gzip;q=1.0, br;q=0.9" ) ;
112133 let accept_enc = AcceptEncoding ( val. into ( ) ) ;
113134
114- assert_eq ! ( accept_enc. prefered_encoding( ) , Some ( "deflate" ) ) ;
135+ assert_eq ! ( accept_enc. prefered_encoding( ) , Some ( ContentCoding :: DEFLATE ) ) ;
115136
116137 let mut encodings = accept_enc. sorted_encodings ( ) ;
117- assert_eq ! ( encodings. next( ) , Some ( "deflate" ) ) ;
118- assert_eq ! ( encodings. next( ) , Some ( "gzip" ) ) ;
119- assert_eq ! ( encodings. next( ) , Some ( "br" ) ) ;
138+ assert_eq ! ( encodings. next( ) , Some ( ContentCoding :: DEFLATE ) ) ;
139+ assert_eq ! ( encodings. next( ) , Some ( ContentCoding :: GZIP ) ) ;
140+ assert_eq ! ( encodings. next( ) , Some ( ContentCoding :: BROTLI ) ) ;
120141 assert_eq ! ( encodings. next( ) , None ) ;
121142 }
122143
@@ -125,11 +146,11 @@ mod tests {
125146 let pairs = vec ! [ ( "gzip" , 1.0 ) , ( "br" , 0.9 ) ] ;
126147 let accept_enc = AcceptEncoding :: from_quality_pairs ( & mut pairs. into_iter ( ) ) . unwrap ( ) ;
127148
128- assert_eq ! ( accept_enc. prefered_encoding( ) , Some ( "gzip" ) ) ;
149+ assert_eq ! ( accept_enc. prefered_encoding( ) , Some ( ContentCoding :: GZIP ) ) ;
129150
130151 let mut encodings = accept_enc. sorted_encodings ( ) ;
131- assert_eq ! ( encodings. next( ) , Some ( "gzip" ) ) ;
132- assert_eq ! ( encodings. next( ) , Some ( "br" ) ) ;
152+ assert_eq ! ( encodings. next( ) , Some ( ContentCoding :: GZIP ) ) ;
153+ assert_eq ! ( encodings. next( ) , Some ( ContentCoding :: BROTLI ) ) ;
133154 assert_eq ! ( encodings. next( ) , None ) ;
134155 }
135156}
0 commit comments