http_type/content_type/
impl.rs1use crate::*;
2
3impl ContentType {
5 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 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 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 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 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 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 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 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 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
188impl FromStr for ContentType {
190 type Err = ();
191
192 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}