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    references::reference::{
5        AccessError, AssignmentError, ReferenceCreationError,
6    },
7    runtime::execution::execution_loop::state::ExecutionLoopState,
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    RequiresRuntime,
66    ResponseError(ResponseError),
67    IllegalTypeError(IllegalTypeError),
68    ReferenceNotFound,
69    DerefOfNonReference,
70    InvalidTypeCast,
71    ExpectedTypeValue,
72    AssignmentError(AssignmentError),
73    ReferenceFromValueContainerError(ReferenceCreationError),
74    IntermediateResultWithState(
75        Option<ValueContainer>,
76        Option<ExecutionLoopState>,
77    ),
78    InvalidApply,
79}
80impl From<ReferenceCreationError> for ExecutionError {
81    fn from(error: ReferenceCreationError) -> Self {
82        ExecutionError::ReferenceFromValueContainerError(error)
83    }
84}
85
86impl From<AccessError> for ExecutionError {
87    fn from(error: AccessError) -> Self {
88        ExecutionError::AccessError(error)
89    }
90}
91
92impl From<DXBParserError> for ExecutionError {
93    fn from(error: DXBParserError) -> Self {
94        ExecutionError::DXBParserError(error)
95    }
96}
97
98impl From<ValueError> for ExecutionError {
99    fn from(error: ValueError) -> Self {
100        ExecutionError::ValueError(error)
101    }
102}
103
104impl From<IllegalTypeError> for ExecutionError {
105    fn from(error: IllegalTypeError) -> Self {
106        ExecutionError::IllegalTypeError(error)
107    }
108}
109
110impl From<InvalidProgramError> for ExecutionError {
111    fn from(error: InvalidProgramError) -> Self {
112        ExecutionError::InvalidProgram(error)
113    }
114}
115
116impl From<ResponseError> for ExecutionError {
117    fn from(error: ResponseError) -> Self {
118        ExecutionError::ResponseError(error)
119    }
120}
121
122impl From<AssignmentError> for ExecutionError {
123    fn from(error: AssignmentError) -> Self {
124        ExecutionError::AssignmentError(error)
125    }
126}
127
128impl Display for ExecutionError {
129    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
130        match self {
131            ExecutionError::ReferenceFromValueContainerError(err) => {
132                core::write!(f, "Reference from value container error: {err}")
133            }
134            ExecutionError::ReferenceNotFound => {
135                core::write!(f, "Reference not found")
136            }
137            ExecutionError::DXBParserError(err) => {
138                core::write!(f, "Parser error: {err}")
139            }
140            ExecutionError::Unknown => {
141                core::write!(f, "Unknown execution error")
142            }
143            ExecutionError::ValueError(err) => {
144                core::write!(f, "Value error: {err}")
145            }
146            ExecutionError::InvalidProgram(err) => {
147                core::write!(f, "Invalid program error: {err}")
148            }
149            ExecutionError::NotImplemented(msg) => {
150                core::write!(f, "Not implemented: {msg}")
151            }
152            ExecutionError::SlotNotAllocated(address) => {
153                core::write!(
154                    f,
155                    "Tried to access unallocated slot at address {address}"
156                )
157            }
158            ExecutionError::SlotNotInitialized(address) => {
159                core::write!(
160                    f,
161                    "Tried to access uninitialized slot at address {address}"
162                )
163            }
164            ExecutionError::RequiresAsyncExecution => {
165                core::write!(f, "Program must be executed asynchronously")
166            }
167            ExecutionError::RequiresRuntime => {
168                core::write!(f, "Execution requires a runtime to be set")
169            }
170            ExecutionError::ResponseError(err) => {
171                core::write!(f, "Response error: {err}")
172            }
173            ExecutionError::IllegalTypeError(err) => {
174                core::write!(f, "Illegal type: {err}")
175            }
176            ExecutionError::DerefOfNonReference => {
177                core::write!(f, "Tried to dereference a non-reference value")
178            }
179            ExecutionError::AssignmentError(err) => {
180                core::write!(f, "Assignment error: {err}")
181            }
182            ExecutionError::InvalidTypeCast => {
183                core::write!(f, "Invalid type cast")
184            }
185            ExecutionError::ExpectedTypeValue => {
186                core::write!(f, "Expected a type value")
187            }
188            ExecutionError::AccessError(err) => {
189                core::write!(f, "Access error: {err}")
190            }
191            ExecutionError::IntermediateResultWithState(
192                value_opt,
193                state_opt,
194            ) => {
195                core::write!(
196                    f,
197                    "Execution produced an intermediate result: {:?} with state: {:?}",
198                    value_opt,
199                    state_opt
200                )
201            }
202            ExecutionError::InvalidApply => {
203                core::write!(f, "Invalid apply operation")
204            }
205        }
206    }
207}