lazy_errors 0.10.1

Effortlessly create, group, and nest arbitrary errors, and defer error handling ergonomically.
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
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
use core::fmt::{self, Debug, Display};

use alloc::{
    boxed::Box,
    string::{String, ToString},
    vec::Vec,
};

use crate::{
    err,
    error::{self, Location},
    Error, StashedResult,
};

/// Something to push (“stash”) errors into.
///
/// This trait is implemented by [`ErrorStash`] and [`StashWithErrors`]
/// and serves to deduplicate internal logic that needs to work with
/// either of these types.
pub trait ErrorSink<E, I>
where
    E: Into<I>,
{
    /// Appends an error to this list of errors.
    fn stash(&mut self, error: E) -> &mut StashWithErrors<I>;
}

/// Something to read errors from.
///
/// This trait is implemented by [`ErrorStash`] and [`StashWithErrors`]
/// and serves to deduplicate internal logic that needs to work with
/// either of these types.
pub trait ErrorSource<I> {
    /// Returns all errors that have been added to this list so far.
    fn errors(&self) -> &[I];
}

/// Something that is/wraps a mutable, empty or non-empty list of errors,
/// and can be forced to contain at least one error.
///
/// This trait is implemented by [`ErrorStash`] and [`StashWithErrors`]
/// and serves to deduplicate internal logic that needs to work with
/// either of these types.
///
/// Since [`enforce_errors`] returns a `&mut StashWithErrors`,
/// this trait is trivially implemented by `StashWithErrors`.
/// It's main purpose, however, is to “coerce” a `&mut ErrorStash`
/// (which is either empty or wraps a `StashWithErrors`)
/// into a `&mut StashWithErrors`.
/// When deduplicating internal implementation details of this crate,
/// we ran into some cases where we know that a given `ErrorStash`
/// won't be empty, but the type system doesn't.
/// While it may be tempting to use [`ErrorStash::ok`] instead,
/// that method returns [`StashedResult<(), _>`]
/// when what we need may be `StashedResult<T, _>`.
/// In those cases we usually don't have such a generic `T` value
/// and can't create it either.
/// While using `unreachable!()` for `T` would be possible,
/// using [`enforce_errors`] instead ensures that the crate won't panic.
///
/// This trait should _never_ be made part of the crate's API.
///
/// [`enforce_errors`]: Self::enforce_errors
pub trait EnforceErrors<I> {
    /// If this list of errors is non-empty,
    /// coerces `&mut self` to [`&mut StashWithErrors`],
    /// otherwise an internal error will be added to the list first,
    /// ensuring that it won't be empty anymore.
    ///
    /// [`&mut StashWithErrors`]: StashWithErrors
    fn enforce_errors(&mut self) -> &mut StashWithErrors<I>;
}

/// A builder for [`Error`] that keeps a list of errors
/// which may still be empty, along with a message that summarizes
/// all errors that end up in the list.
///
/// The generic type parameter `F` is a function or closure that
/// will create the error summary message lazily.
/// It will be called when the first error is added.
/// The generic type parameter `M` is the result returned from `F`,
/// i.e. the type of the error summary message itself.
/// The generic type parameter `I` is the
/// [_inner error type_ of `Error`](Error#inner-error-type-i).
///
/// Essentially, this type is a builder for something similar to
/// `Result<(), Vec<Error>>`. Errors can be added by calling
/// [`push`] or by calling [`or_stash`] on `Result`.
/// When you're done collecting the errors, the [`ErrorStash`] can be
/// transformed into `Result<(), Error>` (via [`From`]/[`Into`]),
/// where [`Error`] basically wraps a `Vec<E>`
/// along with a message that summarizes all errors in that list.
///
/// ```
/// # use lazy_errors::doctest_line_num_helper as replace_line_numbers;
/// #[cfg(any(feature = "rust-v1.81", feature = "std"))]
/// use lazy_errors::prelude::*;
///
/// #[cfg(not(any(feature = "rust-v1.81", feature = "std")))]
/// use lazy_errors::surrogate_error_trait::prelude::*;
///
/// let errs = ErrorStash::new(|| "Something went wrong");
/// assert_eq!(&format!("{errs}"), "Stash of 0 errors currently");
/// let r: Result<(), Error> = errs.into();
/// assert!(r.is_ok());
///
/// let mut errs = ErrorStash::new(|| "Something went wrong");
/// errs.push("This is an error message");
/// assert_eq!(&format!("{errs}"), "Stash of 1 errors currently");
///
/// errs.push("Yet another error message");
/// assert_eq!(&format!("{errs}"), "Stash of 2 errors currently");
///
/// let r: Result<(), Error> = errs.into();
/// let err = r.unwrap_err();
///
/// assert_eq!(&format!("{err}"), "Something went wrong (2 errors)");
///
/// let printed = format!("{err:#}");
/// let printed = replace_line_numbers(&printed);
/// assert_eq!(printed, indoc::indoc! {"
///     Something went wrong
///     - This is an error message
///       at src/stash.rs:1234:56
///     - Yet another error message
///       at src/stash.rs:1234:56"});
/// ```
#[cfg_attr(
    feature = "eyre",
    doc = r##"

There's also [`IntoEyreResult`](crate::IntoEyreResult)
which performs a (lossy) conversion to
[`eyre::Result`](eyre::Result).

 "##
)]
/// If you do not want to create an empty [`ErrorStash`] before adding errors,
/// you can use [`or_create_stash`] which will
/// create a [`StashWithErrors`] when an error actually occurs.
///
/// [`or_stash`]: crate::OrStash::or_stash
/// [`or_create_stash`]: crate::OrCreateStash::or_create_stash
/// [`push`]: Self::push
pub enum ErrorStash<F, M, I>
where
    F: FnOnce() -> M,
    M: Display,
{
    Empty(F),
    WithErrors(StashWithErrors<I>),
}

/// A builder for [`Error`] that keeps a list of one or more errors,
/// along with a message that summarizes all errors that end up in the list.
///
/// The generic type parameter `I` is the
/// [_inner error type_ of `Error`](Error#inner-error-type-i).
///
/// This type is similar to [`ErrorStash`] except that an [`ErrorStash`]
/// may be empty. Since [`StashWithErrors`] contains at least one error,
/// guaranteed by the type system at compile time, this type implements
/// `Into<Error>`.
#[cfg_attr(
    feature = "eyre",
    doc = r##"

There's also [`IntoEyreReport`](crate::IntoEyreReport)
which performs a (lossy) conversion to
[`eyre::Report`](eyre::Report).
"##
)]
#[derive(Debug)]
pub struct StashWithErrors<I> {
    summary:   Box<str>,
    errors:    Vec<I>,
    locations: Vec<Location>,
}

impl<F, M, I> Debug for ErrorStash<F, M, I>
where
    F: FnOnce() -> M,
    M: Display,
    I: Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Empty(_) => write!(f, "ErrorStash(Empty)"),
            Self::WithErrors(errs) => {
                write!(f, "ErrorStash(")?;
                Debug::fmt(errs, f)?;
                write!(f, ")")?;
                Ok(())
            }
        }
    }
}

impl<F, M, I> Display for ErrorStash<F, M, I>
where
    F: FnOnce() -> M,
    M: Display,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Empty(_) => display::<I>(f, &[]),
            Self::WithErrors(errs) => Display::fmt(errs, f),
        }
    }
}

impl<I> Display for StashWithErrors<I> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        display(f, self.errors())
    }
}

impl<F, M, I> ErrorSource<I> for ErrorStash<F, M, I>
where
    F: FnOnce() -> M,
    M: Display,
{
    fn errors(&self) -> &[I] {
        self.errors()
    }
}

impl<I> ErrorSource<I> for StashWithErrors<I> {
    fn errors(&self) -> &[I] {
        self.errors()
    }
}

impl<E, F, M, I> ErrorSink<E, I> for ErrorStash<F, M, I>
where
    E: Into<I>,
    F: FnOnce() -> M,
    M: Display,
{
    #[track_caller]
    fn stash(&mut self, err: E) -> &mut StashWithErrors<I> {
        self.push(err)
    }
}

impl<E, I> ErrorSink<E, I> for StashWithErrors<I>
where
    E: Into<I>,
{
    #[track_caller]
    fn stash(&mut self, err: E) -> &mut StashWithErrors<I> {
        self.push(err)
    }
}

impl<F, M, I> EnforceErrors<I> for ErrorStash<F, M, I>
where
    F: FnOnce() -> M,
    M: Display,
    Error<I>: Into<I>,
{
    #[track_caller]
    fn enforce_errors(&mut self) -> &mut StashWithErrors<I> {
        match self {
            ErrorStash::Empty(_) => self.stash(err!("INTERNAL ERROR")),
            ErrorStash::WithErrors(stash) => stash,
        }
    }
}

impl<I> EnforceErrors<I> for StashWithErrors<I>
where
    Error<I>: Into<I>,
{
    fn enforce_errors(&mut self) -> &mut StashWithErrors<I> {
        self
    }
}

impl<F, M, I> From<ErrorStash<F, M, I>> for Result<(), Error<I>>
where
    F: FnOnce() -> M,
    M: Display,
{
    fn from(stash: ErrorStash<F, M, I>) -> Self {
        match stash {
            ErrorStash::Empty(_) => Ok(()),
            ErrorStash::WithErrors(stash) => Err(stash.into()),
        }
    }
}

impl<I> From<StashWithErrors<I>> for Error<I> {
    fn from(stash: StashWithErrors<I>) -> Self {
        Error::from_stash(stash.summary, stash.errors, stash.locations)
    }
}

impl<F, M, I> ErrorStash<F, M, I>
where
    F: FnOnce() -> M,
    M: Display,
{
    /// Creates a new [`ErrorStash`] with a “lazy” error summary message
    /// that will be evaluated when the first error (if any) is added
    /// to the stash.
    pub fn new(f: F) -> Self {
        Self::Empty(f)
    }

    /// Adds an error to this stash.
    ///
    /// Since the stash is guaranteed to be non-empty afterwards, this method
    /// returns a mutable reference to the inner [`StashWithErrors`].
    /// If you need to get that [`StashWithErrors`] by value,
    /// you can call [`push_and_convert`](Self::push_and_convert) instead.
    #[track_caller]
    pub fn push<E>(&mut self, err: E) -> &mut StashWithErrors<I>
    where
        E: Into<I>,
    {
        // We need to move out of `&mut self`
        // because we want to call `f()` which is `FnOnce()`.

        let mut swap = Self::WithErrors(StashWithErrors {
            summary:   String::new().into_boxed_str(),
            errors:    vec![],
            locations: vec![],
        });

        core::mem::swap(self, &mut swap);
        *self = ErrorStash::WithErrors(swap.push_and_convert(err));
        match self {
            ErrorStash::Empty(_) => unreachable!(),
            ErrorStash::WithErrors(stash_with_errors) => stash_with_errors,
        }
    }

    /// Adds an error to this stash,
    /// consumes `self`, and returns the inner [`StashWithErrors`] by value.
    ///
    /// Usually, you'd want to call [`push`](Self::push) instead,
    /// which takes a `&mut self` instead of `self`.
    /// However, `push_and_convert` is more useful in some cases,
    /// for example if you want to return from a function
    /// after pushing a final error:
    ///
    /// ```
    /// # use lazy_errors::doctest_line_num_helper as replace_line_numbers;
    /// #[cfg(any(feature = "rust-v1.81", feature = "std"))]
    /// use lazy_errors::{prelude::*, Result};
    ///
    /// #[cfg(not(any(feature = "rust-v1.81", feature = "std")))]
    /// use lazy_errors::surrogate_error_trait::{prelude::*, Result};
    ///
    /// fn check(bytes: &[u8]) -> Result<()> {
    ///     let mut errs = ErrorStash::new(|| "Something went wrong");
    ///
    ///     // ... Code that may or may not have added errors to `errs` ...
    ///
    ///     match bytes {
    ///         [] => Ok(()),
    ///         [42] => Ok(()),
    ///         [1, 3, 7] => Ok(()),
    ///         _ => {
    ///             let msg = format!("Invalid bytes: {bytes:?}");
    ///             let errs: StashWithErrors = errs.push_and_convert(msg);
    ///             let errs: Error = errs.into();
    ///             Err(errs)
    ///         }
    ///     }
    /// }
    /// ```
    #[track_caller]
    pub fn push_and_convert<E>(self, err: E) -> StashWithErrors<I>
    where
        E: Into<I>,
    {
        match self {
            ErrorStash::Empty(f) => StashWithErrors::from(f(), err),
            ErrorStash::WithErrors(mut stash) => {
                stash.push(err);
                stash
            }
        }
    }

    /// Returns `true` if the stash is empty.
    ///
    /// ```
    /// #[cfg(any(feature = "rust-v1.81", feature = "std"))]
    /// use lazy_errors::prelude::*;
    ///
    /// #[cfg(not(any(feature = "rust-v1.81", feature = "std")))]
    /// use lazy_errors::surrogate_error_trait::prelude::*;
    ///
    /// let mut errs = ErrorStash::new(|| "Summary message");
    /// assert!(errs.is_empty());
    ///
    /// errs.push("First error");
    /// assert!(!errs.is_empty());
    /// ```
    pub fn is_empty(&self) -> bool {
        match self {
            ErrorStash::Empty(_) => true,
            ErrorStash::WithErrors(_) => false,
        }
    }

    /// Returns all errors that have been put into this stash so far.
    ///
    /// ```
    /// type ErrorStash<F, M> = lazy_errors::ErrorStash<F, M, i32>;
    ///
    /// let mut errs = ErrorStash::new(|| "Summary message");
    /// assert_eq!(errs.errors(), &[]);
    ///
    /// errs.push(42);
    /// errs.push(-1);
    /// errs.push(1337);
    /// assert_eq!(errs.errors(), &[42, -1, 1337]);
    /// ```
    ///
    /// Note that this method only returns errors that have been
    /// put into this stash _directly_.
    /// Each of those errors thus may have been created from
    /// an [`ErrorStash`](crate::ErrorStash),
    /// which stored another level of errors.
    /// Such transitive children will _not_ be returned from this method.
    pub fn errors(&self) -> &[I] {
        match self {
            ErrorStash::Empty(_) => &[],
            ErrorStash::WithErrors(stash) => stash.errors(),
        }
    }

    /// Returns `Ok(())` if the stash is empty,
    /// otherwise returns [`StashedResult::Err`].
    ///
    /// This method basically allows you to use the `?` operator
    /// (currently implemented in the form of the [`try2!`] macro)
    /// on _all_ prior errors simultaneously.
    ///
    /// ```
    /// use std::collections::HashMap;
    ///
    /// #[cfg(any(feature = "rust-v1.81", feature = "std"))]
    /// use lazy_errors::{prelude::*, Result};
    ///
    /// #[cfg(not(any(feature = "rust-v1.81", feature = "std")))]
    /// use lazy_errors::surrogate_error_trait::{prelude::*, Result};
    ///
    /// // Always parses two configs, even if the first one contains an error.
    /// // All errors or groups of errors returned from this function
    /// // share the same error summary message.
    /// fn configure(
    ///     path_to_config_a: &str,
    ///     path_to_config_b: &str,
    /// ) -> Result<HashMap<String, String>> {
    ///     let mut errs = ErrorStash::new(|| "Invalid app config");
    ///
    ///     let config_a = parse_config(path_to_config_a)
    ///         .or_stash(&mut errs)
    ///         .ok();
    ///
    ///     let config_b = parse_config(path_to_config_b)
    ///         .or_stash(&mut errs)
    ///         .ok();
    ///
    ///     // If there was any error, bail out now.
    ///     // If there were no errors, both configs can be unwrapped.
    ///     try2!(errs.ok());
    ///     let config_a = config_a.unwrap();
    ///     let config_b = config_b.unwrap();
    ///
    ///     Ok(try2!(merge(config_a, config_b).or_stash(&mut errs)))
    /// }
    ///
    /// fn parse_config(path: &str) -> Result<HashMap<String, String>> {
    ///     if path == "bad.cfg" {
    ///         Err(err!("Config file contains an error"))
    ///     } else {
    ///         // ...
    ///         Ok(HashMap::new())
    ///     }
    /// }
    ///
    /// fn merge(
    ///     _a: HashMap<String, String>,
    ///     _b: HashMap<String, String>,
    /// ) -> Result<HashMap<String, String>> {
    ///     // ...
    ///     Ok(HashMap::new())
    /// }
    ///
    /// let err = configure("bad.cfg", "bad.cfg").unwrap_err();
    /// assert_eq!(err.children().len(), 2);
    ///
    /// let err = configure("good.cfg", "bad.cfg").unwrap_err();
    /// assert_eq!(err.children().len(), 1);
    ///
    /// assert!(configure("good.cfg", "good.cfg").is_ok());
    /// ```
    ///
    /// This method is similar to [`ErrorStash::into_result`] or
    /// `ErrorStash::into`. As opposed to these other methods, however,
    /// [`ok`] does not consume `self`. It only borrows `self` mutably.
    /// This allows you to continue adding errors later,
    /// as soon as you have dropped the [`StashedResult`]
    /// or called [`StashedResult::ok`] to discard the borrowed reference.
    ///
    /// This method enables you to place “barriers” in your code.
    /// Before the “barrier”, you can collect multiple errors.
    /// Then, at some pivotal check, you'll either return all previous errors
    /// or keep going, knowing that no errors have occurred so far.
    ///
    /// [`ErrorData::Stashed`]: crate::ErrorData::Stashed
    /// [`StashedErrors`]: crate::StashedErrors
    /// [`ok`]: Self::ok
    /// [`try2!`]: crate::try2!
    pub fn ok(&mut self) -> StashedResult<(), I> {
        match self {
            ErrorStash::Empty(_) => StashedResult::Ok(()),
            ErrorStash::WithErrors(errs) => StashedResult::Err(errs),
        }
    }

    /// Returns `Ok(())` if the stash is empty, otherwise returns an `Err`
    /// containing all errors from this stash.
    ///
    /// You can usually call `into` instead of this method.
    /// This method actually does nothing else besides specifying
    /// the return type. In some cases, Rust cannot figure out
    /// which type you want to convert into.
    /// This method may be more readable than specifying the concrete types:
    ///
    /// ```
    /// # use core::str::FromStr;
    /// #[cfg(any(feature = "rust-v1.81", feature = "std"))]
    /// use lazy_errors::{prelude::*, Result};
    ///
    /// #[cfg(not(any(feature = "rust-v1.81", feature = "std")))]
    /// use lazy_errors::surrogate_error_trait::{prelude::*, Result};
    ///
    /// fn count_numbers(nums: &[&str]) -> Result<usize> {
    ///     let mut errs = ErrorStash::new(|| "Something wasn't a number");
    ///
    ///     for n in nums {
    ///         i32::from_str(n).or_stash(&mut errs);
    ///     }
    ///
    ///     // errs.into()?; // Does not compile
    ///     // Result::<()>::from(errs)?; // Works but is hard to read and type
    ///     errs.into_result()?; // Much nicer
    ///
    ///     Ok(nums.len())
    /// }
    ///
    /// assert_eq!(count_numbers(&["42"]).unwrap(), 1);
    /// assert!(count_numbers(&["42", ""]).is_err());
    /// ```
    ///
    /// In case there was at least one error in this stash,
    /// the [`Error`] will hold the [`ErrorData::Stashed`] variant
    /// which contains a [`StashedErrors`] object.
    ///
    /// [`ErrorData::Stashed`]: crate::ErrorData::Stashed
    /// [`StashedErrors`]: crate::StashedErrors
    pub fn into_result(self) -> Result<(), Error<I>> {
        self.into()
    }
}

impl<I> StashWithErrors<I> {
    /// Creates a [`StashWithErrors`] that contains a single error so far;
    /// the supplied message shall summarize
    /// that error and all errors that will be added later.
    #[track_caller]
    pub fn from<M, E>(summary: M, error: E) -> Self
    where
        M: Display,
        E: Into<I>,
    {
        Self {
            summary:   summary.to_string().into(),
            errors:    vec![error.into()],
            locations: vec![error::location()],
        }
    }

    /// Adds an error into the stash.
    #[track_caller]
    pub fn push<E>(&mut self, err: E) -> &mut StashWithErrors<I>
    where
        E: Into<I>,
    {
        self.errors.push(err.into());
        self.locations.push(error::location());
        self
    }

    /// Returns all errors that have been put into this stash so far.
    ///
    /// Note that this method only returns errors that have been
    /// put into this stash _directly_.
    /// Each of those errors thus may have been created from
    /// an [`ErrorStash`](crate::ErrorStash),
    /// which stored another level of errors.
    /// Such transitive children will _not_ be returned from this method.
    pub fn errors(&self) -> &[I] {
        &self.errors
    }

    /// ⚠️ Do not use this method! ⚠️
    ///
    /// Returns a [`StashWithErrors`] that's identical to `self`
    /// by replacing the contents of `&mut self` with dummy values.
    ///
    /// Do not call this method. It must only be used for internal purposes.
    /// This method is basically a wrapper for [`core::mem::swap`]
    /// that also handles the `I` type parameter.
    ///
    /// For internal usage only. Even then: Take care when using this method.
    /// Even if you have a `&mut`, you or your callers may not expect
    /// the value to change “that much”.
    /// This method should only be used by the [`try2!`] macro.
    /// When the `Try` trait is stabilized, we can implement it
    /// and remove the [`try2!`] macro and this method.
    ///
    /// ⚠️ Do not use this method! ⚠️
    ///
    /// [`try2!`]: crate::try2!
    #[doc(hidden)]
    pub fn take(&mut self) -> Self {
        // The dummy we'll be swapping into `self` should never “leak”,
        // if this type is used correctly.
        // But better print a specific error message in case it does.
        const WARNING: &str = "Internal error: Error info cleared by take()";

        let mut swap_with = Self {
            summary:   WARNING.to_string().into_boxed_str(),
            errors:    vec![],
            locations: vec![],
        };

        core::mem::swap(&mut swap_with, self);
        swap_with
    }
}

fn display<I>(f: &mut fmt::Formatter<'_>, errors: &[I]) -> fmt::Result {
    let count = errors.len();
    write!(f, "Stash of {count} errors currently")
}

#[cfg(test)]
mod tests {
    #[cfg(any(feature = "rust-v1.81", feature = "std"))]
    use crate::prelude::*;

    #[cfg(not(any(feature = "rust-v1.81", feature = "std")))]
    use crate::surrogate_error_trait::prelude::*;

    use crate::stash::EnforceErrors;

    #[test]
    fn stash_debug_fmt_when_empty() {
        let errs = ErrorStash::new(|| "Mock message");
        assert_eq!(format!("{errs:?}"), "ErrorStash(Empty)");
    }

    #[test]
    fn stash_debug_fmt_with_errors() {
        let mut errs = ErrorStash::new(|| "Mock message");
        errs.push("First error");
        errs.push(Error::from_message("Second error"));

        let msg = format!("{errs:?}");
        dbg!(&msg);

        assert!(msg.contains("ErrorStash"));
        assert!(msg.contains("StashWithErrors"));

        assert!(msg.contains("First error"));
        assert!(msg.contains("Second error"));

        assert!(msg.contains("lazy_errors"));
        assert!(msg.contains("stash.rs"));
    }

    #[cfg(feature = "eyre")]
    #[test]
    fn stash_debug_fmt_with_errors_eyre() {
        let mut errs = ErrorStash::new(|| "Mock message");

        errs.push(eyre::eyre!("Eyre error"));

        let msg = format!("{errs:?}");
        dbg!(&msg);

        assert!(msg.contains("Eyre error"));
        assert!(msg.contains("lazy_errors"));
        assert!(msg.contains("stash.rs"));
    }

    #[test]
    fn error_stash_enforce_errors_modifies_original_stash() {
        let mut error_stash = ErrorStash::new(|| "Failure");
        assert_eq!(error_stash.errors().len(), 0);

        {
            let stash_with_errors = error_stash.enforce_errors();
            assert_eq!(stash_with_errors.errors().len(), 1);
            // Drop the mutable borrow
        }

        assert_eq!(error_stash.errors().len(), 1);

        let err = error_stash.into_result().unwrap_err();
        let msg = format!("{err}");
        assert_eq!("Failure: INTERNAL ERROR", &msg);
    }

    #[test]
    fn error_stash_enforce_errors_modifies_only_once() {
        let mut error_stash = ErrorStash::new(|| "Failure");
        assert_eq!(error_stash.errors().len(), 0);

        error_stash.enforce_errors();
        assert_eq!(error_stash.errors().len(), 1);

        {
            let stash_with_errors = error_stash.enforce_errors();
            assert_eq!(stash_with_errors.errors().len(), 1);
            // Drop the mutable borrow
        }

        assert_eq!(error_stash.errors().len(), 1);

        let err = error_stash.into_result().unwrap_err();
        let msg = format!("{err}");
        assert_eq!("Failure: INTERNAL ERROR", &msg);
    }

    #[test]
    fn error_stash_enforce_errors_does_not_modify_if_nonempty() {
        let mut error_stash = ErrorStash::new(|| "Failure");
        assert_eq!(error_stash.errors().len(), 0);

        error_stash.push("External error");
        assert_eq!(error_stash.errors().len(), 1);

        error_stash.enforce_errors();
        assert_eq!(error_stash.errors().len(), 1);

        {
            let stash_with_errors = error_stash.enforce_errors();
            assert_eq!(stash_with_errors.errors().len(), 1);
            // Drop the mutable borrow
        }

        assert_eq!(error_stash.errors().len(), 1);

        let err = error_stash.into_result().unwrap_err();
        let msg = format!("{err}");
        assert_eq!("Failure: External error", &msg);
    }

    #[test]
    fn stash_with_errors_enforce_errors_modifies_only_once() {
        let mut error_stash = ErrorStash::new(|| "Failure");
        assert_eq!(error_stash.errors().len(), 0);

        error_stash.enforce_errors();
        assert_eq!(error_stash.errors().len(), 1);

        {
            let stash_with_errors = error_stash.enforce_errors();
            assert_eq!(stash_with_errors.errors().len(), 1);

            stash_with_errors.enforce_errors();
            assert_eq!(stash_with_errors.errors().len(), 1);

            // Drop the mutable borrow
        }

        assert_eq!(error_stash.errors().len(), 1);
    }

    #[test]
    fn stash_with_errors_enforce_errors_does_not_modify() {
        let mut swe = StashWithErrors::from("Failure", "External error");
        assert_eq!(swe.errors().len(), 1);

        swe.enforce_errors();
        assert_eq!(swe.errors().len(), 1);

        let err: Error = swe.into();
        let msg = format!("{err}");
        assert_eq!("Failure: External error", &msg);
    }
}