aboutsummaryrefslogtreecommitdiff
path: root/mingling_core/src/program/exec/error.rs
blob: 2790a7bbfdb5e265592da4dc37fbc03095065574 (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
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
use crate::error::{ChainProcessError, ProgramPanic};
use std::fmt;

#[derive(Debug)]
pub enum ProgramExecuteError {
    DispatcherNotFound,
    RendererNotFound(String),
    Panic(ProgramPanic),
    Other(String),
}

impl fmt::Display for ProgramExecuteError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ProgramExecuteError::DispatcherNotFound => write!(f, "No Dispatcher Found"),
            ProgramExecuteError::RendererNotFound(s) => {
                write!(f, "No Renderer (`{}`) Found", s)
            }
            ProgramExecuteError::Panic(p) => write!(f, "Panic: {:?}", p),
            ProgramExecuteError::Other(s) => write!(f, "Other error: {}", s),
        }
    }
}

impl std::error::Error for ProgramExecuteError {}

impl From<ProgramPanic> for ProgramExecuteError {
    fn from(value: ProgramPanic) -> Self {
        ProgramExecuteError::Panic(value)
    }
}

#[derive(Debug)]
pub enum ProgramInternalExecuteError {
    DispatcherNotFound,
    RendererNotFound(String),
    Other(String),
    IO(std::io::Error),
}

impl fmt::Display for ProgramInternalExecuteError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ProgramInternalExecuteError::DispatcherNotFound => {
                write!(f, "No Dispatcher Found")
            }
            ProgramInternalExecuteError::RendererNotFound(s) => {
                write!(f, "No Renderer (`{}`) Found", s)
            }
            ProgramInternalExecuteError::Other(s) => write!(f, "Other error: {}", s),
            ProgramInternalExecuteError::IO(e) => write!(f, "IO error: {}", e),
        }
    }
}

impl std::error::Error for ProgramInternalExecuteError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            ProgramInternalExecuteError::IO(e) => Some(e),
            _ => None,
        }
    }
}

impl From<std::io::Error> for ProgramInternalExecuteError {
    fn from(e: std::io::Error) -> Self {
        ProgramInternalExecuteError::IO(e)
    }
}

impl From<ProgramInternalExecuteError> for ProgramExecuteError {
    fn from(value: ProgramInternalExecuteError) -> Self {
        match value {
            ProgramInternalExecuteError::DispatcherNotFound => {
                ProgramExecuteError::DispatcherNotFound
            }
            ProgramInternalExecuteError::RendererNotFound(s) => {
                ProgramExecuteError::RendererNotFound(s)
            }
            ProgramInternalExecuteError::Other(s) => ProgramExecuteError::Other(s),
            ProgramInternalExecuteError::IO(e) => ProgramExecuteError::Other(format!("{}", e)),
        }
    }
}

impl From<ChainProcessError> for ProgramInternalExecuteError {
    fn from(value: ChainProcessError) -> Self {
        match value {
            ChainProcessError::Other(s) => ProgramInternalExecuteError::Other(s),
            ChainProcessError::IO(error) => ProgramInternalExecuteError::IO(error),
        }
    }
}