lexa_framework/macros_rules/
routes.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#[macro_export]
12macro_rules! routes {
13	(
14		pub enum $enum_route:ident<$l:lifetime> {
15			$(
16				#[route(url = $url:literal)]
17				$variant:ident
18				$({
19					$($field:ident : $type:ty),*
20				})?
21			),*,
22		}
23	) => {
24		pub enum $enum_route<$l> {
25			#[non_exhaustive] PhantomData(std::marker::PhantomData<&$l ()>),
26			$(
27				$variant $({
28					$($field : $type),*
29				})?,
30			)*
31		}
32
33		impl std::fmt::Display for $enum_route<'_> {
34			fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
35				let route = match self {
36					Self::PhantomData(_) => unimplemented!(),
37					$(
38						| Self::$variant $({ $($field),* })? => format!($url),
39					)*
40				};
41				write!(f, "{route}")
42			}
43		}
44
45		impl std::fmt::Debug for $enum_route<'_> {
46			fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
47				match self {
48					Self::PhantomData(_) => unimplemented!(),
49					$(
50						#[allow(dead_code)]
51						| Self::$variant $({ $($field),* })? => write!(f, stringify!($variant)),
52					)*
53				}
54			}
55		}
56	};
57}