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

/// Errors that can occur during program execution.
///
/// This enum represents the various error conditions that may arise
/// when executing a program, including missing dispatchers/renderers,
/// panics, and other miscellaneous errors.
#[derive(Debug)]
pub enum ProgramExecuteError {
    /// No dispatcher was found to handle the program execution.
    DispatcherNotFound,

    /// No renderer was found for the given name.
    RendererNotFound(String),

    /// The program encountered a panic during execution.
    Panic(ProgramPanic),

    /// An other error occurred.
    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 (`{s}`) Found")
            }
            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)
    }
}

/// Errors that can occur during internal program execution.
///
/// This enum represents error conditions that arise specifically within
/// the internal execution pipeline of a program, including missing
/// dispatchers/renderers, I/O errors, and other miscellaneous failures.
/// These errors are typically not exposed directly to the end user but
/// are used internally and can be converted into [`ProgramExecuteError`].
#[derive(Debug)]
pub enum ProgramInternalExecuteError {
    /// No dispatcher was found to handle the program execution.
    DispatcherNotFound,

    /// No renderer was found for the given name.
    RendererNotFound(String),

    /// An other internal error occurred.
    Other(String),

    /// A single REPL execution failed
    REPLPanic(ProgramPanic),

    /// An I/O error occurred during execution.
    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 (`{s}`) Found")
            }
            ProgramInternalExecuteError::Other(s) => write!(f, "Other error: {s}"),
            ProgramInternalExecuteError::IO(e) => write!(f, "IO error: {e}"),
            ProgramInternalExecuteError::REPLPanic(panic) => {
                write!(f, "A single REPL execution failed: {panic}")
            }
        }
    }
}

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}")),
            ProgramInternalExecuteError::REPLPanic(p) => {
                ProgramExecuteError::Other(format!("A single REPL execution failed: {p}"))
            }
        }
    }
}

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