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
use HeaderValue;
// Derives an enum to represent content codings and some helpful impls
macro_rules! define_content_coding {
($($coding:ident; $str:expr,)+) => {
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
/// Values that are used with headers like [`Content-Encoding`](self::ContentEncoding) or
/// [`Accept-Encoding`](self::AcceptEncoding)
///
/// [RFC7231](https://www.iana.org/assignments/http-parameters/http-parameters.xhtml)
pub enum ContentCoding {
$(
#[doc = $str]
$coding,
)+
}
impl ContentCoding {
/// Returns a `&'static str` for a `ContentCoding`
///
/// # Example
///
/// ```
/// use headers::ContentCoding;
///
/// let coding = ContentCoding::BROTLI;
/// assert_eq!(coding.to_static(), "br");
/// ```
#[inline]
pub fn to_static(&self) -> &'static str {
match *self {
$(ContentCoding::$coding => $str,)+
}
}
/// Given a `&str` returns a `ContentCoding`
///
/// Note this will never fail, in the case of `&str` being an invalid content coding,
/// will return `ContentCoding::IDENTITY` because `'identity'` is generally always an
/// accepted coding.
///
/// # Example
///
/// ```
/// use headers::ContentCoding;
///
/// let invalid = ContentCoding::from_str("not a valid coding");
/// assert_eq!(invalid, ContentCoding::IDENTITY);
///
/// let valid = ContentCoding::from_str("gzip");
/// assert_eq!(valid, ContentCoding::GZIP);
/// ```
#[inline]
pub fn from_str(s: &str) -> Self {
ContentCoding::try_from_str(s).unwrap_or_else(|_| ContentCoding::IDENTITY)
}
/// Given a `&str` will try to return a `ContentCoding`
///
/// Different from `ContentCoding::from_str(&str)`, if `&str` is an invalid content
/// coding, it will return `Err(())`
///
/// # Example
///
/// ```
/// use headers::ContentCoding;
///
/// let invalid = ContentCoding::try_from_str("not a valid coding");
/// assert!(invalid.is_err());
///
/// let valid = ContentCoding::try_from_str("gzip");
/// assert_eq!(valid.unwrap(), ContentCoding::GZIP);
/// ```
#[inline]
pub fn try_from_str(s: &str) -> Result<Self, ()> {
match s {
$(
stringify!($coding)
| $str => Ok(ContentCoding::$coding),
)+
_ => Err(())
}
}
}
impl std::string::ToString for ContentCoding {
#[inline]
fn to_string(&self) -> String {
match *self {
$(ContentCoding::$coding => $str.to_string(),)+
}
}
}
impl From<ContentCoding> for HeaderValue {
fn from(coding: ContentCoding) -> HeaderValue {
match coding {
$(ContentCoding::$coding => HeaderValue::from_static($str),)+
}
}
}
}
}
define_content_coding! {
BROTLI; "br",
COMPRESS; "compress",
DEFLATE; "deflate",
GZIP; "gzip",
IDENTITY; "identity",
ZSTD; "zstd",
}
#[cfg(test)]
mod tests {
use super::ContentCoding;
#[test]
fn to_static() {
assert_eq!(ContentCoding::GZIP.to_static(), "gzip");
}
#[test]
fn to_string() {
assert_eq!(ContentCoding::DEFLATE.to_string(), "deflate".to_string());
}
#[test]
fn from_str() {
assert_eq!(ContentCoding::from_str("br"), ContentCoding::BROTLI);
assert_eq!(ContentCoding::from_str("GZIP"), ContentCoding::GZIP);
assert_eq!(ContentCoding::from_str("zstd"), ContentCoding::ZSTD);
assert_eq!(
ContentCoding::from_str("blah blah"),
ContentCoding::IDENTITY
);
}
#[test]
fn try_from_str() {
assert_eq!(ContentCoding::try_from_str("br"), Ok(ContentCoding::BROTLI));
assert_eq!(ContentCoding::try_from_str("zstd"), Ok(ContentCoding::ZSTD));
assert_eq!(ContentCoding::try_from_str("blah blah"), Err(()));
}
}