lexa_framework/routing/collection.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
11// --------- //
12// Structure //
13// --------- //
14
15#[derive(Clone)]
16pub struct RouteCollection<UserState>(Vec<super::Route<UserState>>);
17
18// -------------- //
19// Implémentation //
20// -------------- //
21
22impl<US> RouteCollection<US> {
23 #[allow(clippy::new_without_default)]
24 pub fn new() -> Self {
25 Self(Default::default())
26 }
27}
28
29impl<US> RouteCollection<US> {
30 /// Retourne toutes les routes de la collection.
31 pub fn all(&self) -> impl Iterator<Item = &super::Route<US>> {
32 self.0.iter()
33 }
34}
35
36impl<US> RouteCollection<US> {
37 /// Ajoute un router à la collection de route.
38 #[allow(clippy::should_implement_trait)]
39 pub fn add(mut self, router: super::Router<US>) -> Self {
40 assert!(router.action.is_some());
41
42 self.0.push(super::Route {
43 action: router.action.unwrap().to_owned(),
44 fullpath: router.fullpath,
45 name: Some(router.name),
46 methods: router.methods,
47 });
48
49 self
50 }
51
52 /// Étend une collection de routes avec la collection de routes déjà en
53 /// place.
54 pub fn extend(&mut self, this: Self)
55 where
56 US: Clone,
57 {
58 self.0.extend(this.0.to_vec());
59 }
60}