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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#[doc(hidden)]
#[macro_export]
macro_rules! __inner_field_pat{
    (
        output($($prev:tt)*)
        rem($f0:pat,$f1:pat,$f2:pat,$f3:pat,$f4:pat,$f5:pat,$f6:pat,$f7:pat, $($rem:tt)*)
    )=>{
        $crate::__inner_field_pat!(
            output($($prev)* ($f0,$f1,$f2,$f3,$f4,$f5,$f6,$f7), )
            rem( $($rem)* )
        )
    };
    (
        output($($prev:tt)*)
        rem()
    )=>{
        ($($prev)*)
    };
    (
        output($($prev:tt)*)
        rem($($rem:tt)*)
    )=>{
        ($($prev)* ($($rem)*),)
    };
}

/// Macro to destructure the tuple returned by `StructuralExt` methods that
/// access multiple fields.
///
/// This macro is most useful when destructuring a tuple of over 8 fields,
/// since accessing over 8 fields returns a tuple of tuples (8 fields each).
///
/// # StructuralExt Example
///
/// <span id="the-structuralext-example"></span>
///
/// These examples demonstrate both
/// the return value of `StructuralExt` methods that return over 8 fields,
/// as well as destructuring the return value using the `field_pat` macro.
///
/// Calling **`StructuralExt::fields`**:
/// ```rust
/// use structural::{field_pat, fp, StructuralExt};
///
/// let arr = [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27];
///
/// assert_eq!(
///     arr.fields(fp!(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 ,15, 16)),
///     (
///         (&10, &11, &12, &13, &14, &15, &16, &17),
///         (&18, &19, &20, &21, &22, &23, &24, &25),
///         (&26,)
///     )
/// );
///
/// let field_pat!(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, b0, b1, b2, b3, b4, b5, b6)=
///     arr.fields(fp!(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 ,15, 16));
///
/// assert_eq!(
///     [a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, b0, b1, b2, b3, b4, b5, b6],
///     [&10, &11, &12, &13, &14, &15, &16, &17, &18, &19, &20, &21, &22, &23, &24, &25, &26],
/// );
/// ```
///
/// Calling **`StructuralExt::fields_mut`**:
/// ```rust
/// use structural::{field_pat, fp, StructuralExt};
///
/// let mut arr = [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27];
///
/// assert_eq!(
///     arr.fields_mut(fp!(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 ,15, 16)),
///     (
///         (&mut 10, &mut 11, &mut 12, &mut 13, &mut 14, &mut 15, &mut 16, &mut 17),
///         (&mut 18, &mut 19, &mut 20, &mut 21, &mut 22, &mut 23, &mut 24, &mut 25),
///         (&mut 26,)
///     )
/// );
///
/// let field_pat!(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, b0, b1, b2, b3, b4, b5, b6)=
///     arr.fields_mut(fp!(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 ,15, 16));
///
/// assert_eq!(
///     [a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, b0, b1, b2, b3, b4, b5, b6],
///     [
///         &mut 10, &mut 11, &mut 12, &mut 13, &mut 14, &mut 15, &mut 16, &mut 17,
///         &mut 18, &mut 19, &mut 20, &mut 21, &mut 22, &mut 23, &mut 24, &mut 25,
///         &mut 26
///     ],
/// );
///
/// ```
///
/// Calling **`StructuralExt::into_fields`**:
/// ```rust
/// use structural::{field_pat, fp, StructuralExt};
///
/// let arr = [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27];
///
/// assert_eq!(
///     arr.into_fields(fp!(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 ,15, 16)),
///     (
///         (10, 11, 12, 13, 14, 15, 16, 17),
///         (18, 19, 20, 21, 22, 23, 24, 25),
///         (26,)
///     )
/// );
///
/// let field_pat!(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, b0, b1, b2, b3, b4, b5, b6)=
///     arr.into_fields(fp!(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 ,15, 16));
///
/// assert_eq!(
///     [a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, b0, b1, b2, b3, b4, b5, b6],
///     [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26],
/// );
///
/// ```
///
/// # Example
///
/// This demonstrates what values this pattern macro destructures.
///
/// ```rust
/// use structural::field_pat;
///
/// let field_pat!() = ();
///
/// let field_pat!(f0) = (0,);
///
/// let field_pat!(f0, f1) = (0, 1);
///
/// let field_pat!(f0, f1, f2, f3, f4, f5, f6, f7) = (0, 1, 2, 3, 4, 5, 6, 7);
///
/// let field_pat!(f0, f1, f2, f3, f4, f5, f6, f7, f8) = (
///     (0, 1, 2, 3, 4, 5, 6, 7),
///     (8,),
/// );
///
/// let field_pat!(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, b0, b1, b2, b3, b4, b5) = (
///     (0, 1, 2, 3, 4, 5, 6, 7),
///     (8, 9, 10, 11, 12, 13, 14 ,15),
/// );
///
/// let field_pat!(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, b0, b1, b2, b3, b4, b5, b6) = (
///     (0, 1, 2, 3, 4, 5, 6, 7),
///     (8, 9, 10, 11, 12, 13, 14 ,15),
///     (16,),
/// );
///
/// ```
///
#[macro_export]
macro_rules! field_pat{
    ()=>{ () };
    ($f0:pat,$f1:pat,$f2:pat,$f3:pat,$f4:pat,$f5:pat,$f6:pat,$f7:pat $(,)? )=>{
        ($f0,$f1,$f2,$f3,$f4,$f5,$f6,$f7)
    };
    ($f0:pat,$f1:pat,$f2:pat,$f3:pat,$f4:pat,$f5:pat,$f6:pat,$f7:pat, $($rem:pat),* $(,)? )=>{
        $crate::__inner_field_pat!(
            output()
            rem($f0,$f1,$f2,$f3,$f4,$f5,$f6,$f7, $($rem,)*)
        )
    };
    ( $($f:pat),* $(,)? )=>{
        ($($f,)*)
    };
}

#[doc(hidden)]
#[macro_export]
macro_rules! __inner_path_tuple{
    (
        output($($prev:tt)*)
        rem($f0:expr,$f1:expr,$f2:expr,$f3:expr,$f4:expr,$f5:expr,$f6:expr,$f7:expr, $($rem:tt)*)
    )=>{
        $crate::__inner_path_tuple!(
            output($($prev)* ($f0,$f1,$f2,$f3,$f4,$f5,$f6,$f7), )
            rem( $($rem)* )
        )
    };
    (
        output($($prev:tt)*)
        rem()
    )=>{
        ($($prev)*)
    };
    (
        output($($prev:tt)*)
        rem($($rem:tt)*)
    )=>{
        ($($prev)* ($($rem)*),)
    };
}

/// For manually constructing a `FieldPathSet` to access up to 64 fields.
///
/// # Example
///
/// This demonstrates how to construct a `FieldPathSet` to access over 8 fields.
///
/// ```rust
/// use structural::{ FieldPathSet, StructuralExt, path_tuple, ts };
///
/// let this = ('a', 'b', 3, 5, "foo", "bar", false, true, Some(8), Some(13));
///
/// // If you access up to 8 fields with `FieldPathSet::large(path_tuple!(....))`,
/// // then accessor methods return non-nested tuples.
/// {
///     let path8 = FieldPathSet::large(path_tuple!(
///         ts!(0), ts!(1), ts!(2), ts!(3), ts!(4), ts!(5), ts!(6), ts!(7)
///     ));
///     assert_eq!(
///         this.fields(path8),
///         (&'a', &'b', &3, &5, &"foo", &"bar", &false, &true)
///     );
/// }
///
/// // If you access more than 8 fields with `FieldPathSet::large(path_tuple!(....))`,
/// // then accessor methods return nested tuples. 8 elements each.
/// {
///     let path10 = FieldPathSet::large(path_tuple!(
///         ts!(0), ts!(1), ts!(2), ts!(3), ts!(4), ts!(5), ts!(6), ts!(7),
///         ts!(8), ts!(9),
///     ));
///     assert_eq!(
///         this.fields(path10),
///         (
///             (&'a', &'b', &3, &5, &"foo", &"bar", &false, &true),
///             (&Some(8), &Some(13))
///         ),
///     );
///     assert_eq!(
///         this.cloned_fields(path10),
///         (
///             ('a', 'b', 3, 5, "foo", "bar", false, true),
///             (Some(8), Some(13))
///         ),
///     );
/// }
///
/// ```
///
/// # Example
///
/// This demnstrates what the macro expands into:
///
/// ```rust
/// use structural::path_tuple;
///
/// assert_eq!( path_tuple!(), () );
///
/// assert_eq!( path_tuple!(1), ((1,),) );
///
/// assert_eq!( path_tuple!(1, 2), ((1, 2),) );
///
/// assert_eq!(
///     path_tuple!(0, 1, 2, 3, 4, 5, 6, 7),
///     ((0, 1, 2, 3, 4, 5, 6, 7),),
/// );
///
/// assert_eq!(
///     path_tuple!(0, 1, 2, 3, 4, 5, 6, 7, 8),
///     ((0, 1, 2, 3, 4, 5, 6, 7), (8,)),
/// );
///
/// assert_eq!(
///     path_tuple!(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15),
///     (
///         (0, 1, 2, 3, 4, 5, 6, 7),
///         (8, 9, 10, 11, 12, 13, 14, 15),
///     ),
/// );
///
/// assert_eq!(
///     path_tuple!(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16),
///     (
///         (0, 1, 2, 3, 4, 5, 6, 7),
///         (8, 9, 10, 11, 12, 13, 14, 15),
///         (16,),
///     )
/// );
///
/// ```
///
#[macro_export]
macro_rules! path_tuple{
    ()=>{ () };
    ($($expr:expr),* $(,)?)=>{
        $crate::__inner_path_tuple!(output() rem($($expr,)*))
    }
}

#[cfg(test)]
mod tests {
    #[test]
    fn field_pat_tests() {
        let field_pat!() = ();

        {
            let field_pat!(a) = (1,);
            assert_eq!(a, 1);
        }
        {
            let field_pat!(a, b, c, d, e, f, g, h) = (1, 2, 3, 4, 5, 6, 7, 8);
            assert_eq!((a, b, c, d, e, f, g, h), (1, 2, 3, 4, 5, 6, 7, 8));
        }
        {
            let field_pat!(a, b, c, d, e, f, g, h, i,) = ((1, 2, 3, 4, 5, 6, 7, 8), (9,));
            assert_eq!((a, b, c, d, e, f, g, h), (1, 2, 3, 4, 5, 6, 7, 8));
            assert_eq!(i, 9);
        }
        {
            let field_pat!(
                f00, f01, f02, f03, f04, f05, f06, f07, f10, f11, f12, f13, f14, f15, f16, f17,
                f20,
            ) = (
                (1, 2, 3, 4, 5, 6, 7, 8),
                (11, 12, 13, 14, 15, 16, 17, 18),
                (20,),
            );
            assert_eq!(
                (f00, f01, f02, f03, f04, f05, f06, f07,),
                (1, 2, 3, 4, 5, 6, 7, 8)
            );
            assert_eq!(
                (f10, f11, f12, f13, f14, f15, f16, f17,),
                (11, 12, 13, 14, 15, 16, 17, 18)
            );
            assert_eq!(f20, 20);
        }
    }

    #[test]
    fn path_tuple_tests() {
        let field_pat!() = ();

        {
            let ((a,),) = path_tuple!(1);
            assert_eq!(a, 1);
        }
        {
            let (tup0,) = path_tuple!(1, 2, 3, 4, 5, 6, 7, 8);
            assert_eq!(tup0, (1, 2, 3, 4, 5, 6, 7, 8));
        }
        {
            let (tup0, tup1) = path_tuple!(1, 2, 3, 4, 5, 6, 7, 8, 10);
            assert_eq!(tup0, (1, 2, 3, 4, 5, 6, 7, 8));
            assert_eq!(tup1, (10,));
        }
        {
            let (tup0, tup1) = path_tuple!(1, 2, 3, 4, 5, 6, 7, 8, 11, 12, 13, 14, 15, 16, 17, 18,);
            assert_eq!(tup0, (1, 2, 3, 4, 5, 6, 7, 8));
            assert_eq!(tup1, (11, 12, 13, 14, 15, 16, 17, 18,));
        }
        {
            let (tup0, tup1, tup2) =
                path_tuple!(1, 2, 3, 4, 5, 6, 7, 8, 11, 12, 13, 14, 15, 16, 17, 18, 20,);
            assert_eq!(tup0, (1, 2, 3, 4, 5, 6, 7, 8));
            assert_eq!(tup1, (11, 12, 13, 14, 15, 16, 17, 18,));
            assert_eq!(tup2, (20,));
        }
    }
}