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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
//!
//! Readable indexing macros for 1-4 dimensional data structures
//!

#![no_std]


///
/// Converts an identifier _x_, _y_, _z_ or _w_ to a `usize` value.
///
/// Using any identifier apart from the above or multiple identifiers will result in a compile time error.
///
/// It is recommended to use parentheses when calling this macro for clarity.
///
/// # Possible Variations
///
/// ```
/// # #[macro_use] extern crate axmac; fn main() {
/// # use axmac::ax;
/// let first: usize = ax!(x);
/// let second = ax!(y);
/// let third  = ax!(z);
/// let fourth = ax!(w);
///
/// assert_eq!(first,  0);
/// assert_eq!(second, 1);
/// assert_eq!(third,  2);
/// assert_eq!(fourth, 3);
///
/// // ERROR: Only allowed to use one of x, y, z or w
/// // let fifth_axis = ax!(v);
///
/// // ERROR: Only accepts one identifier
/// //        If multiple axes are what you need, see the 'axs' macro
/// // let third_and_fourth = ax!(z, w);
/// # }
/// ```
///
#[macro_export]
macro_rules! ax {

    (x) => { 0usize };
    (y) => { 1usize };
    (z) => { 2usize };
    (w) => { 3usize };

}


///
/// Converts an array of identifiers _x_, _y_, _z_ or _w_ to an array of `usize` values
///
/// Using any identifier or expression apart from the above will result in a compile time error
///
/// It is recommended to use square brackets when calling this macro for clarity
///
/// # Possible Variations
///
/// ```
/// # #[macro_use] extern crate axmac; fn main() {
/// # use axmac::axs;
/// // Explicitly specifying items in array
/// let arr =  axs![x, y, z, w];
/// assert_eq!(arr, [0, 1, 2, 3]);
///
/// // Repeat specified item N times
/// let arr =  axs![w; 4];
/// assert_eq!(arr, [3, 3, 3, 3]);
/// # }
/// ```
///
/// Using identifiers multiple times is allowed, this is only a more readable way to create arrays after
/// all
///
/// ```
/// # #[macro_use] extern crate axmac; fn main() {
/// # use axmac::axs;
/// let arr  =  axs![x,x, z,z, y,y];
/// assert_eq!(arr, [0,0, 2,2, 1,1]);
/// # }
/// ```
///
#[macro_export]
macro_rules! axs {

    // [x, x, w, z, y, z]
    ( $( $d:ident ), * ) => { [ $( ax!($d), )* ] };

    // [z; 3]
    ( $d:ident; $i:expr ) => { [ax!($d); $i] };

}


///
/// Converts a range of identifiers and/or `usize` expressions to a range of `usize` values
///
/// Using any identifiers apart from _x_, _y_, _z_ or _w_ will result in a compile time error
///
///  It is recommended to use parentheses when calling this macro for clarity
///
/// # Possible Variations
///
/// ```
/// # #[macro_use] extern crate axmac; fn main() {
/// # use axmac::axr;
/// // PLEASE NOTE!
/// //  x => 0usize
/// //  y => 1
/// //  z => 2
/// //  w => 3
///
/// // Range with identifiers
/// assert_eq!(axr!(x..z), 0..2);
///
/// // RangeInclusive with identifiers
/// assert_eq!(axr!(y..=w), 1..=3);
///
/// // RangeTo with identifiers
/// assert_eq!(axr!(..z), ..2);
///
/// // RangeToInclusive with identifiers
/// assert_eq!(axr!(..=w), ..=3);
///
/// // RangeFrom with identifier
/// assert_eq!(axr!(y..), 1..);
///
/// // Range with identifier and expression
/// assert_eq!(axr!(x..10), 0..10);
///
/// // RangeInclusive with identifier and expression
/// assert_eq!(axr!(x..=7), 0..=7);
///
/// // Range with expression and identifier
/// //  The parentheses around the expression are compulsory
/// assert_eq!(axr!((0)..z), 0..2);
///
/// // RangeInclusive with expression and identifier
/// //  The parentheses around the expression are compulsory
/// assert_eq!(axr!((1)..=w), 1..=3);
/// # }
/// ```
///
#[macro_export]
macro_rules! axr {

    // Ident to Ident
    //  Range x..w
    ( $a:ident..$b:ident ) => { ax!($a)..ax!($b) };
    //  RangeInclusive y..=z
    ( $a:ident..=$b:ident ) => { ax!($a)..=ax!($b) };

    // Ident to Expr
    //  Range z..6
    ( $a:ident..$b:expr ) => { ax!($a)..$b };
    //  RangeInclusive w..=9
    ( $a:ident..=$b:expr ) => { ax!($a)..=$b };

    // Inf to Ident
    //  RangeTo ..w
    ( ..$a:ident ) => { ..ax!($a) };
    //  RangeToInclusive ..=z
    ( ..=$a:ident ) => { ..=ax!($a) };

    // Ident to Inf
    //  RangeFrom x..
    ( $a:ident.. ) => { ax!($a).. };

    // Expr to Ident
    //  Range (0)..z
    ( ($a:expr)..$b:ident )  => { $a..ax!($b) };
    // RangeInclusive (1)..=w
    ( ($a:expr)..=$b:ident )  => { $a..=ax!($b) };

}



#[cfg(test)]
mod tests {

    #[cfg(test)]
    mod ax {
        #[test]
        fn it_works() {
            assert_eq!(ax!(x), 0);
            assert_eq!(ax!(y), 1);
            assert_eq!(ax!(z), 2);
            assert_eq!(ax!(w), 3);
        }
    }

    #[cfg(test)]
    mod axs {
        #[test]
        fn it_works() {
            assert_eq!(axs![x,y,z,w], [0,1,2,3]);
            assert_eq!(axs![x,z,y],   [0,2,1]);
            assert_eq!(axs![x,y,x,y], [0,1,0,1]);
        }
    }


    #[cfg(test)]
    mod axr {

        #[test]
        fn ident_to_ident_works() {
            let arr = [0,1,2,3,4,5,6,7,8,9];
            let slice = &arr[axr![x..z]];
            assert_eq!(*slice, [0,1]);
        }
        #[test]
        fn ident_to_eq_ident_works() {
            let arr = [0,1,2,3,4,5,6,7,8,9];
            let slice = &arr[axr![y..=w]];
            assert_eq!(*slice, [1,2,3]);
        }

        #[test]
        fn ident_to_expr_works() {
            let arr = [0,1,2,3,4,5,6,7,8,9];
            let slice = &arr[axr![y..9]];
            assert_eq!(*slice, [1,2,3,4,5,6,7,8]);
        }
        #[test]
        fn ident_to_eq_expr_works() {
            let arr = [0,1,2,3,4,5,6,7,8,9];
            let slice = &arr[axr![x..=5]];
            assert_eq!(*slice, [0,1,2,3,4,5]);
        }

        #[test]
        fn inf_to_ident_works() {
            let arr = [0,1,2,3,4,5,6,7,8,9];
            let slice = &arr[axr![..w]];
            assert_eq!(*slice, [0,1,2]);
        }
        #[test]
        fn inf_to_eq_ident_works() {
            let arr = [0,1,2,3,4,5,6,7,8,9];
            let slice = &arr[axr![..=w]];
            assert_eq!(*slice, [0,1,2,3]);
        }

        #[test]
        fn ident_to_inf_works() {
            let arr = [0,1,2,3,4,5,6,7,8,9];
            let slice = &arr[axr![x..]];
            assert_eq!(*slice, arr);
        }

        #[test]
        fn expr_to_ident_works() {
            let arr = [0,1,2,3,4];
            let slice = &arr[axr!((0)..z)];
            assert_eq!(*slice, [0,1]);

            let expr = 1usize;
            let slice = &arr[axr!((expr)..w)];
            assert_eq!(*slice, [1, 2]);
        }

        #[test]
        fn expr_to_eq_ident_works() {
            let arr = [0,1,2,3,4];
            let slice = &arr[axr!((0)..=z)];
            assert_eq!(*slice, [0,1,2]);

            let expr = 1usize;
            let slice = &arr[axr!((expr)..=w)];
            assert_eq!(*slice, [1,2,3]);
        }

    }


}