1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
//! # Serialization/Deserialization Module
//!
//! This module provides traits and utilities for handling HTTP request and response body
//! serialization and deserialization. It enables automatic conversion between Rust types
//! and various data formats like JSON, form data, and other content types.
//!
//! ## Key Components
//!
//! - [`RequestBody`]: Trait for serializing request bodies into bytes
//! - [`ResponseBody`]: Trait for deserializing response bodies from bytes
//! - Content type registration and management
//! - Format-agnostic serialization interface
//!
//! ## Features
//!
//! - **Format Agnostic**: Support for multiple serialization formats
//! - **Content Type Management**: Automatic content-type header handling
//! - **Type Safety**: Compile-time guarantees for serializable types
//! - **Error Handling**: Comprehensive error reporting for serialization failures
//! - **Extensibility**: Easy to add new serialization formats
//!
//! ## Usage
//!
//! ### Implementing Custom Request Body Serializer
//!
//! ```rust, ignore
//! use deboa::client::serde::RequestBody;
//! use deboa::{request::DeboaRequestBuilder, Result};
//! use serde::Serialize;
//!
//! struct JsonBody;
//!
//! impl RequestBody for JsonBody {
//! fn register_content_type(&self, request: &mut DeboaRequestBuilder) {
//! request.set_content_type("application/json");
//! }
//!
//! fn serialize<T: Serialize>(&self, value: T) -> Result<Vec<u8>> {
//! serde_json::to_vec(&value).map_err(Into::into)
//! }
//! }
//! ```
//!
//! ### Implementing Custom Response Body Deserializer
//!
//! ```rust, ignore
//! use deboa::client::serde::ResponseBody;
//! use deboa::Result;
//! use serde::Deserialize;
//!
//! struct JsonBody;
//!
//! impl ResponseBody for JsonBody {
//! fn deserialize<T: for<'de> Deserialize<'de>>(&self, bytes: &[u8]) -> Result<T> {
//! serde_json::from_slice(bytes).map_err(Into::into)
//! }
//! }
//! ```
//!
//! ## Integration with HTTP Client
//!
//! The serialization traits are used internally by the Deboa client to handle:
//! - Automatic JSON serialization for request bodies
//! - JSON deserialization for response bodies
//! - Form data encoding and decoding
//! - Custom content type handling
use crateResult;
use ;
/// Trait that represents request body serialization capabilities.
///
/// This trait defines the interface for converting Rust types into HTTP request bodies.
/// Implementations handle different serialization formats like JSON, XML, form data, etc.
///
/// # Requirements
///
/// Implementations must:
/// - Register the appropriate content type on the request
/// - Serialize the given value into a byte vector
/// - Handle serialization errors appropriately
///
/// # Examples
///
/// ## JSON Serializer
///
/// ```rust, ignore
/// use deboa::client::serde::RequestBody;
/// use deboa::{request::DeboaRequest, Result};
/// use serde::Serialize;
///
/// struct JsonSerializer;
///
/// impl RequestBody for JsonSerializer {
/// fn mime_type(&self) -> Result<HeaderValue> {
/// Ok(HeaderValue::from_str("application/json").unwrap())
/// }
///
/// fn serialize<T: Serialize>(&self, value: T) -> Result<Vec<u8>> {
/// serde_json::to_vec(&value)
/// .map_err(|e| DeboaError::SerializationError(e.to_string()))
/// }
/// }
/// ```
///
/// ## Form URL-encoded Serializer
///
/// ```rust, ignore
/// use deboa::client::serde::RequestBody;
/// use deboa::{request::DeboaRequest, Result};
/// use serde::Serialize;
///
/// struct FormSerializer;
///
/// impl RequestBody for FormSerializer {
/// fn mime_type(&self) -> Result<HeaderValue> {
/// Ok(HeaderValue::from_str("application/x-www-form-urlencoded").unwrap())
/// }
///
/// fn serialize<T: Serialize>(&self, value: T) -> Result<Vec<u8>> {
/// serde_urlencoded::to_string(&value)
/// .map(|s| s.into_bytes())
/// .map_err(|e| DeboaError::SerializationError(e.to_string()))
/// }
/// }
/// ```
/// Trait that represents response body deserialization capabilities.
///
/// This trait defines the interface for converting HTTP response bodies
/// from bytes into Rust types. Implementations handle different deserialization
/// formats like JSON, XML, text, etc.
///
/// # Requirements
///
/// Implementations must:
/// - Parse bytes into the target Rust type
/// - Handle deserialization errors appropriately
/// - Support the target data format
///
/// # Examples
///
/// ## JSON Deserializer
///
/// ```rust, ignore
/// use deboa::client::serde::ResponseBody;
/// use deboa::Result;
/// use serde::Deserialize;
///
/// struct JsonBody;
///
/// impl ResponseBody for JsonBody {
/// fn deserialize<T: for<'de> Deserialize<'de>>(&self, bytes: &[u8]) -> Result<T> {
/// serde_json::from_slice(bytes)
/// .map_err(|e| DeboaError::DeserializationError(e.to_string()))
/// }
/// }
/// ```
///
/// ## Text Deserializer
///
/// ```rust, ignore
/// use deboa::client::serde::ResponseBody;
/// use deboa::Result;
/// use std::str;
///
/// struct TextBody;
///
/// impl ResponseBody for TextBody {
/// fn deserialize<T: for<'de> Deserialize<'de>>(&self, bytes: &[u8]) -> Result<T> {
/// let text = str::from_utf8(bytes)
/// .map_err(|e| DeboaError::DeserializationError(e.to_string()))?;
///
/// // For simple string types
/// if std::any::TypeId::of::<T>() == std::any::TypeId::of::<String>() {
/// // SAFETY: We've verified the type
/// Ok(unsafe { std::mem::transmute_copy(&text) })
/// } else {
/// Err(DeboaError::DeserializationError("Unsupported type".to_string()))
/// }
/// }
/// }
/// ```