headers_ext/common/content_encoding.rs
1use util::FlatCsv;
2use {HeaderValue};
3use self::sealed::AsCoding;
4
5/// `Content-Encoding` header, defined in
6/// [RFC7231](http://tools.ietf.org/html/rfc7231#section-3.1.2.2)
7///
8/// The `Content-Encoding` header field indicates what content codings
9/// have been applied to the representation, beyond those inherent in the
10/// media type, and thus what decoding mechanisms have to be applied in
11/// order to obtain data in the media type referenced by the Content-Type
12/// header field. Content-Encoding is primarily used to allow a
13/// representation's data to be compressed without losing the identity of
14/// its underlying media type.
15///
16/// # ABNF
17///
18/// ```text
19/// Content-Encoding = 1#content-coding
20/// ```
21///
22/// # Example values
23///
24/// * `gzip`
25///
26/// # Examples
27///
28/// ```
29/// # extern crate headers_ext as headers;
30/// use headers::ContentEncoding;
31///
32/// let content_enc = ContentEncoding::gzip();
33/// ```
34#[derive(Clone, Debug, Header)]
35pub struct ContentEncoding(FlatCsv);
36
37impl ContentEncoding {
38 /// A constructor to easily create a `Content-Encoding: gzip` header.
39 #[inline]
40 pub fn gzip() -> ContentEncoding {
41 ContentEncoding(HeaderValue::from_static("gzip").into())
42 }
43
44 /// Check if this header contains a given "coding".
45 ///
46 /// This can be used with these argument types:
47 ///
48 /// - `&str`
49 ///
50 /// # Example
51 ///
52 /// ```
53 /// # extern crate headers_ext as headers;
54 /// use headers::ContentEncoding;
55 ///
56 /// let content_enc = ContentEncoding::gzip();
57 ///
58 /// assert!(content_enc.contains("gzip"));
59 /// assert!(!content_enc.contains("br"));
60 /// ```
61 pub fn contains(&self, coding: impl AsCoding) -> bool {
62 let s = coding.as_coding();
63 self
64 .0
65 .iter()
66 .find(|&opt| opt == s)
67 .is_some()
68 }
69}
70
71mod sealed {
72 pub trait AsCoding: Sealed {}
73
74 pub trait Sealed {
75 fn as_coding(&self) -> &str;
76 }
77
78 impl<'a> AsCoding for &'a str {}
79
80 impl<'a> Sealed for &'a str {
81 fn as_coding(&self) -> &str {
82 *self
83 }
84 }
85}