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 /// - `&(Serialize + Display)` - 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 serde_json::to_string(data).unwrap_or_default()
19 }
20
21 /// Serializes data into XML format.
22 ///
23 /// # Arguments
24 ///
25 /// - `&(Serialize + Display)` - 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_default()
35 }
36
37 /// Formats data into plain text.
38 ///
39 /// # Arguments
40 ///
41 /// - `&(Serialize + Debug + Clone + Default + Display)` - 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 /// - `&(Serialize + Debug + Clone + Default)` - 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 /// - `&(Serialize + Display)` - 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_default()
87 }
88
89 /// Formats data as hexadecimal string.
90 ///
91 /// # Arguments
92 ///
93 /// - `&(Serialize + Debug + Clone + Default + Display)` - 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 /// - `&(Serialize + Debug + Clone + Default + Display)` - 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 /// - `AsRef<str>` - Content type
133 /// - `AsRef<str>` - Charset
134 ///
135 /// # Returns
136 ///
137 /// - `String` - Formatted string
138 pub fn format_content_type_with_charset<T, S>(content_type: T, charset: S) -> String
139 where
140 T: AsRef<str>,
141 S: AsRef<str>,
142 {
143 let content_type_ref: &str = content_type.as_ref();
144 let charset_ref: &str = charset.as_ref();
145 let mut result: String = String::with_capacity(
146 content_type_ref.len()
147 + SEMICOLON_SPACE.len()
148 + CHARSET_EQUAL.len()
149 + charset_ref.len(),
150 );
151 result.push_str(content_type_ref);
152 result.push_str(SEMICOLON_SPACE);
153 result.push_str(CHARSET_EQUAL);
154 result.push_str(charset_ref);
155 result
156 }
157
158 /// Formats content type with charset declaration.
159 ///
160 /// # Arguments
161 ///
162 /// - `AsRef<str>` - Content type
163 /// - `AsRef<str>` - Charset declaration
164 ///
165 /// # Returns
166 ///
167 /// - `String` - Formatted string
168 pub fn format_content_type_with_charset_declaration<T, S>(
169 content_type: T,
170 charset_with_key: S,
171 ) -> String
172 where
173 T: AsRef<str>,
174 S: AsRef<str>,
175 {
176 let content_type_ref: &str = content_type.as_ref();
177 let charset_with_key_ref: &str = charset_with_key.as_ref();
178 let mut result: String = String::with_capacity(
179 content_type_ref.len() + SEMICOLON_SPACE.len() + charset_with_key_ref.len(),
180 );
181 result.push_str(content_type_ref);
182 result.push_str(SEMICOLON_SPACE);
183 result.push_str(charset_with_key_ref);
184 result
185 }
186}
187
188/// Implements `FromStr` for `ContentType`.
189impl FromStr for ContentType {
190 type Err = ();
191
192 /// Parses string into ContentType.
193 ///
194 /// # Arguments
195 ///
196 /// - `&str` - String to parse
197 ///
198 /// # Returns
199 ///
200 /// - `Result<Self, Self::Err>` - Parse result
201 fn from_str(data: &str) -> Result<Self, Self::Err> {
202 match data.to_ascii_lowercase().as_str() {
203 APPLICATION_JSON => Ok(Self::ApplicationJson),
204 APPLICATION_XML => Ok(Self::ApplicationXml),
205 TEXT_PLAIN => Ok(Self::TextPlain),
206 TEXT_HTML => Ok(Self::TextHtml),
207 FORM_URLENCODED => Ok(Self::FormUrlEncoded),
208 _ => Ok(Self::Unknown),
209 }
210 }
211}