Skip to main content

datex_core/runtime/execution/
errors.rs

1use crate::{
2    dxb_parser::body::DXBParserError,
3    network::com_hub::network_response::ResponseError,
4    runtime::execution::execution_loop::state::ExecutionLoopState,
5    shared_values::shared_container::{
6        AccessError, AssignmentError, SharedValueCreationError,
7    },
8    types::error::IllegalTypeError,
9    values::value_container::{ValueContainer, ValueError},
10};
11use core::fmt::Display;
12
13use crate::prelude::*;
14#[derive(Debug, Clone, PartialEq, Eq)]
15pub enum InvalidProgramError {
16    // any unterminated sequence, e.g. missing key in key-value pair
17    UnterminatedSequence,
18    MissingRemoteExecutionReceiver,
19    ExpectedTypeValue,
20    ExpectedValue,
21    ExpectedInstruction,
22    ExpectedRegularInstruction,
23    ExpectedTypeInstruction,
24}
25
26impl Display for InvalidProgramError {
27    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
28        match self {
29            InvalidProgramError::UnterminatedSequence => {
30                core::write!(f, "Unterminated sequence")
31            }
32            InvalidProgramError::MissingRemoteExecutionReceiver => {
33                core::write!(f, "Missing remote execution receiver")
34            }
35            InvalidProgramError::ExpectedTypeValue => {
36                core::write!(f, "Expected a type value")
37            }
38            InvalidProgramError::ExpectedValue => {
39                core::write!(f, "Expected a value")
40            }
41            InvalidProgramError::ExpectedRegularInstruction => {
42                core::write!(f, "Expected a regular instruction")
43            }
44            InvalidProgramError::ExpectedTypeInstruction => {
45                core::write!(f, "Expected a type instruction")
46            }
47            InvalidProgramError::ExpectedInstruction => {
48                core::write!(f, "Expected an instruction")
49            }
50        }
51    }
52}
53
54#[derive(Debug)]
55pub enum ExecutionError {
56    DXBParserError(DXBParserError),
57    ValueError(ValueError),
58    InvalidProgram(InvalidProgramError),
59    AccessError(AccessError),
60    Unknown,
61    NotImplemented(String),
62    SlotNotAllocated(u32),
63    SlotNotInitialized(u32),
64    RequiresAsyncExecution,
65    ResponseError(ResponseError),
66    IllegalTypeError(IllegalTypeError),
67    ReferenceNotFound,
68    InvalidUnbox,
69    InvalidTypeCast,
70    ExpectedTypeValue,
71    ReferenceToNonSharedValue,
72    MutableReferenceToNonMutableValue,
73    AssignmentError(AssignmentError),
74    ReferenceCreationError(SharedValueCreationError),
75    IntermediateResultWithState(
76        Option<ValueContainer>,
77        Option<ExecutionLoopState>,
78    ),
79    InvalidApply,
80}
81impl From<SharedValueCreationError> for ExecutionError {
82    fn from(error: SharedValueCreationError) -> Self {
83        ExecutionError::ReferenceCreationError(error)
84    }
85}
86
87impl From<AccessError> for ExecutionError {
88    fn from(error: AccessError) -> Self {
89        ExecutionError::AccessError(error)
90    }
91}
92
93impl From<DXBParserError> for ExecutionError {
94    fn from(error: DXBParserError) -> Self {
95        ExecutionError::DXBParserError(error)
96    }
97}
98
99impl From<ValueError> for ExecutionError {
100    fn from(error: ValueError) -> Self {
101        ExecutionError::ValueError(error)
102    }
103}
104
105impl From<IllegalTypeError> for ExecutionError {
106    fn from(error: IllegalTypeError) -> Self {
107        ExecutionError::IllegalTypeError(error)
108    }
109}
110
111impl From<InvalidProgramError> for ExecutionError {
112    fn from(error: InvalidProgramError) -> Self {
113        ExecutionError::InvalidProgram(error)
114    }
115}
116
117impl From<ResponseError> for ExecutionError {
118    fn from(error: ResponseError) -> Self {
119        ExecutionError::ResponseError(error)
120    }
121}
122
123impl From<AssignmentError> for ExecutionError {
124    fn from(error: AssignmentError) -> Self {
125        ExecutionError::AssignmentError(error)
126    }
127}
128
129impl Display for ExecutionError {
130    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
131        match self {
132            ExecutionError::ReferenceCreationError(err) => {
133                core::write!(f, "Reference from value container error: {err}")
134            }
135            ExecutionError::ReferenceNotFound => {
136                core::write!(f, "Reference not found")
137            }
138            ExecutionError::DXBParserError(err) => {
139                core::write!(f, "Parser error: {err}")
140            }
141            ExecutionError::Unknown => {
142                core::write!(f, "Unknown execution error")
143            }
144            ExecutionError::ValueError(err) => {
145                core::write!(f, "Value error: {err}")
146            }
147            ExecutionError::InvalidProgram(err) => {
148                core::write!(f, "Invalid program error: {err}")
149            }
150            ExecutionError::NotImplemented(msg) => {
151                core::write!(f, "Not implemented: {msg}")
152            }
153            ExecutionError::SlotNotAllocated(address) => {
154                core::write!(
155                    f,
156                    "Tried to access unallocated slot at address {address}"
157                )
158            }
159            ExecutionError::SlotNotInitialized(address) => {
160                core::write!(
161                    f,
162                    "Tried to access uninitialized slot at address {address}"
163                )
164            }
165            ExecutionError::RequiresAsyncExecution => {
166                core::write!(f, "Program must be executed asynchronously")
167            }
168            ExecutionError::ResponseError(err) => {
169                core::write!(f, "Response error: {err}")
170            }
171            ExecutionError::IllegalTypeError(err) => {
172                core::write!(f, "Illegal type: {err}")
173            }
174            ExecutionError::InvalidUnbox => {
175                core::write!(f, "Tried to unbox a non-reference value")
176            }
177            ExecutionError::AssignmentError(err) => {
178                core::write!(f, "Assignment error: {err}")
179            }
180            ExecutionError::InvalidTypeCast => {
181                core::write!(f, "Invalid type cast")
182            }
183            ExecutionError::ExpectedTypeValue => {
184                core::write!(f, "Expected a type value")
185            }
186            ExecutionError::AccessError(err) => {
187                core::write!(f, "Access error: {err}")
188            }
189            ExecutionError::IntermediateResultWithState(
190                value_opt,
191                state_opt,
192            ) => {
193                core::write!(
194                    f,
195                    "Execution produced an intermediate result: {:?} with state: {:?}",
196                    value_opt,
197                    state_opt
198                )
199            }
200            ExecutionError::InvalidApply => {
201                core::write!(f, "Invalid apply operation")
202            }
203            ExecutionError::ReferenceToNonSharedValue => {
204                core::write!(
205                    f,
206                    "Tried to create a reference to a non-shared value"
207                )
208            }
209            ExecutionError::MutableReferenceToNonMutableValue => {
210                core::write!(
211                    f,
212                    "Tried to create a mutable reference to a non-mutable value"
213                )
214            }
215        }
216    }
217}