aboutsummaryrefslogtreecommitdiff
path: root/mingling_core/src/program/error.rs
blob: 822e4297e5621b28b21a3e8cc01e7df86fb5e386 (plain) (blame)
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
use std::any::Any;
use std::fmt;

/// Error type returned when a panic occurs during execution.
pub struct ProgramPanic {
    pub payload: Box<dyn Any + Send>,
}

impl fmt::Display for ProgramPanic {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if let Some(s) = self.payload.downcast_ref::<&str>() {
            write!(f, "{s}")
        } else if let Some(s) = self.payload.downcast_ref::<String>() {
            write!(f, "{s}")
        } else {
            write!(f, "")
        }
    }
}

impl ProgramPanic {
    #[must_use]
    pub fn new(payload: Box<dyn Any + Send>) -> Self {
        ProgramPanic { payload }
    }
}

impl fmt::Debug for ProgramPanic {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{:?}", self.payload)
    }
}