error2/
extract.rs

1use crate::{Error2, NextError};
2
3pub fn extract_error_stack(e: &dyn Error2) -> Box<[Box<str>]> {
4    fn extract_single<'a>(stack: &mut Vec<Box<str>>, e: &'a dyn Error2) -> NextError<'a> {
5        let (locations, next_error) = e.entry();
6
7        for location in locations.inner().iter().rev() {
8            let idx = stack.len();
9            stack.push(format!("{idx}: {e}, at {location}").into_boxed_str());
10        }
11
12        next_error
13    }
14
15    let mut stack = Vec::with_capacity(16);
16
17    let mut next = extract_single(&mut stack, e);
18
19    loop {
20        match next {
21            NextError::Err2(e) => {
22                next = extract_single(&mut stack, e);
23                continue;
24            }
25            NextError::Std(e) => {
26                let idx = stack.len();
27                stack.push(format!("{idx}: {e}").into_boxed_str());
28                break;
29            }
30            NextError::None => break,
31        }
32    }
33
34    stack.into_boxed_slice()
35}