1use std::fmt;
2
3pub type Result<T> = std::result::Result<T, MiraError>;
5
6#[derive(Debug, Clone, PartialEq)]
8#[non_exhaustive]
9pub enum MiraError {
10 Compile {
12 diagnostics: Vec<u32>,
14 },
15 InvalidBytecode {
17 offset: usize,
19 reason: String,
21 },
22 Runtime {
24 message: String,
26 function: Option<String>,
28 offset: Option<usize>,
30 stack: Vec<String>,
32 },
33 Conversion {
35 expected: String,
37 actual: String,
39 path: Option<String>,
41 },
42 Extern {
44 message: String,
46 },
47 BorrowConflict {
49 operation: &'static str,
51 tag: String,
53 },
54 Timeout,
56 MaxCallDepth {
58 max: u32,
60 },
61 EscapingClosure,
63 ExecutionEnded,
65}
66
67impl MiraError {
68 pub(crate) fn runtime(message: impl Into<String>) -> Self {
69 Self::Runtime {
70 message: message.into(),
71 function: None,
72 offset: None,
73 stack: Vec::new(),
74 }
75 }
76
77 pub(crate) fn conversion(expected: impl Into<String>, value: &crate::MiraAny) -> Self {
78 Self::Conversion {
79 expected: expected.into(),
80 actual: value.type_name().into(),
81 path: None,
82 }
83 }
84
85 pub fn at_path(mut self, path: impl Into<String>) -> Self {
89 if let Self::Conversion { path: slot, .. } = &mut self {
90 *slot = Some(path.into());
91 }
92 self
93 }
94
95 pub(crate) fn with_runtime_context(
96 mut self,
97 function: Option<String>,
98 offset: usize,
99 stack: Vec<String>,
100 ) -> Self {
101 if let Self::Runtime {
102 function: error_function,
103 offset: error_offset,
104 stack: error_stack,
105 ..
106 } = &mut self
107 && error_function.is_none()
108 {
109 *error_function = function;
110 *error_offset = Some(offset);
111 *error_stack = stack;
112 }
113 self
114 }
115}
116
117impl fmt::Display for MiraError {
118 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
119 match self {
120 Self::Compile { diagnostics } => {
121 write!(
122 f,
123 "failed to compile MiraScript ({} diagnostic words)",
124 diagnostics.len()
125 )
126 }
127 Self::InvalidBytecode { offset, reason } => {
128 write!(f, "invalid bytecode at offset {offset}: {reason}")
129 }
130 Self::Runtime {
131 message,
132 function,
133 offset,
134 ..
135 } => {
136 write!(f, "{message}")?;
137 if let Some(function) = function {
138 write!(f, " in {function}")?;
139 }
140 if let Some(offset) = offset {
141 write!(f, " at bytecode offset {offset}")?;
142 }
143 Ok(())
144 }
145 Self::Conversion {
146 expected,
147 actual,
148 path,
149 } => {
150 write!(f, "failed to convert {actual} to {expected}")?;
151 if let Some(path) = path {
152 write!(f, " at {path}")?;
153 }
154 Ok(())
155 }
156 Self::Extern { message } => write!(f, "extern error: {message}"),
157 Self::BorrowConflict { operation, tag } => {
158 write!(f, "borrow conflict while attempting to {operation} {tag}")
159 }
160 Self::Timeout => write!(f, "MiraScript execution timed out"),
161 Self::MaxCallDepth { max } => write!(f, "maximum call depth of {max} exceeded"),
162 Self::EscapingClosure => {
163 write!(f, "a MiraScript closure or module cannot escape run()")
164 }
165 Self::ExecutionEnded => write!(f, "MiraScript execution has ended"),
166 }
167 }
168}
169
170impl std::error::Error for MiraError {}