http_type/content_type/impl.rs
1use crate::*;
2
3impl ContentType {
4 /// Handles the `application/json` Content-Type by serializing the provided data
5 /// into a JSON string.
6 ///
7 /// # Type Parameters
8 /// - `T`: The type of the data to be serialized, which must implement `Serialize`.
9 ///
10 /// # Parameters
11 /// - `data`: The data to be serialized into JSON.
12 ///
13 /// # Returns
14 /// A string containing the serialized JSON representation of the provided data.
15 /// If serialization fails, it returns an empty JSON object (`{}`).
16 #[inline]
17 fn get_application_json<T: Serialize + Display>(data: &T) -> String {
18 serde_json::to_string(data).unwrap_or_else(|_| String::from("{}"))
19 }
20
21 /// Handles the `application/xml` Content-Type by serializing the provided data
22 /// into an XML string.
23 ///
24 /// # Type Parameters
25 /// - `T`: The type of the data to be serialized, which must implement `Serialize`.
26 ///
27 /// # Parameters
28 /// - `data`: The data to be serialized into XML.
29 ///
30 /// # Returns
31 /// A string containing the serialized XML representation of the provided data.
32 /// If serialization fails, it returns an empty XML root element (`<root></root>`).
33 #[inline]
34 fn get_application_xml<T: Serialize + Display>(data: &T) -> String {
35 serde_xml_rs::to_string(data).unwrap_or_else(|_| String::from("<root></root>"))
36 }
37
38 /// Handles the `text/plain` Content-Type by formatting the provided data
39 /// into a plain text string.
40 ///
41 /// # Type Parameters
42 /// - `T`: The type of the data to be formatted, which must implement `Serialize`, `Debug`, `Clone`, and `Default`.
43 ///
44 /// # Parameters
45 /// - `data`: The data to be formatted into plain text.
46 ///
47 /// # Returns
48 /// A plain text string representing the provided data, formatted with the `Debug` trait.
49 #[inline]
50 fn get_text_plain<T: Serialize + Debug + Clone + Default + Display>(data: &T) -> String {
51 data.to_string()
52 }
53
54 /// Handles the `text/html` Content-Type by formatting the provided data
55 /// into an HTML string, typically inside a simple table.
56 ///
57 /// # Type Parameters
58 /// - `T`: The type of the data to be formatted, which must implement `Serialize`, `Debug`, `Clone`, and `Default`.
59 ///
60 /// # Parameters
61 /// - `data`: The data to be formatted into HTML.
62 ///
63 /// # Returns
64 /// A string containing the HTML representation of the provided data, inside a table row.
65 #[inline]
66 fn get_text_html<T: Serialize + Debug + Clone + Default>(data: &T) -> String {
67 let mut html: String = String::from("<table>");
68 html.push_str(&format!("<tr><td>{:?}</td></tr>", data));
69 html.push_str("</table>");
70 html
71 }
72
73 /// Handles the `application/x-www-form-urlencoded` Content-Type by serializing
74 /// the provided data into a URL-encoded string.
75 ///
76 /// # Type Parameters
77 /// - `T`: The type of the data to be serialized, which must implement `Serialize`.
78 ///
79 /// # Parameters
80 /// - `data`: The data to be serialized into URL-encoded format.
81 ///
82 /// # Returns
83 /// A string containing the URL-encoded representation of the provided data.
84 /// If serialization fails, it returns an empty string.
85 #[inline]
86 fn get_form_url_encoded<T: Serialize + Display>(data: &T) -> String {
87 serde_urlencoded::to_string(data).unwrap_or_else(|_| String::from(""))
88 }
89
90 /// Handles binary data when the `Content-Type` is unknown by formatting the
91 /// provided data as a hexadecimal string.
92 ///
93 /// # Type Parameters
94 /// - `T`: The type of the data to be formatted, which must implement `Serialize`, `Debug`, `Clone`, and `Default`.
95 ///
96 /// # Parameters
97 /// - `data`: The data to be formatted into binary representation.
98 ///
99 /// # Returns
100 /// A string containing the hexadecimal encoding of the provided data.
101 #[inline]
102 fn get_binary<T: Serialize + Debug + Clone + Default + Display>(data: &T) -> String {
103 hex::encode(data.to_string())
104 }
105
106 /// Public interface for getting a formatted body string based on the `ContentType`.
107 ///
108 /// This method routes the data to the appropriate handler method based on the
109 /// `ContentType`, formatting the body accordingly.
110 ///
111 /// # Type Parameters
112 /// - `T`: The type of the data to be formatted, which must implement `Serialize`, `Debug`, `Clone`, and `Default`.
113 ///
114 /// # Parameters
115 /// - `data`: The data to be formatted into the body string.
116 ///
117 /// # Returns
118 /// A string containing the formatted body based on the content type, such as JSON, XML, plain text, HTML, etc.
119 #[inline]
120 pub fn get_body_string<T: Serialize + Debug + Clone + Default + Display>(
121 &self,
122 data: &T,
123 ) -> String {
124 match self {
125 Self::ApplicationJson => Self::get_application_json(data),
126 Self::ApplicationXml => Self::get_application_xml(data),
127 Self::TextPlain => Self::get_text_plain(data),
128 Self::TextHtml => Self::get_text_html(data),
129 Self::FormUrlEncoded => Self::get_form_url_encoded(data),
130 Self::Unknown => Self::get_binary(data),
131 }
132 }
133}
134
135impl FromStr for ContentType {
136 type Err = ();
137
138 #[inline]
139 fn from_str(data: &str) -> Result<Self, Self::Err> {
140 match data {
141 _data if _data.eq_ignore_ascii_case(APPLICATION_JSON) => Ok(Self::ApplicationJson),
142 _data if _data.eq_ignore_ascii_case(APPLICATION_XML) => Ok(Self::ApplicationXml),
143 _data if _data.eq_ignore_ascii_case(TEXT_PLAIN) => Ok(Self::TextPlain),
144 _data if _data.eq_ignore_ascii_case(TEXT_HTML) => Ok(Self::TextHtml),
145 _data if _data.eq_ignore_ascii_case(FORM_URLENCODED) => Ok(Self::FormUrlEncoded),
146 _ => Ok(Self::Unknown),
147 }
148 }
149}
150
151impl Default for ContentType {
152 #[inline]
153 fn default() -> Self {
154 Self::Unknown
155 }
156}