shrimple_parser/
tuple.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
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
//! This module contains utilities for working with generic tuples, such as:
//! - Extracting & transforming the N-th element of a tuple;
//! - Extracting N first elements of a tuple or splitting it;
//! - Extending a tuple from both ends;
//! - Reversing a tuple.
//! - Copying/cloning a tuple element per element. (i.e. turn `(&T, &U)` into `(T, U)`
//!
//! See the [`Tuple`] trait or the free-standing functions.

/// The trait for a tuple that has the N-th element, the backbone of the [`Tuple::nth`] function.
/// The associated functions are not to be used directly, instead use the equivalent functions
/// or methods of the [`Tuple`] trait.
#[diagnostic::on_unimplemented(
    message = "`{Self}` is not a tuple, doesn't have an element #{N}, or is too long",
    note = "At the moment, the trait is implemented only for tuples up to length 8"
)]
pub trait Index<const N: usize>: Tuple {
    /// The N-th element of the tuple.
    type Nth;
    /// The tuple with its N-th element mapped to `U`.
    type NthMapped<U>;

    /// Returns the N-th element of the tuple.
    fn nth(this: Self) -> Self::Nth;

    /// Returns a reference to the N-th element of the tuple.
    fn nth_ref(this: &Self) -> &Self::Nth;

    /// Returns the tuple with the N-th element transformed by `f`
    fn map_nth<U>(this: Self, f: impl FnOnce(Self::Nth) -> U) -> Self::NthMapped<U>;
}

/// The trait for a tuple that has at least N elements, the backbone of the
/// [`Tuple::first_n`] function.
/// The associated functions are not to be used directly, instead use the equivalent functions
/// or methods of the [`Tuple`] trait.
#[diagnostic::on_unimplemented(
    message = "`{Self}` is not tuple, has less than {N} elements, or is too long",
    note = "At the moment, the trait is implemented only for tuples up to length 8",
)]
pub trait Slice<const N: usize>: Tuple {
    /// A tuple containing the first N elements of the original tuple. 
    type FirstN;

    /// A tuple with the first N elements of the original tuple.
    type FirstNStripped;

    /// Return the first N elements of the tuple as a tuple.
    fn first_n(this: Self) -> Self::FirstN;

    /// Returns the tuple without the first N elements
    fn strip_first_n(this: Self) -> Self::FirstNStripped;

    /// Splits the tuple into 2, with the 1st tuple having the 1st N element,
    /// and the 2nd tuple having the rest.
    fn split(this: Self) -> (Self::FirstN, Self::FirstNStripped);
}

/// The trait for a tuple, all elements of which are references to [`Clone`]-able values,
/// the backbone of the [`Tuple::cloned`] function.
/// The associated functions are not to be used directly, instead use the equivalent free-standing
/// functions or methods of the [`Tuple`] trait.
pub trait CloneableRefs: Tuple {
    /// The result of [`CloneableRefs::cloned`]
    type Cloned;

    /// Clone the tuple element-wise, e.g. turn `(&T, &U)` into `(T, U)`
    fn cloned(this: Self) -> Self::Cloned;
}

/// The trait for a tuple, all elements of which are references to [`Copy`]-able values,
/// the backbone of the [`Tuple::copied`] function.
/// The associated functions are not to be used directly, instead use the equivalent free-standing
/// functions or methods of the [`Tuple`] trait.
pub trait CopiableRefs: Tuple {
    /// The result of [`CopiableRefs::copied`]
    type Copied;

    /// Copy the tuple element-wise, e.g. turn `(&T, &U)` into `(T, U)`
    fn copied(this: Self) -> Self::Copied;
}

macro_rules! impl_nth_methods {
    ($n:literal, $name:ident, $ref_name:ident, $map_name:ident) => {
        #[doc = concat!("Returns the ", stringify!($name), " element of the tuple.")]
        #[doc = "For a more generic function, see [`Tuple::nth`]"]
        fn $name(self) -> Self::Nth where Self: Index<$n> {
            Index::nth(self)
        }

        #[doc = concat!("Returns a reference to the ", stringify!($name), " element of the tuple.")]
        #[doc = "For a more generic function, see [`Tuple::nth_ref`]"]
        fn $ref_name(&self) -> &Self::Nth where Self: Index<$n> {
            Index::nth_ref(self)
        }

        #[doc = concat!("Transforms the ", stringify!($name), " element of the tuple with `f`.")]
        #[doc = "For a more generic function, see [`Tuple::map_nth`]"]
        fn $map_name<U>(self, f: impl FnOnce(Self::Nth) -> U) -> Self::NthMapped<U>
        where
            Self: Index<$n>
        {
            Index::map_nth(self, f)
        }
    };
}

/// Trait for a generic tuple.
#[diagnostic::on_unimplemented(
    message = "`{Self}` is not a tuple or is too long",
    note = "At the moment, the trait is implemented only for tuples up to length 8"
)]
pub trait Tuple: Sized {
    /// The tuple + a new element at the end, the result of [`Tuple::append`]
    type Appended<NewElement>;

    /// The tuple + a new element at the start, the result of [`Tuple::prepend`]
    type Prepended<NewElement>;

    /// The tuple with its elements in reverse order, the result of [`Tuple::rev`]
    type Reversed;

    /// Adds `new_element` to the end of the tuple.
    /// Also see [`append`]
    fn append<NewElement>(self, new_element: NewElement) -> Self::Appended<NewElement>;

    /// Adds `new_element` to the start of the tuple.
    /// Also see [`prepend`]
    fn prepend<NewElement>(self, new_element: NewElement) -> Self::Prepended<NewElement>;

    /// Returns the tuple with its elements in reverse order.
    /// Also see [`rev`]
    fn rev(self) -> Self::Reversed;

    /// Clones the tuple element-wise, e.g. turn `(&T, &U)` into `(T, U)`
    /// Also see [`cloned`]
    fn cloned(self) -> Self::Cloned where Self: CloneableRefs {
        CloneableRefs::cloned(self)
    }

    /// Copies the tuple element-wise, e.g. turn `(&T, &U)` into `(T, U)`
    /// Also see [`copied`]
    fn copied(self) -> Self::Copied where Self: CopiableRefs {
        CopiableRefs::copied(self)
    }

    /// Returns the `N`-th element of the tuple.
    /// For shortcuts see [`Tuple::first`], [`Tuple::second`], [`Tuple::third`]
    fn nth<const N: usize>(self) -> Self::Nth where Self: Index<N> {
        Index::nth(self)
    }

    /// Returns a reference to the `N`-th element of the tuple.
    /// For shortcuts see [`Tuple::first_ref`], [`Tuple::second_ref`], [`Tuple::third_ref`]
    fn nth_ref<const N: usize>(&self) -> &Self::Nth where Self: Index<N> {
        Index::nth_ref(self)
    }

    /// Returns a function that transforms the N-th element of a tuple with `f`.
    /// For common shortcuts, see [`Tuple::map_first`], [`Tuple::map_second`], [`Tuple::map_third`]
    fn map_nth<const N: usize, U>(self, f: impl FnOnce(Self::Nth) -> U) -> Self::NthMapped<U>
    where
        Self: Index<N>
    {
        Index::map_nth(self, f)
    }

    impl_nth_methods!(0, first, first_ref, map_first);
    impl_nth_methods!(1, second, second_ref, map_second);
    impl_nth_methods!(2, third, third_ref, map_third);

    /// Returns a tuple that containing the first N elements of the original tuple.
    /// The other elements are discarded.
    fn first_n<const N: usize>(self) -> Self::FirstN where Self: Slice<N> {
        Slice::first_n(self)
    }

    /// Returns the original tuple with its first N elements discarded.
    /// Logical complement of [`Tuple::first_n`]
    fn strip_first_n<const N: usize>(self) -> Self::FirstNStripped where Self: Slice<N> {
        Slice::strip_first_n(self)
    }

    /// Splits the tuple into one with the first N elements and one with the rest.
    fn split<const N: usize>(self) -> (Self::FirstN, Self::FirstNStripped)
    where
        Self: Slice<N>
    {
        Slice::split(self)
    }
}

macro_rules! rev {
    ($($x:ident,)*) => { rev!(| $($x,)* |) };
    (| $x:ident, $($rest:ident,)* | $($rev:ident,)*) => { rev!(| $($rest,)* | $x, $($rev,)*) };
    (| | $($rev:ident,)*) => { ($($rev,)*) };
}

macro_rules! impl_tuple_traits {
    ($length:literal - $($n:literal : $t:ident),*) => {
        impl_tuple_traits!([] [$($n:$t,)*] [$($t),*]);

        impl<$($t),*> Slice<$length> for ($($t,)*) {
            type FirstN = Self;
            type FirstNStripped = ();

            fn first_n(this: Self) -> Self::FirstN { this }
            fn strip_first_n(_: Self) -> Self::FirstNStripped {}
            fn split(this: Self) -> (Self::FirstN, Self::FirstNStripped) { (this, ()) }
        }

        #[allow(non_snake_case)]
        impl<$($t: Clone),*> CloneableRefs for ($(&$t,)*) {
            type Cloned = ($($t,)*);

            #[allow(clippy::unused_unit)]
            fn cloned(this: Self) -> Self::Cloned {
                let ($($t,)*) = this;
                ($($t.clone(),)*)
            }
        }

        #[allow(non_snake_case)]
        impl<$($t: Copy),*> CopiableRefs for ($(&$t,)*) {
            type Copied = ($($t,)*);

            #[allow(clippy::unused_unit)]
            fn copied(this: Self) -> Self::Copied {
                let ($($t,)*) = this;
                ($(*$t,)*)
            }
        }

        #[allow(non_snake_case)]
        impl<$($t),*> Tuple for ($($t,)*) {
            type Appended<NewElement> = ($($t,)* NewElement,);
            type Prepended<NewElement> = (NewElement, $($t,)*);
            type Reversed = rev!($($t,)*);

            fn append<NewElement>(self, new_element: NewElement) -> Self::Appended<NewElement> {
                let ($($t,)*) = self;
                ($($t,)* new_element,)
            }

            fn prepend<NewElement>(self, new_element: NewElement) -> Self::Prepended<NewElement> {
                let ($($t,)*) = self;
                (new_element, $($t,)*)
            }

            fn rev(self) -> Self::Reversed {
                let ($($t,)*) = self;
                rev!($($t,)*)
            }
        }
    };

    ($prev:tt [] $t:tt) => {};

    ([$($prev:ident),*] [$id:literal : $nth:ident, $($next_id:literal : $next:ident,)*] [$($t:ident),+]) => {
        #[allow(non_snake_case)]
        impl<$($t),+> Index<$id> for ($($t,)+) {
            type Nth = $nth;
            type NthMapped<U> = ($($prev,)* U, $($next,)*);

            #[allow(unused)]
            fn nth(this: Self) -> Self::Nth {
                let ($($t,)+) = this;
                $nth
            }

            #[allow(unused)]
            fn nth_ref(this: &Self) -> &Self::Nth {
                let ($($t,)+) = this;
                $nth
            }

            fn map_nth<U>(this: Self, f: impl FnOnce(Self::Nth) -> U) -> Self::NthMapped<U> {
                let ($($t,)+) = this;
                ($($prev,)* f($nth), $($next,)*)
            }
        }

        #[allow(non_snake_case)]
        impl<$($t),+> Slice<$id> for ($($t,)+) {
            type FirstN = ($($prev,)*);
            type FirstNStripped = ($nth, $($next,)*);

            #[allow(unused, clippy::unused_unit)]
            fn first_n(this: Self) -> Self::FirstN {
                let ($($t,)+) = this;
                ($($prev,)*)
            }

            #[allow(unused)]
            fn strip_first_n(this: Self) -> Self::FirstNStripped {
                let ($($t,)+) = this;
                ($nth, $($next,)*)
            }

            fn split(this: Self) -> (Self::FirstN, Self::FirstNStripped) {
                let ($($t,)+) = this;
                (($($prev,)*), ($nth, $($next,)*))
            }
        }

        impl_tuple_traits!([$($prev,)* $nth] [$($next_id:$next,)*] [$($t),+]);
    };
}

impl_tuple_traits!(0 -);
impl_tuple_traits!(1 - 0: T0);
impl_tuple_traits!(2 - 0: T0, 1: T1);
impl_tuple_traits!(3 - 0: T0, 1: T1, 2: T2);
impl_tuple_traits!(4 - 0: T0, 1: T1, 2: T2, 3: T3);
impl_tuple_traits!(5 - 0: T0, 1: T1, 2: T2, 3: T3, 4: T4);
impl_tuple_traits!(6 - 0: T0, 1: T1, 2: T2, 3: T3, 4: T4, 5: T5);
impl_tuple_traits!(7 - 0: T0, 1: T1, 2: T2, 3: T3, 4: T4, 5: T5, 6: T6);
impl_tuple_traits!(8 - 0: T0, 1: T1, 2: T2, 3: T3, 4: T4, 5: T5, 6: T6, 7: T7);

macro_rules! impl_nth_fn {
    ($n:literal, $name:ident, $ref_name:ident, $map_name:ident) => {
        #[doc = concat!("Returns the ", stringify!($name), " element of the tuple.")]
        #[doc = "For a more generic function, see [`Tuple::nth`]"]
        pub fn $name<T: Index<$n>>(tuple: T) -> T::Nth {
            Index::nth(tuple)
        }

        #[doc = concat!("Returns a reference to the ", stringify!($name), " element of the tuple.")]
        #[doc = "For a more generic function, see [`Tuple::nth_ref`]"]
        pub fn $ref_name<T: Index<$n>>(tuple: &T) -> &T::Nth {
            Index::nth_ref(tuple)
        }

        #[doc = concat!(
            "Returns a function that transforms the ",
            stringify!($name),
            " element of a tuple with `f`."
        )]
        #[doc = "For a more generic function, see [`Tuple::map_nth`]"]
        pub fn $map_name<T: Index<$n>, U>(mut f: impl FnMut(T::Nth) -> U)
            -> impl FnMut(T)
            -> T::NthMapped<U>
        {
            move |tuple| Index::map_nth(tuple, &mut f)
        }
    };
}

impl_nth_fn!(0, first, first_ref, map_first);
impl_nth_fn!(1, second, second_ref, map_second);
impl_nth_fn!(2, third, third_ref, map_third);

/// Adds `new_element` to the end of a tuple and returns the resulting new tuple.
pub fn append<T: Tuple, U: Clone>(new_element: U) -> impl Fn(T) -> T::Appended<U> {
    move |tuple| tuple.append(new_element.clone())
}

/// Adds `new_element` to the beginning of a tuple and returns the resulting new tuple.
pub fn prepend<U: Clone, T: Tuple>(new_element: U) -> impl Fn(T) -> T::Prepended<U> {
    move |tuple| tuple.prepend(new_element.clone())
}

/// Turns `T` into a tuple with 1 element, `T`
pub const fn tuple<T>(x: T) -> (T,) { (x,) }

/// Reverses the tuple.
pub fn rev<T: Tuple>(x: T) -> T::Reversed {
    x.rev()
}

/// Clones the tuple element-wise, e.g. turns `(&T, &U)` into `(T, U)`
pub fn cloned<T: CloneableRefs>(x: T) -> T::Cloned {
    CloneableRefs::cloned(x)
}

/// Copies the tuple element-wise, e.g. turns `(&T, &U)` into `(T, U)`
pub fn copied<T: CopiableRefs>(x: T) -> T::Copied {
    CopiableRefs::copied(x)
}

/// Generates a closure that constructs a struct from a tuple.
/// The struct fields must be exactly in the order in which they're expected to be in the tuple.
/// ```rust
/// # fn main() {
/// use shrimple_parser::{Parser, pattern::parse_until_ex, from_tuple};
///
/// #[derive(Debug, PartialEq, Eq)]
/// struct Example<'src> { a: &'src str, b: &'src str }
///
/// let input = "abc|def|";
/// let res = parse_until_ex("|")
///     .and(parse_until_ex("|"))
///     .map(from_tuple!(Example { a, b }))
///     .parse(input);
/// assert_eq!(res, Ok(("", Example { a: "abc", b: "def" })))
/// # }
/// ```
#[macro_export]
macro_rules! from_tuple {
    ($name:ident { $($field:ident),* $(,)? }) => { |($($field,)*)| $name { $($field),* } };
}

#[macro_export]
#[doc(hidden)]
macro_rules! last {
    ($_:tt $($rest:tt)+) => { $($rest)+ };
    ($last:tt) => { $last };
}

/// Generates a closure that calls a function with a tuple's contents as it arguments.
/// The input can be anything as long as the last token contains all the arguments parenthesized.
/// ```rust
/// # fn main() {
/// use shrimple_parser::{Parser, pattern::parse_until_ex, call};
///
/// fn len_sum(a: &str, b: &str) -> usize {
///     a.len() + b.len()
/// }
///
/// let input = "abc|def|";
/// let res = parse_until_ex("|")
///     .and(parse_until_ex("|"))
///     .map(call!(len_sum(a, b)))
///     .parse(input);
/// assert_eq!(res, Ok(("", 6)))
/// # }
/// ```
#[macro_export]
macro_rules! call {
    ($($args:tt)*) => { |$crate::last!($($args)*)| $($args)* };
}