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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
use std::{borrow::Cow, str::FromStr};
use bytes::Bytes;
use futures_util::StreamExt;
use http::{header::CONTENT_TYPE, HeaderValue};
use http_body_util::{BodyStream, Full};
use multer::Multipart;
use serde::Deserialize;
use crate::server::NgynContext;
/// Represents a transformer trait.
pub trait Transformer<'a> {
/// Transforms the given `NgynContext` and returns an instance of `Self`.
///
/// ### Arguments
///
/// * `cx` - The mutable reference to the `NgynContext`.
///
/// ### Examples
///
/// ```rust ignore
/// struct MyTransformer;
///
/// impl Transformer for MyTransformer {
/// fn transform(cx: &mut NgynContext) -> Self {
/// // Transformation logic goes here
/// MyTransformer {}
/// }
/// }
/// ```
#[must_use]
fn transform(cx: &'a mut NgynContext) -> Self
where
Self: Sized;
}
/// Represents a transducer struct.
pub struct Transducer;
impl<'a> Transducer {
/// Reduces the given `NgynContext` using the specified `Transformer` and returns an instance of `S`.
///
/// ### Arguments
///
/// * `cx` - The mutable reference to the `NgynContext`.
///
/// ### Examples
///
/// ```rust ignore
///
/// struct MyTransformer;
///
/// impl Transformer for MyTransformer {
/// fn transform(cx: &mut NgynContext) -> Option<Self> {
/// // Transformation logic goes here
/// MyTransformer
/// }
/// }
///
/// let mut cx = NgynContext::default();
///
/// let result: MyTransformer = Transducer::reduce(&mut cx);
/// ```
#[must_use]
pub fn reduce<S: Transformer<'a>>(cx: &'a mut NgynContext) -> S {
S::transform(cx)
}
}
/// Represents a parameter struct.
pub struct Param<'a> {
data: Vec<(&'a str, &'a str)>,
}
impl<'a> Param<'a> {
/// Retrieves the value associated with the specified `id` from the parameter data.
///
/// ### Arguments
///
/// * `id` - The identifier to search for.
///
/// ### Returns
///
/// * `Some(String)` - The value associated with the `id`, if found.
/// * `None` - If no value is associated with the `id`.
///
/// ### Examples
///
/// ```rust ignore
/// let param = Param {
/// data: vec![
/// ("id", "123"),
/// ("name", "John"),
/// ],
/// };
///
/// assert_eq!(param.get("id"), Some("123".to_string()));
/// assert_eq!(param.get("name"), Some("John".to_string()));
/// assert_eq!(param.get("age"), None);
/// ```
pub fn get<F: FromStr>(&self, id: &str) -> Option<F> {
for (key, value) in &self.data {
if *key == id {
if let Ok(value) = value.parse() {
return Some(value);
}
}
}
None
}
}
impl<'a: 'b, 'b> Transformer<'a> for Param<'b> {
/// Transforms the given `NgynContext` into a `Param` instance.
///
/// ### Arguments
///
/// * `cx` - The mutable reference to the `NgynContext`.
///
/// ### Returns
///
/// * `Param` - The transformed `Param` instance.
///
/// ### Examples
///
/// ```rust ignore
/// use crate::context::NgynContext;
///
/// let mut cx = NgynContext::default();
/// let param: Param = Param::transform(&mut cx);
/// ```
fn transform(cx: &'a mut NgynContext) -> Self {
let data: Vec<(&'a str, &'a str)> = cx
.params()
.expect("Extracting params should only be done in route handlers.")
.iter()
.collect();
Param { data }
}
}
/// Represents a query struct.
pub struct Query<'q> {
uri: &'q http::uri::Uri,
}
impl<'q> Query<'q> {
/// Retrieves the value associated with the specified `id` from the query parameters.
///
/// ### Arguments
///
/// * `id` - The identifier to search for.
///
/// ### Returns
///
/// * `Some(String)` - The value associated with the `id`, if found.
/// * `None` - If no value is associated with the `id`.
///
/// ### Examples
///
/// ```rust ignore
/// use hyper::Uri;
///
/// let uri: Uri = "https://example.com/?id=123&name=John".parse().unwrap();
/// let query = Query { uri: &uri };
///
/// assert_eq!(query.get("id"), Some("123".to_string()));
/// assert_eq!(query.get("name"), Some("John".to_string()));
/// assert_eq!(query.get("age"), None);
/// ```
pub fn get<F: FromStr>(&self, id: &str) -> Option<F> {
let query = self.uri.query().unwrap_or("");
let query = url::form_urlencoded::parse(query.as_bytes());
for (key, value) in query {
if key == id {
if let Ok(value) = value.parse() {
return Some(value);
}
}
}
None
}
}
impl<'a: 'q, 'q> Transformer<'a> for Query<'q> {
/// Transforms the given `NgynContext` into a `Query` instance.
///
/// ### Arguments
///
/// * `cx` - The mutable reference to the `NgynContext`.
///
/// ### Returns
///
/// * `Query` - The transformed `Query` instance.
///
/// ### Examples
///
/// ```rust ignore
/// use crate::context::NgynContext;
/// use hyper::Uri;
///
/// let mut cx = NgynContext::default();
///
/// let uri: Uri = "https://example.com/?id=123&name=John".parse().unwrap();
/// cx.request.set_uri(uri);
///
/// let query: Query = Query::transform(&mut cx);
/// ```
fn transform(cx: &'a mut NgynContext) -> Self {
Query {
uri: cx.request().uri(),
}
}
}
/// Represents a data transfer object struct.
pub struct Body<'b> {
content_type: Option<&'b HeaderValue>,
data: &'b Vec<u8>,
}
impl<'b> Body<'b> {
/// Parses the data into the specified type using serde deserialization.
/// Once read, the body data is consumed and cannot be read again.
///
/// ### Arguments
///
/// * `S` - The type to deserialize the data into.
///
/// ### Returns
///
/// * `Result<S, serde_json::Error>` - The deserialized result, if successful.
///
/// ### Examples
///
/// ```rust ignore
/// use serde::Deserialize;
///
/// let body = Body {
/// data: r#"{"name": "John", "age": 30}"#.to_string().into_bytes(),
/// };
///
/// #[derive(Deserialize)]
/// struct Person {
/// name: String,
/// age: u32,
/// }
///
/// let result: Result<Person, serde_json::Error> = body.json();
/// ```
pub fn json<S: for<'a> Deserialize<'a>>(self) -> Result<S, serde_json::Error> {
serde_json::from_str(&self.text())
}
/// Reads the body data as a string.
/// Once read, the body data is consumed and cannot be read again.
///
/// ### Returns
///
/// * `String` - The body data as a string.
///
/// ### Examples
///
/// ```rust ignore
/// let body = Body {
/// data: r#"{"name": "John", "age": 30}"#.to_string().into_bytes(),
/// };
///
/// assert_eq!(body.text(), r#"{"name": "John", "age": 30}"#);
/// ```
pub fn text(self) -> Cow<'b, str> {
String::from_utf8_lossy(self.data)
}
/// Parses the data into a `multipart/form-data` stream.
/// Once read, the body data is consumed and cannot be read again.
///
/// ### Returns
///
/// * `Multipart<'static>` - The body data as a `multipart/form-data` stream.
///
/// ### Examples
///
/// ```rust ignore
/// let body = Body {
/// data: r#"------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name="file"; filename="example.txt"\r\nContent-Type: text/plain\r\n\r\nHello World\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--\r\n"#.to_string().into_bytes(),
/// };
///
/// let stream = body.form_data();
/// ```
pub fn form_data(self) -> Result<Multipart<'b>, multer::Error> {
if let Some(content_type) = self.content_type {
let boundary = multer::parse_boundary(
content_type
.to_str()
.expect("Content Type header contains invalid ASCII value"),
)?;
let body: Full<Bytes> = Full::new(Bytes::from(self.data.to_owned()));
let stream = BodyStream::new(body).filter_map(|result| async move {
result.map(|frame| frame.into_data().ok()).transpose()
});
Ok(Multipart::new(stream, boundary))
} else {
Err(multer::Error::NoBoundary)
}
}
}
impl<'a: 'b, 'b> Transformer<'a> for Body<'b> {
/// Transforms the given `NgynContext` into a `Body` instance.
///
/// ### Arguments
///
/// * `cx` - The mutable reference to the `NgynContext`.
///
/// ### Returns
///
/// * `Body` - The transformed `Body` instance.
///
/// ### Examples
///
/// ```rust ignore
/// use crate::context::NgynContext;
///
/// let mut cx = NgynContext::default();
///
/// let dto: Body = Body::transform(&mut cx);
/// ```
fn transform(cx: &'a mut NgynContext) -> Self {
let data = cx.request().body();
let content_type = cx.request().headers().get(CONTENT_TYPE);
Body { data, content_type }
}
}