http_type/content_type/
impl.rs

1use crate::*;
2
3/// Implementation for `ContentType` enum.
4impl ContentType {
5    /// Serializes data into JSON format.
6    ///
7    /// # Arguments
8    ///
9    /// - `&T` - Data to serialize
10    ///
11    /// # Returns
12    ///
13    /// - `String` - Serialized JSON string
14    fn get_application_json<T>(data: &T) -> String
15    where
16        T: Serialize + Display,
17    {
18        json_to_string(data).unwrap_or_else(|_| "{}".to_string())
19    }
20
21    /// Serializes data into XML format.
22    ///
23    /// # Arguments
24    ///
25    /// - `&T` - Data to serialize
26    ///
27    /// # Returns
28    ///
29    /// - `String` - Serialized XML string
30    fn get_application_xml<T>(data: &T) -> String
31    where
32        T: Serialize + Display,
33    {
34        serde_xml_rs::to_string(data).unwrap_or_else(|_| "<root></root>".to_string())
35    }
36
37    /// Formats data into plain text.
38    ///
39    /// # Arguments
40    ///
41    /// - `&T` - Data to format
42    ///
43    /// # Returns
44    ///
45    /// - `String` - Formatted plain text
46    fn get_text_plain<T>(data: &T) -> String
47    where
48        T: Serialize + Debug + Clone + Default + Display,
49    {
50        data.to_string()
51    }
52
53    /// Formats data into HTML.
54    ///
55    /// # Arguments
56    ///
57    /// - `&T` - Data to format
58    ///
59    /// # Returns
60    ///
61    /// - `String` - Formatted HTML
62    fn get_text_html<T>(data: &T) -> String
63    where
64        T: Serialize + Debug + Clone + Default,
65    {
66        let mut html: String = String::with_capacity(64);
67        html.push_str("<table><tr><td>");
68        html.push_str(&format!("{:?}", data));
69        html.push_str("</td></tr></table>");
70        html
71    }
72
73    /// Serializes data into URL-encoded format.
74    ///
75    /// # Arguments
76    ///
77    /// - `&T` - Data to serialize
78    ///
79    /// # Returns
80    ///
81    /// - `String` - URL-encoded string
82    fn get_form_url_encoded<T>(data: &T) -> String
83    where
84        T: Serialize + Display,
85    {
86        serde_urlencoded::to_string(data).unwrap_or_else(|_| String::new())
87    }
88
89    /// Formats data as hexadecimal string.
90    ///
91    /// # Arguments
92    ///
93    /// - `&T` - Data to format
94    ///
95    /// # Returns
96    ///
97    /// - `String` - Hexadecimal encoded string
98    fn get_binary<T>(data: &T) -> String
99    where
100        T: Serialize + Debug + Clone + Default + Display,
101    {
102        hex::encode(data.to_string())
103    }
104
105    /// Gets formatted body string for content type.
106    ///
107    /// # Arguments
108    ///
109    /// - `&T` - Data to format
110    ///
111    /// # Returns
112    ///
113    /// - `String` - Formatted body string
114    pub fn get_body_string<T>(&self, data: &T) -> String
115    where
116        T: Serialize + Debug + Clone + Default + Display,
117    {
118        match self {
119            Self::ApplicationJson => Self::get_application_json(data),
120            Self::ApplicationXml => Self::get_application_xml(data),
121            Self::TextPlain => Self::get_text_plain(data),
122            Self::TextHtml => Self::get_text_html(data),
123            Self::FormUrlEncoded => Self::get_form_url_encoded(data),
124            Self::Unknown => Self::get_binary(data),
125        }
126    }
127
128    /// Formats content type with charset.
129    ///
130    /// # Arguments
131    ///
132    /// - `&str` - Content type
133    /// - `&str` - Charset
134    ///
135    /// # Returns
136    ///
137    /// - `String` - Formatted string
138    pub fn format_content_type_with_charset(content_type: &str, charset: &str) -> String {
139        let mut result: String = String::with_capacity(
140            content_type.len() + SEMICOLON_SPACE.len() + CHARSET_EQUAL.len() + charset.len(),
141        );
142        result.push_str(content_type);
143        result.push_str(SEMICOLON_SPACE);
144        result.push_str(CHARSET_EQUAL);
145        result.push_str(charset);
146        result
147    }
148
149    /// Formats content type with charset declaration.
150    ///
151    /// # Arguments
152    ///
153    /// - `&str` - Content type
154    /// - `&str` - Charset declaration
155    ///
156    /// # Returns
157    ///
158    /// - `String` - Formatted string
159    pub fn format_content_type_with_charset_declaration(
160        content_type: &str,
161        charset_with_key: &str,
162    ) -> String {
163        let mut result: String = String::with_capacity(
164            content_type.len() + SEMICOLON_SPACE.len() + charset_with_key.len(),
165        );
166        result.push_str(content_type);
167        result.push_str(SEMICOLON_SPACE);
168        result.push_str(charset_with_key);
169        result
170    }
171}
172
173/// Implements `FromStr` for `ContentType`.
174impl FromStr for ContentType {
175    type Err = ();
176
177    /// Parses string into ContentType.
178    ///
179    /// # Arguments
180    ///
181    /// - `&str` - String to parse
182    ///
183    /// # Returns
184    ///
185    /// - `Result<Self, Self::Err>` - Parse result
186    fn from_str(data: &str) -> Result<Self, Self::Err> {
187        match data.to_ascii_lowercase().as_str() {
188            APPLICATION_JSON => Ok(Self::ApplicationJson),
189            APPLICATION_XML => Ok(Self::ApplicationXml),
190            TEXT_PLAIN => Ok(Self::TextPlain),
191            TEXT_HTML => Ok(Self::TextHtml),
192            FORM_URLENCODED => Ok(Self::FormUrlEncoded),
193            _ => Ok(Self::Unknown),
194        }
195    }
196}