lexa_framework/http/response/
mod.rs

1// ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
2// ┃ Copyright: (c) 2023, Mike 'PhiSyX' S. (https://github.com/PhiSyX)         ┃
3// ┃ SPDX-License-Identifier: MPL-2.0                                          ┃
4// ┃ ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ┃
5// ┃                                                                           ┃
6// ┃  This Source Code Form is subject to the terms of the Mozilla Public      ┃
7// ┃  License, v. 2.0. If a copy of the MPL was not distributed with this      ┃
8// ┃  file, You can obtain one at https://mozilla.org/MPL/2.0/.                ┃
9// ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
10
11use std::sync::Arc;
12
13pub use axum::response::*;
14
15// --------- //
16// Structure //
17// --------- //
18
19pub struct HttpResponse<T> {
20	pub(crate) context: Arc<T>,
21}
22
23// -------------- //
24// Implémentation //
25// -------------- //
26
27impl<T> HttpResponse<T> {
28	/// Rend l'HTML d'une vue.
29	#[inline]
30	pub fn html<R>(
31		&self,
32		html: impl Into<axum::response::Html<R>>,
33	) -> axum::response::Html<R>
34	where
35		R: Into<axum::body::Full<hyper::body::Bytes>>,
36	{
37		html.into()
38	}
39
40	/// Retourne un JSON en réponse.
41	//
42	// BUG(phisyx): provoque une erreur avec la commande `cargo doc`.
43	// ISSUE(rust): "rustdoc RPITIT ICE:
44	//               compiler\rustc_middle\src\ty\generic_args.rs:900:9: type
45	//               parameter impl ToString/#1 (impl ToString/1) out of range
46	//               when substituting, args=[MyStruct] "
47	//               https://github.com/rust-lang/rust/issues/113929
48	// ISSUE(rust): Closed (Fixed by https://github.com/rust-lang/rust/pull/113956).
49	#[inline]
50	pub fn json<D>(&self, data: D) -> axum::response::Json<D>
51	where
52		D: serde::Serialize,
53	{
54		// axum::response::Json(serde_json::json!(data))
55		data.into()
56	}
57
58	/// Redirige le client vers une URL (Code HTTP 303).
59	#[inline]
60	pub fn redirect_to(&self, uri: impl ToString) -> axum::response::Redirect
61	where
62		Self: Sized,
63	{
64		axum::response::Redirect::to(uri.to_string().as_ref())
65	}
66
67	/// Redirige le client vers une URL de manière permanente (Code HTTP 308).
68	#[inline]
69	pub fn redirect_permanent(
70		&self,
71		uri: impl ToString,
72	) -> axum::response::Redirect
73	where
74		Self: Sized,
75	{
76		axum::response::Redirect::permanent(uri.to_string().as_ref())
77	}
78
79	/// Redirige le client vers une URL de manière temporaire (Code HTTP 307).
80	#[inline]
81	pub fn redirect_temporary(
82		&self,
83		uri: impl ToString,
84	) -> axum::response::Redirect
85	where
86		Self: Sized,
87	{
88		axum::response::Redirect::temporary(uri.to_string().as_ref())
89	}
90}