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
use super::{ContextExt, CustomError, Error};
use alloc::borrow::Cow;
#[cfg(not(feature = "std"))]
use alloc::{boxed::Box, format, string::ToString, vec::Vec};
use core::fmt::{self, Write};

#[cfg(feature = "verbose-backtrace")]
const BOLD_OPEN: &str = "\x1b[1m";
#[cfg(feature = "verbose-backtrace")]
const BOLD_CLOSE: &str = "\x1b[22m";
#[cfg(not(feature = "verbose-backtrace"))]
const BOLD_OPEN: &str = "";
#[cfg(not(feature = "verbose-backtrace"))]
const BOLD_CLOSE: &str = "";

/// An error backtrace.
#[non_exhaustive]
#[derive(Debug)]
pub struct Backtrace {
    /// The source error which caused this backtrace.
    ///
    /// This is guaranteed to not itself be a backtrace.
    pub error: Box<Error>,

    /// The frames which lead to the given error.
    ///
    /// The first frame is the innermost frame.
    pub frames: Vec<BacktraceFrame>,
}

impl Backtrace {
    /// Creates a new backtrace from a source error and a set of frames.
    ///
    /// If the source error is an [`Error::Backtrace`], the given frames are
    /// appended to that object and it is unwrapped and used instead of creating
    /// a new backtrace. This ensures that [`Backtrace::error`] is never a
    /// `Backtrace` and avoids recursion.
    #[must_use]
    pub fn new(error: Error, frames: Vec<BacktraceFrame>) -> Self {
        let mut frames = frames;
        match error {
            Error::Backtrace(mut backtrace) => {
                backtrace.frames.append(&mut frames);
                backtrace
            }
            error => Self {
                error: Box::new(error),
                frames,
            },
        }
    }

    fn fmt_no_bars(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut frames = self.frames.iter();

        if let Some(first_frame) = frames.next() {
            first_frame.display_with_message(
                f,
                &format!(
                    "{BOLD_OPEN}Error: {}{BOLD_CLOSE}\n    {}{BOLD_OPEN}{}{BOLD_CLOSE}",
                    FirstErrorFmt(&self.error),
                    if matches!(self.error.as_ref(), Error::EnumErrors { .. }) {
                        "..."
                    } else {
                        "       "
                    },
                    first_frame.message(),
                ),
                0,
            )?;

            for (i, frame) in frames.enumerate() {
                frame.display(f, i + 1)?;
            }
        }

        Ok(())
    }
}

impl ContextExt for Backtrace {
    fn with_context<Frame: Into<BacktraceFrame>>(mut self, frame: Frame) -> Self {
        self.frames.push(frame.into());
        self
    }

    #[track_caller]
    fn with_message(self, message: impl Into<Cow<'static, str>>) -> Self {
        let caller = core::panic::Location::caller();
        self.with_context(BacktraceFrame::Full {
            code: None,
            message: message.into(),
            file: caller.file(),
            line: caller.line(),
        })
    }
}

impl fmt::Display for Backtrace {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if cfg!(feature = "verbose-backtrace") {
            writeln!(
                f,
                "\n ╺━━━━━━━━━━━━━━━━━━━━┅ Backtrace ┅━━━━━━━━━━━━━━━━━━━━╸\n"
            )?;
        }

        self.fmt_no_bars(f)?;

        if cfg!(feature = "verbose-backtrace") {
            writeln!(
                f,
                "\n ╺━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╸\n"
            )?;
        }

        Ok(())
    }
}

/// A captured backtrace frame.
#[derive(Debug)]
pub enum BacktraceFrame {
    /// A standard frame.
    Full {
        /// The code at the location where the frame was generated.
        code: Option<&'static str>,

        /// The context message. This may be overridden by the error itself when
        /// full backtraces are enabled.
        message: Cow<'static, str>,

        /// The origin filename.
        file: &'static str,

        /// The origin line number.
        line: u32,
    },

    /// A message-only frame.
    Message(Cow<'static, str>),

    /// A user-specified custom error context.
    Custom(Box<dyn CustomError>),
}

impl BacktraceFrame {
    fn display_with_message(
        &self,
        f: &mut fmt::Formatter<'_>,
        message: &impl fmt::Display,
        index: usize,
    ) -> fmt::Result {
        match self {
            BacktraceFrame::Full {
                code, file, line, ..
            } => {
                writeln!(
                    f,
                    " {index}: {BOLD_OPEN}{message}{BOLD_CLOSE}\n     at {file}:{line}",
                )?;
                if let Some(code) = code {
                    write!(f, "{code}")?;
                }
                Ok(())
            }
            BacktraceFrame::Message(_) | BacktraceFrame::Custom(_) => {
                writeln!(f, " {index}: {BOLD_OPEN}{message}{BOLD_CLOSE}")
            }
        }
    }

    fn display(&self, f: &mut fmt::Formatter<'_>, index: usize) -> fmt::Result {
        self.display_with_message(f, &self.message(), index)
    }

    fn message(&self) -> Cow<'_, str> {
        match self {
            BacktraceFrame::Full { message: msg, .. } | BacktraceFrame::Message(msg) => msg.clone(),
            BacktraceFrame::Custom(context) => context.to_string().into(),
        }
    }
}

impl<T: CustomError + 'static> From<T> for BacktraceFrame {
    fn from(err: T) -> Self {
        Self::Custom(Box::new(err) as _)
    }
}

struct FirstErrorFmt<'a>(&'a Error);

impl fmt::Display for FirstErrorFmt<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self.0 {
            Error::EnumErrors {
                pos,
                variant_errors,
            } => {
                writeln!(f, "no variants matched at {pos:#x?}...{BOLD_CLOSE}")?;

                for (i, (name, err)) in variant_errors.iter().enumerate() {
                    if i != 0 {
                        writeln!(f)?;
                    }

                    writeln!(
                        f,
                        "   ╭───────────────────────┄ {name} ┄────────────────────┄"
                    )?;
                    writeln!(f, "   ┆")?;
                    write!(f, "   ┆")?;
                    write!(Indenter(f), "{}", NoBars(err))?;
                    write!(
                        f,
                        "\n   ╰─────────────────────────{}──────────────────────┄",
                        "─".repeat(name.len())
                    )?;
                }

                Ok(())
            }
            error => <Error as fmt::Display>::fmt(error, f),
        }
    }
}

struct Indenter<'a, 'b>(&'a mut fmt::Formatter<'b>);

impl Write for Indenter<'_, '_> {
    fn write_str(&mut self, s: &str) -> fmt::Result {
        let mut is_first = true;
        for line in s.split_inclusive('\n') {
            if is_first {
                is_first = false;
            } else {
                self.0.write_str("   ┆")?;
            }
            self.0.write_str(line)?;
        }

        if s.ends_with('\n') {
            self.0.write_str("   ┆")
        } else {
            Ok(())
        }
    }
}

struct NoBars<'a>(&'a Error);

impl fmt::Display for NoBars<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self.0 {
            Error::Backtrace(backtrace) => backtrace.fmt_no_bars(f),
            error => <Error as fmt::Display>::fmt(error, f),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    #[cfg_attr(coverage_nightly, coverage(off))]
    fn backtrace() {
        const ERR0: &str = "assert_failed";
        const ERR1: &str = "while doing something";
        const ERR2: &str = "then this happened";
        const ERR3: &str = "and lastly this happened";

        let error = Error::AssertFail {
            pos: 4,
            message: ERR0.to_string(),
        };

        let (line1, error) = (line!(), Err::<(), _>(error.with_message(ERR1)));
        let (line2, error) = (line!(), error.with_message(ERR2));
        let error = error.with_context(ERR3);

        if let Error::Backtrace(backtrace) = error.unwrap_err() {
            if let Error::AssertFail { pos: 4, message } = &*backtrace.error {
                assert_eq!(message, ERR0);
            } else {
                panic!("Not AssertFail")
            }

            if let [BacktraceFrame::Full {
                code: None,
                message: Cow::Borrowed(ERR1),
                file: file!(),
                line: l1,
            }, BacktraceFrame::Full {
                code: None,
                message: Cow::Borrowed(ERR2),
                file: file!(),
                line: l2,
            }, BacktraceFrame::Custom(last)] = &backtrace.frames[..]
            {
                assert_eq!(line1, *l1);
                assert_eq!(line2, *l2);
                assert_eq!(last.to_string(), ERR3);
            } else {
                panic!("Backtrace incorrect: {:?}", &backtrace.frames)
            }
        } else {
            panic!("Not a backtrace")
        }
    }
}