any_of 2.2.0

A general optional sum of product type which can be Neither, Left, Right or Both.
Documentation
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
//! This module provides type definitions representing combinations
//! of multiple possible types and their associated utility implementations.
//!
//! # Overview
//!
//! The `AnyOf` types are designed to express collections of various
//! possible types within a single type while preserving type safety
//! and making it easier to work with nested combinations of types.
//! Each `AnyOfX` type represents a combination containing up to
//! `X` potential variants.
//!
//! # Utility Implementations
//!
//! Each `AnyOfX` type provides utility methods to extract specific variants
//! if they exist. These methods are named based on their position in
//! the hierarchy and return an `Option` of the specified type.
//!
//! Example for `AnyOf8`:
//! - `lll()`: Returns the left-left-left value if it exists.
//! - `lrr()`: Returns the left-right-right value if it exists.
//! - `rrr()`: Returns the right-right-right value if it exists.
//!
//! Example usage:
//! ```rust
//! use any_of::AnyOf4;
//! use any_of::AnyOf;
//!
//! let value: AnyOf4<i32, &str, f64, char> = AnyOf4::new_left(AnyOf::new_left(42));
//! if let Some(inner) = value.ll() {
//!     println!("The left-left value is: {}", inner);
//! }
//! ```
//!
//! # Note
//! These `AnyOfX` types are highly compositional but increase in complexity as the `X` grows.
//! Use `AnyOf16` or higher with caution.

use crate::concepts::{Opt16, Opt4, Opt8};
use crate::{AnyOf, LeftOrRight};

/// A type representing a combination of four possible types.
pub type AnyOf4<LL, LR = LL, RL = LR, RR = RL> = AnyOf<AnyOf<LL, LR>, AnyOf<RL, RR>>;

impl<LL, LR, RL, RR> AnyOf4<LL, LR, RL, RR> {
    /// Creates a new `AnyOf4` instance from four optional values.
    ///
    /// The parameters correspond to the `ll`, `lr`, `rl`, and `rr` positions in
    /// the nested [`AnyOf`] structure. This mirrors [`AnyOf::new`].
    ///
    /// # Examples
    /// ```rust
    /// use any_of::{AnyOf4, AnyOf};
    ///
    /// let value = AnyOf4::new4(Some(1), None::<i32>, None::<i32>, None::<i32>);
    /// assert_eq!(value.ll(), Some(&1));
    /// ```
    pub fn new4(
        ll: Option<LL>,
        lr: Option<LR>,
        rl: Option<RL>,
        rr: Option<RR>,
    ) -> Self {
        Self::from_opt4((ll, lr, rl, rr))
    }

    /// Builds an `AnyOf4` from an [`Opt4`] tuple.
    ///
    /// `Opt4` is a convenient representation of four optional values.
    ///
    /// # Examples
    /// ```rust
    /// use any_of::{AnyOf4, Opt4};
    ///
    /// let tuple: Opt4<i32, i32, i32, i32> = (Some(1), None, None, None);
    /// let value = AnyOf4::from_opt4(tuple);
    /// assert!(value.ll().is_some());
    /// ```
    pub fn from_opt4(opt: Opt4<LL, LR, RL, RR>) -> Self {
        let (ll, lr, rl, rr) = opt;
        let left = if ll.is_none() && lr.is_none() {
            None
        } else {
            Some(AnyOf::new(ll, lr))
        };
        let right = if rl.is_none() && rr.is_none() {
            None
        } else {
            Some(AnyOf::new(rl, rr))
        };
        AnyOf::new(left, right)
    }

    /// Returns the left-left value if it exists.
    pub fn ll(&self) -> Option<&LL> {
        self.left()?.left()
    }

    /// Returns the left-right value if it exists.
    pub fn lr(&self) -> Option<&LR> {
        self.left()?.right()
    }

    /// Returns the right-left value if it exists.
    pub fn rl(&self) -> Option<&RL> {
        self.right()?.left()
    }

    /// Returns the right-right value if it exists.
    pub fn rr(&self) -> Option<&RR> {
        self.right()?.right()
    }

    /// Returns a tuple containing the four optional values represented by this `AnyOf4`.
    ///
    /// This is useful when you need a simple `(Option<LL>, Option<LR>, Option<RL>, Option<RR>)`
    /// representation instead of nested enums.
    ///
    /// # Examples
    /// ```rust
    /// use any_of::{AnyOf4, AnyOf, Opt4};
    ///
    /// let any: AnyOf4<i32, i32, i32, i32> = AnyOf4::new_left(AnyOf::new_left(1));
    /// assert_eq!(any.opt4(), (Some(&1), None, None, None));
    /// ```
    pub fn opt4(&self) -> Opt4<&LL, &LR, &RL, &RR> {
        (self.ll(), self.lr(), self.rl(), self.rr())
    }
}

/// A type representing a combination of eight possible types.
pub type AnyOf8<LLL, LLR = LLL, LRL = LLR, LRR = LRL, RLL = LLL, RLR = LLR, RRL = LRL, RRR = LRR> =
    AnyOf<AnyOf4<LLL, LLR, LRL, LRR>, AnyOf4<RLL, RLR, RRL, RRR>>;

impl<LLL, LLR, LRL, LRR, RLL, RLR, RRL, RRR> AnyOf8<LLL, LLR, LRL, LRR, RLL, RLR, RRL, RRR> {
    /// Creates a new `AnyOf8` from eight optional values.
    ///
    /// # Examples
    /// ```rust
    /// use any_of::AnyOf8;
    ///
    /// let value = AnyOf8::new8(
    ///     Some(1),
    ///     None::<i32>, None::<i32>, None::<i32>,
    ///     None::<i32>, None::<i32>, None::<i32>, None::<i32>,
    /// );
    /// assert!(value.lll().is_some());
    /// ```
    pub fn new8(
        lll: Option<LLL>,
        llr: Option<LLR>,
        lrl: Option<LRL>,
        lrr: Option<LRR>,
        rll: Option<RLL>,
        rlr: Option<RLR>,
        rrl: Option<RRL>,
        rrr: Option<RRR>,
    ) -> Self {
        Self::from_opt8((lll, llr, lrl, lrr, rll, rlr, rrl, rrr))
    }

    /// Builds an `AnyOf8` from an [`Opt8`] tuple.
    ///
    /// # Examples
    /// ```rust
    /// use any_of::{AnyOf8, Opt8};
    ///
    /// let tuple: Opt8<i32, i32, i32, i32, i32, i32, i32, i32> =
    ///     (Some(1), None, None, None, None, None, None, None);
    /// let value = AnyOf8::from_opt8(tuple);
    /// assert_eq!(value.lll(), Some(&1));
    /// ```
    pub fn from_opt8(opt: Opt8<LLL, LLR, LRL, LRR, RLL, RLR, RRL, RRR>) -> Self {
        let (lll, llr, lrl, lrr, rll, rlr, rrl, rrr) = opt;
        let left = if [lll.is_none(), llr.is_none(), lrl.is_none(), lrr.is_none()].iter().all(|&x| x) {
            None
        } else {
            Some(AnyOf4::new4(lll, llr, lrl, lrr))
        };
        let right = if [rll.is_none(), rlr.is_none(), rrl.is_none(), rrr.is_none()].iter().all(|&x| x) {
            None
        } else {
            Some(AnyOf4::new4(rll, rlr, rrl, rrr))
        };
        AnyOf::new(left, right)
    }

    /// Returns the left-left-left value if it exists.
    pub fn lll(&self) -> Option<&LLL> {
        self.left()?.ll()
    }

    /// Returns the left-left-right value if it exists.
    pub fn llr(&self) -> Option<&LLR> {
        self.left()?.lr()
    }

    /// Returns the left-right-left value if it exists.
    pub fn lrl(&self) -> Option<&LRL> {
        self.left()?.rl()
    }

    /// Returns the left-right-right value if it exists.
    pub fn lrr(&self) -> Option<&LRR> {
        self.left()?.rr()
    }

    /// Returns the right-left-left value if it exists.
    pub fn rll(&self) -> Option<&RLL> {
        self.right()?.ll()
    }

    /// Returns the right-left-right value if it exists.
    pub fn rlr(&self) -> Option<&RLR> {
        self.right()?.lr()
    }

    /// Returns the right-right-left value if it exists.
    pub fn rrl(&self) -> Option<&RRL> {
        self.right()?.rl()
    }

    /// Returns the right-right-right value if it exists.
    pub fn rrr(&self) -> Option<&RRR> {
        self.right()?.rr()
    }

    /// Returns the eight optional values represented by this `AnyOf8` as a tuple.
    ///
    /// The returned [`Opt8`] allows easier pattern matching and conversion into
    /// other structures.
    ///
    /// # Examples
    /// ```rust
    /// use any_of::{AnyOf, AnyOf4, AnyOf8, Opt8};
    ///
    /// let any: AnyOf8<i32, i32, i32, i32, i32, i32, i32, i32> =
    ///     AnyOf8::new_left(AnyOf4::new_left(AnyOf::new_left(1)));
    /// let tuple = any.opt8();
    /// assert_eq!(tuple.0, Some(&1));
    /// ```
    pub fn opt8(&self) -> Opt8<&LLL, &LLR, &LRL, &LRR, &RLL, &RLR, &RRL, &RRR> {
        (
            self.lll(),
            self.llr(),
            self.lrl(),
            self.lrr(),
            self.rll(),
            self.rlr(),
            self.rrl(),
            self.rrr(),
        )
    }
}

/// A type representing a combination of sixteen possible types.
///
/// This type is highly complex—use cautiously.
pub type AnyOf16<
    LLLL,
    LLLR = LLLL,
    LLRL = LLLR,
    LLRR = LLRL,
    LRLL = LLLL,
    LRLR = LLLR,
    LRRL = LLRL,
    LRRR = LLRR,
    RLLL = LLLL,
    RLLR = LLLR,
    RLRL = LLRL,
    RLRR = LLRR,
    RRLL = LRLL,
    RRLR = LRLR,
    RRRL = LRRL,
    RRRR = LRRR,
> = AnyOf<
    AnyOf8<LLLL, LLLR, LLRL, LLRR, LRLL, LRLR, LRRL, LRRR>,
    AnyOf8<RLLL, RLLR, RLRL, RLRR, RRLL, RRLR, RRRL, RRRR>,
>;

impl<
        LLLL,
        LLLR,
        LLRL,
        LLRR,
        LRLL,
        LRLR,
        LRRL,
        LRRR,
        RLLL,
        RLLR,
        RLRL,
        RLRR,
        RRLL,
        RRLR,
        RRRL,
        RRRR,
    >
    AnyOf16<
        LLLL,
        LLLR,
        LLRL,
        LLRR,
        LRLL,
        LRLR,
        LRRL,
        LRRR,
        RLLL,
        RLLR,
        RLRL,
        RLRR,
        RRLL,
        RRLR,
        RRRL,
        RRRR,
    >
{
    /// Creates a new `AnyOf16` from sixteen optional values.
    ///
    /// # Examples
    /// ```rust
    /// use any_of::AnyOf16;
    ///
    /// let value = AnyOf16::new16(
    ///     Some(1), None::<i32>, None::<i32>, None::<i32>,
    ///     None::<i32>, None::<i32>, None::<i32>, None::<i32>,
    ///     None::<i32>, None::<i32>, None::<i32>, None::<i32>,
    ///     None::<i32>, None::<i32>, None::<i32>, None::<i32>,
    /// );
    /// assert!(value.llll().is_some());
    /// ```
    #[allow(clippy::too_many_arguments)]
    pub fn new16(
        llll: Option<LLLL>,
        lllr: Option<LLLR>,
        llrl: Option<LLRL>,
        llrr: Option<LLRR>,
        lrll: Option<LRLL>,
        lrlr: Option<LRLR>,
        lrrl: Option<LRRL>,
        lrrr: Option<LRRR>,
        rlll: Option<RLLL>,
        rllr: Option<RLLR>,
        rlrl: Option<RLRL>,
        rlrr: Option<RLRR>,
        rrll: Option<RRLL>,
        rrlr: Option<RRLR>,
        rrrl: Option<RRRL>,
        rrrr: Option<RRRR>,
    ) -> Self {
        Self::from_opt16((
            llll, lllr, llrl, llrr, lrll, lrlr, lrrl, lrrr,
            rlll, rllr, rlrl, rlrr, rrll, rrlr, rrrl, rrrr,
        ))
    }

    /// Builds an `AnyOf16` from an [`Opt16`] tuple.
    ///
    /// # Examples
    /// ```rust
    /// use any_of::{AnyOf16, Opt16};
    ///
    /// let tuple: Opt16<i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32,
    ///     i32, i32, i32, i32, i32> =
    ///     (Some(1), None, None, None, None, None, None, None,
    ///      None, None, None, None, None, None, None, None);
    /// let value = AnyOf16::from_opt16(tuple);
    /// assert_eq!(value.llll(), Some(&1));
    /// ```
    #[allow(clippy::too_many_arguments)]
    pub fn from_opt16(
        opt: Opt16<
            LLLL,
            LLLR,
            LLRL,
            LLRR,
            LRLL,
            LRLR,
            LRRL,
            LRRR,
            RLLL,
            RLLR,
            RLRL,
            RLRR,
            RRLL,
            RRLR,
            RRRL,
            RRRR,
        >,
    ) -> Self {
        let (
            llll, lllr, llrl, llrr, lrll, lrlr, lrrl, lrrr,
            rlll, rllr, rlrl, rlrr, rrll, rrlr, rrrl, rrrr,
        ) = opt;
        let left_empty = [
            llll.is_none(),
            lllr.is_none(),
            llrl.is_none(),
            llrr.is_none(),
            lrll.is_none(),
            lrlr.is_none(),
            lrrl.is_none(),
            lrrr.is_none(),
        ]
        .iter()
        .all(|&x| x);
        let left = if left_empty {
            None
        } else {
            Some(AnyOf8::new8(
                llll, lllr, llrl, llrr, lrll, lrlr, lrrl, lrrr,
            ))
        };
        let right_empty = [
            rlll.is_none(),
            rllr.is_none(),
            rlrl.is_none(),
            rlrr.is_none(),
            rrll.is_none(),
            rrlr.is_none(),
            rrrl.is_none(),
            rrrr.is_none(),
        ]
        .iter()
        .all(|&x| x);
        let right = if right_empty {
            None
        } else {
            Some(AnyOf8::new8(
                rlll, rllr, rlrl, rlrr, rrll, rrlr, rrrl, rrrr,
            ))
        };
        AnyOf::new(left, right)
    }

    /// Returns the left-left-left-left value if it exists.
    pub fn llll(&self) -> Option<&LLLL> {
        self.left()?.lll()
    }

    /// Returns the left-left-left-right value if it exists.
    pub fn lllr(&self) -> Option<&LLLR> {
        self.left()?.llr()
    }

    /// Returns the left-left-right-left value if it exists.
    pub fn llrl(&self) -> Option<&LLRL> {
        self.left()?.lrl()
    }

    /// Returns the left-left-right-right value if it exists.
    pub fn llrr(&self) -> Option<&LLRR> {
        self.left()?.lrr()
    }

    /// Returns the left-right-left-left value if it exists.
    pub fn lrll(&self) -> Option<&LRLL> {
        self.left()?.rll()
    }

    /// Returns the left-right-left-right value if it exists.
    pub fn lrlr(&self) -> Option<&LRLR> {
        self.left()?.rlr()
    }

    /// Returns the left-right-right-left value if it exists.
    pub fn lrrl(&self) -> Option<&LRRL> {
        self.left()?.rrl()
    }

    /// Returns the left-right-right-right value if it exists.
    pub fn lrrr(&self) -> Option<&LRRR> {
        self.left()?.rrr()
    }

    /// Returns the right-left-left-left value if it exists.
    pub fn rlll(&self) -> Option<&RLLL> {
        self.right()?.lll()
    }

    /// Returns the right-left-left-right value if it exists.
    pub fn rllr(&self) -> Option<&RLLR> {
        self.right()?.llr()
    }

    /// Returns the right-left-right-left value if it exists.
    pub fn rlrl(&self) -> Option<&RLRL> {
        self.right()?.lrl()
    }

    /// Returns the right-left-right-right value if it exists.
    pub fn rlrr(&self) -> Option<&RLRR> {
        self.right()?.lrr()
    }

    /// Returns the right-right-left-left value if it exists.
    pub fn rrll(&self) -> Option<&RRLL> {
        self.right()?.rll()
    }

    /// Returns the right-right-left-right value if it exists.
    pub fn rrlr(&self) -> Option<&RRLR> {
        self.right()?.rlr()
    }

    /// Returns the right-right-right-left value if it exists.
    pub fn rrrl(&self) -> Option<&RRRL> {
        self.right()?.rrl()
    }

    /// Returns the right-right-right-right value if it exists.
    pub fn rrrr(&self) -> Option<&RRRR> {
        self.right()?.rrr()
    }

    #[allow(clippy::type_complexity)]
    /// Returns all sixteen optional values represented by this `AnyOf16`.
    ///
    /// This method provides a direct mapping to the [`Opt16`] type for easier
    /// inspection or conversion.
    ///
    /// # Examples
    /// ```rust
    /// use any_of::{AnyOf, AnyOf4, AnyOf8, AnyOf16, Opt16};
    ///
    /// let any: AnyOf16<i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32> =
    ///     AnyOf16::new_left(AnyOf8::new_left(AnyOf4::new_left(AnyOf::new_left(1))));
    /// let tuple = any.opt16();
    /// assert_eq!(tuple.0, Some(&1));
    /// ```
    pub fn opt16(
        &self,
    ) -> Opt16<
        &LLLL,
        &LLLR,
        &LLRL,
        &LLRR,
        &LRLL,
        &LRLR,
        &LRRL,
        &LRRR,
        &RLLL,
        &RLLR,
        &RLRL,
        &RLRR,
        &RRLL,
        &RRLR,
        &RRRL,
        &RRRR,
    > {
        (
            self.llll(),
            self.lllr(),
            self.llrl(),
            self.llrr(),
            self.lrll(),
            self.lrlr(),
            self.lrrl(),
            self.lrrr(),
            self.rlll(),
            self.rllr(),
            self.rlrl(),
            self.rlrr(),
            self.rrll(),
            self.rrlr(),
            self.rrrl(),
            self.rrrr(),
        )
    }
}