Debian Codegraph @

third_party/rust/headers/src/common/content_encoding.rs

raw
1 use self::sealed::AsCoding;
2 use util::FlatCsv;
3 use HeaderValue;
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;
30 /// use headers::ContentEncoding;
31 ///
32 /// let content_enc = ContentEncoding::gzip();
33 /// ```
34 #[derive(Clone, Debug)]
35 pub struct ContentEncoding(FlatCsv);
36
37 derive_header! {
38 ContentEncoding(_),
39 name: CONTENT_ENCODING
40 }
41
42 impl ContentEncoding {
43 /// A constructor to easily create a `Content-Encoding: gzip` header.
44 #[inline]
45 pub fn gzip() -> ContentEncoding {
46 ContentEncoding(HeaderValue::from_static("gzip").into())
47 }
48
49 /// Check if this header contains a given "coding".
50 ///
51 /// This can be used with these argument types:
52 ///
53 /// - `&str`
54 ///
55 /// # Example
56 ///
57 /// ```
58 /// # extern crate headers;
59 /// use headers::ContentEncoding;
60 ///
61 /// let content_enc = ContentEncoding::gzip();
62 ///
63 /// assert!(content_enc.contains("gzip"));
64 /// assert!(!content_enc.contains("br"));
65 /// ```
66 pub fn contains(&self, coding: impl AsCoding) -> bool {
67 let s = coding.as_coding();
68 self.0.iter().find(|&opt| opt == s).is_some()
69 }
70 }
71
72 mod sealed {
73 pub trait AsCoding: Sealed {}
74
75 pub trait Sealed {
76 fn as_coding(&self) -> &str;
77 }
78
79 impl<'a> AsCoding for &'a str {}
80
81 impl<'a> Sealed for &'a str {
82 fn as_coding(&self) -> &str {
83 *self
84 }
85 }
86 }