use alloc::borrow::Cow;
use miden_processor::ExecutionError;
use miden_processor::operation::OperationError;
use crate::Felt;
pub struct MasmError {
message: Cow<'static, str>,
}
impl MasmError {
pub const fn from_static_str(message: &'static str) -> Self {
Self { message: Cow::Borrowed(message) }
}
pub fn new(message: impl Into<Cow<'static, str>>) -> Self {
let message = message.into();
Self { message }
}
pub fn message(&self) -> &str {
&self.message
}
pub fn code(&self) -> Felt {
crate::assembly::mast::error_code_from_msg(&self.message)
}
pub fn matches_execution_error(&self, error: &ExecutionError) -> bool {
let ExecutionError::OperationError {
err: OperationError::FailedAssertion { err_code, err_msg },
..
} = error
else {
return false;
};
if *err_code != self.code() {
return false;
}
match err_msg {
Some(msg) => msg.as_ref() == self.message(),
None => true,
}
}
}
impl core::fmt::Display for MasmError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_fmt(format_args!("\"{}\" (code: {})", self.message(), self.code()))
}
}