mco_http/header/shared/
encoding.rs

1use std::fmt;
2use std::str;
3
4pub use self::Encoding::{Chunked, Gzip, Deflate, Compress, Identity, EncodingExt};
5
6/// A value to represent an encoding used in `Transfer-Encoding`
7/// or `Accept-Encoding` header.
8#[derive(Clone, PartialEq, Debug)]
9pub enum Encoding {
10    /// The `chunked` encoding.
11    Chunked,
12    /// The `gzip` encoding.
13    Gzip,
14    /// The `deflate` encoding.
15    Deflate,
16    /// The `compress` encoding.
17    Compress,
18    /// The `identity` encoding.
19    Identity,
20    /// Some other encoding that is less common, can be any String.
21    EncodingExt(String)
22}
23
24impl fmt::Display for Encoding {
25    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
26        f.write_str(match *self {
27            Chunked => "chunked",
28            Gzip => "gzip",
29            Deflate => "deflate",
30            Compress => "compress",
31            Identity => "identity",
32            EncodingExt(ref s) => s.as_ref()
33        })
34    }
35}
36
37impl str::FromStr for Encoding {
38    type Err = crate::Error;
39    fn from_str(s: &str) -> crate::Result<Encoding> {
40        match s {
41            "chunked" => Ok(Chunked),
42            "deflate" => Ok(Deflate),
43            "gzip" => Ok(Gzip),
44            "compress" => Ok(Compress),
45            "identity" => Ok(Identity),
46            _ => Ok(EncodingExt(s.to_owned()))
47        }
48    }
49}