lexa_framework/state/
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
11mod interface;
12
13pub use self::interface::StateInterface;
14
15// --------- //
16// Structure //
17// --------- //
18
19/// État de l'application.
20#[derive(Clone)]
21pub struct State<UserState> {
22	#[cfg(feature = "cookies")]
23	pub cookie_key: Option<tower_cookies::Key>,
24	#[cfg(feature = "database-postgres")]
25	pub database_postgres:
26		Option<crate::database::services::postgres::PostgresService>,
27	/// État utilisateur.
28	pub user_state: Option<UserState>,
29}
30
31// -------------- //
32// Implémentation // -> Interface
33// -------------- //
34
35#[cfg(feature = "cookies")]
36impl<US> axum::extract::FromRef<State<US>> for tower_cookies::Key {
37	fn from_ref(state: &State<US>) -> Self {
38		state.cookie_key.clone().expect(
39			"Veuillez définir une clé de cookie dans votre application. \
40			 `your_app.server.define_encrypt_key(&key)`.",
41		)
42	}
43}
44
45#[cfg(feature = "database-postgres")]
46impl<US> axum::extract::FromRef<State<US>>
47	for crate::database::services::postgres::PostgresService
48{
49	fn from_ref(state: &State<US>) -> Self {
50		state.database_postgres.clone().expect(
51			"Veuillez définir la connexion à la base de données dans votre \
52			 application. Utilisez: \
53			 `your_app.server.using_database_postgres_service(&database_url)`.",
54		)
55	}
56}
57
58impl axum::extract::FromRef<State<()>> for () {
59	fn from_ref(_: &State<()>) -> Self {}
60}