scrappy_http/header/shared/
encoding.rs

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