devela/code/util/
ident.rs

1// devela::code::util::ident
2//
3//! identifier related macro functionality
4//
5
6/// Defines a constant for every given identifier with a value of its index in the list.
7/// # Examples
8/// ```
9/// # use devela::ident_const_index;
10/// ident_const_index![pub, 3; first, second, third]; // with commas
11/// assert![0 == first && 1 == second && 2 == third];
12///
13/// ident_const_index![pub(crate), 2; fourth fifth]; // without commas
14/// assert![0 == fourth && 1 == fifth];
15/// ```
16// USEDBY: enumset macro
17#[macro_export]
18#[cfg_attr(cargo_primary_package, doc(hidden))]
19macro_rules! ident_const_index {
20    ( // without commas:
21        // $vis: the visibility of the constants (pub, pub(super), …).
22        // $total: the total number of identifiers.
23        // $ident, $($rest),*: the list of identifiers.
24        $vis:vis, $total:expr; $ident:ident $($rest:ident)*
25    ) => { $crate::paste! {
26        #[allow(non_upper_case_globals)]
27        $vis const $ident: usize = $total - $crate::ident_total!($($rest)*) - 1;
28        $( $crate::ident_const_index!($vis, $total; $rest); )*
29    }};
30    ( // with commas:
31        // $vis: the visibility of the constants (pub, pub(super), …).
32        // $total: the total number of identifiers.
33        // $ident, $($rest),*: the list of identifiers.
34        $vis:vis, $total:expr; $ident:ident $(, $($rest:ident),* $(,)? )?
35    ) => { $crate::paste! {
36        #[allow(non_upper_case_globals)]
37        $vis const $ident: usize = $total - $crate::ident_total!( $($($rest)*)? ) - 1;
38        $( $crate::ident_const_index!($vis, $total; $($rest),*); )?
39    }};
40}
41#[doc(inline)]
42pub use ident_const_index;