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