clientix_core/core/
headers.rs

1pub mod content_type {
2    use std::fmt::Display;
3    use reqwest::header::HeaderValue;
4
5    #[derive(Debug, Clone, Copy)]
6    pub enum ContentType {
7        ApplicationJson,
8        ApplicationXml,
9        ApplicationXWwwFormUrlEncoded,
10        TextHtml,
11        TextEventStream
12    }
13
14    impl TryFrom<String> for ContentType {
15        type Error = ();
16
17        fn try_from(value: String) -> Result<Self, Self::Error> {
18            match value.as_str() {
19                "application/json" => Ok(ContentType::ApplicationJson),
20                "application/xml" => Ok(ContentType::ApplicationXml),
21                "application/x-www-form-urlencoded" => Ok(ContentType::ApplicationXWwwFormUrlEncoded),
22                "text/html" => Ok(ContentType::TextHtml),
23                "text/event-bytes" => Ok(ContentType::TextEventStream),
24                _ => Err(())
25            }
26        }
27    }
28
29    impl Display for ContentType {
30        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
31            let str = match self {
32                ContentType::ApplicationJson => "application/json",
33                ContentType::ApplicationXml => "application/xml",
34                ContentType::ApplicationXWwwFormUrlEncoded => "application/x-www-form-urlencoded",
35                ContentType::TextHtml => "text/html",
36                ContentType::TextEventStream => "text/event-bytes"
37            };
38            
39            write!(f, "{}", str)
40        }
41    }
42
43    impl TryFrom<ContentType> for HeaderValue {
44
45        type Error = http::header::InvalidHeaderValue;
46
47        fn try_from(value: ContentType) -> Result<Self, Self::Error> {
48            let string: String = value.to_string();
49            HeaderValue::from_str(string.as_str())
50        }
51    }
52    
53}