miden_lib/errors/
masm_error.rs

1use alloc::borrow::Cow;
2
3use miden_objects::Felt;
4
5/// A convenience wrapper around an error extracted from Miden Assembly source files.
6pub struct MasmError {
7    message: Cow<'static, str>,
8}
9
10impl MasmError {
11    /// Constructs a new error from a static str.
12    pub const fn from_static_str(message: &'static str) -> Self {
13        Self { message: Cow::Borrowed(message) }
14    }
15
16    /// Constructs a new error from string.
17    pub fn new(message: impl Into<Cow<'static, str>>) -> Self {
18        let message = message.into();
19
20        Self { message }
21    }
22
23    /// Returns the message of this error.
24    pub fn message(&self) -> &str {
25        &self.message
26    }
27
28    /// Returns the code of this error.
29    pub fn code(&self) -> Felt {
30        miden_objects::assembly::mast::error_code_from_msg(&self.message)
31    }
32}
33
34impl core::fmt::Display for MasmError {
35    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
36        f.write_fmt(format_args!("\"{}\" (code: {})", self.message(), self.code()))
37    }
38}