devela/code/util/
capture.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// devela::code::util::capture
//
//! capture tokens macro functionality
//

/// Captures the first token from a list of inputs.
///
/// Usage: `first!(<category> <first>, <tail>*, <optional_comma>?);`
///
/// # Example
/// ```
/// # use devela::capture_first;
/// assert_eq![5, capture_first!(expr 5, 6, 7)];
/// assert_eq![42, capture_first!(literal 42, "hello", true)];
/// ```
#[macro_export]
#[rustfmt::skip]
#[cfg_attr(cargo_primary_package, doc(hidden))]
macro_rules! capture_first {
    (block $first:block $(, $tail:block)* $(,)?) => { $first };
    (expr $first:expr $(, $tail:expr)* $(,)?) => { $first };
    (ident $first:ident $(, $tail:ident)* $(,)?) => { $first };
    (literal $first:literal $(, $tail:literal)* $(,)?) => { $first };
    (meta $first:meta $(, $tail:meta)* $(,)?) => { $first };
    (pat $first:pat $(, $tail:pat)* $(,)?) => { $first };
    (path $first:path $(, $tail:path)* $(,)?) => { $first };
    (ty $first:ty $(, $tail:ty)* $(,)?) => { $first };
    (tt $first:tt $(, $tail:tt)* $(,)?) => { $first };
}
#[doc(inline)]
pub use capture_first;

/// Captures all the tokens except the first one, as a tuple.
#[macro_export]
#[rustfmt::skip]
#[cfg_attr(cargo_primary_package, doc(hidden))]
macro_rules! capture_tail_tuple {
    (block $first:block, $($tail:block),* $(,)?) => { ($($tail),*) };
    (expr $first:expr, $($tail:expr),* $(,)?) => { ($($tail),*) };
    (ident $first:ident, $($tail:ident),* $(,)?) => { ($($tail),*) };
    (literal $first:literal, $($tail:literal),* $(,)?) => { ($($tail),*) };
    (meta $first:meta, $($tail:meta),* $(,)?) => { ($($tail),*) };
    (pat $first:pat, $($tail:pat),* $(,)?) => { ($($tail),*) };
    (path $first:path, $($tail:path),* $(,)?) => { ($($tail),*) };
    (tt $first:tt, $($tail:tt),* $(,)?) => { ($($tail),*) };
    (ty $first:ty, $($tail:ty),* $(,)?) => { ($($tail),*) };

    // Handles the case where there is no tail (optional trailing comma)
    ($cat:tt $first:tt) => { () };
}
#[doc(inline)]
pub use capture_tail_tuple;

// /// Captures all the tokens except the first one.
// #[macro_export]
// #[rustfmt::skip]
// macro_rules! capture_tail {
//     (block $first:block, $($tail:block),*) => { $($tail),* };
//     (expr $first:expr, $($tail:expr),*) => { $($tail),* };
//     (ident $first:ident, $($tail:ident),*) => { $($tail),* };
//     (literal $first:literal, $($tail:literal),*) => { $($tail),* };
//     (meta $first:meta, $($tail:meta),*) => { $($tail),* };
//     (pat $first:pat, $($tail:pat),*) => { $($tail),* };
//     (path $first:path, $($tail:path),*) => { $($tail),* };
//     (tt $first:tt, $($tail:tt),*) => { $($tail),* };
//     (ty $first:ty, $($tail:ty),*) => { $($tail),* };
// }
// pub use capture_tail;

/// Captures the last token from a list of inputs.
#[doc(hidden)]
#[macro_export]
#[rustfmt::skip]
macro_rules! _capture_last {
    // Base case: when there is only one item left, return it
    (block $first:block) => { $first };
    (expr $first:expr) => { $first };
    (ident $first:ident) => { $first };
    (literal $first:literal) => { $first };
    (meta $first:meta) => { $first };
    (pat $first:pat) => { $first };
    (path $first:path) => { $first };
    (tt $first:tt) => { $first };
    (ty $first:ty) => { $first };

    // Recursive case: discard the first item and continue with the rest
    (block $first:block, $($tail:block),* $(,)?) => {
        $crate::capture_last!(block $($tail),*) };
    (expr $first:expr, $($tail:expr),* $(,)?) => {
        $crate::capture_last!(expr $($tail),*) };
    (ident $first:ident, $($tail:ident),* $(,)?) => {
        $crate::capture_last!(ident $($tail),*) };
    (literal $first:literal, $($tail:literal),* $(,)?) => {
        $crate::capture_last!(literal $($tail),*) };
    (meta $first:meta, $($tail:meta),* $(,)?) => {
        $crate::capture_last!(meta $($tail),*) };
    (pat $first:pat, $($tail:pat),* $(,)?) => {
        $crate::capture_last!(pat $($tail),*) };
    (path $first:path, $($tail:path),* $(,)?) => {
        $crate::capture_last!(path $($tail),*) };
    (tt $first:tt, $($tail:tt),* $(,)?) => {
        $crate::capture_last!(tt $($tail),*) };
    (ty $first:ty, $($tail:ty),* $(,)?) => {
        $crate::capture_last!(ty $($tail),*) };
}
#[doc(inline)]
pub use _capture_last as capture_last;

#[cfg(test)]
mod tests {
    pub use super::*;

    #[test]
    fn capture_first() {
        assert_eq![{ 2 }, capture_first!(block { 1 + 1 }, { loop {} }, { 3 * 2 })];

        assert_eq![5, capture_first!(expr 5, "a", if a == 3 {}, 4 + 3)];

        let my_var = ();
        assert_eq![my_var, capture_first!(ident my_var, another_var)];

        assert_eq!["32", capture_first!(literal "32", 100, '€')];

        assert_eq!['%', capture_first!(tt '%', "$", 3, {}, something)];

        let captured = 1 as capture_first!(ty i32, u64, bool, f32);
        assert_eq![1_i32, captured];

        /* single item */

        assert_eq!(5, capture_first!(expr 5));
        assert_eq!(my_var, capture_first!(ident my_var));
        assert_eq!('€', capture_first!(literal '€'));
    }

    #[test]
    fn capture_tail_tuple() {
        assert_eq![(6, "a"), capture_tail_tuple!(expr if a == 3 {}, 6, "a")];

        let (my_var, another_var) = ((), ());
        assert_eq![another_var, capture_tail_tuple!(ident my_var, another_var)];

        assert_eq![(my_var, another_var), capture_tail_tuple!(tt { token }, my_var, another_var)];

        struct TestStruct<T>(T);
        let _instance: TestStruct<capture_tail_tuple!(ty i32, f32, bool)> = TestStruct((2.5, true));
    }

    #[test]
    fn capture_last() {
        assert_eq![{ 6 }, capture_last!(block { 1 + 1 }, { loop {} }, { 3 * 2 })];

        assert_eq![7, capture_last!(expr 5, "a", if a == 3 {}, 4 + 3)];

        let (my_var, another_var) = ((), ());
        assert_eq![another_var, capture_last!(ident my_var, another_var)];

        assert_eq!['€', capture_last!(literal "32", 100, '€')];

        let something = ();
        assert_eq![something, capture_last!(tt '%', "$", 3, {}, something)];

        let captured = 1.3 as capture_last!(ty i32, u64, bool, f32);
        assert_eq![1.3_f32, captured];

        /* single item */

        assert_eq!(5, capture_last!(expr 5));
        assert_eq!(my_var, capture_last!(ident my_var));
        assert_eq!('€', capture_last!(literal '€'));
    }
}