miden_lib/errors/
script_builder_errors.rs

1use alloc::{boxed::Box, string::String};
2use core::error::Error;
3
4use miden_objects::assembly::diagnostics::{Report, reporting::PrintDiagnostic};
5
6// SCRIPT BUILDER ERROR
7// ================================================================================================
8
9#[derive(Debug, thiserror::Error)]
10#[error("failed to build script: {message}")]
11pub struct ScriptBuilderError {
12    /// Stack size of `Box<str>` is smaller than String.
13    message: Box<str>,
14    /// thiserror will return this when calling Error::source on ScriptBuilderError.
15    source: Option<Box<dyn Error + Send + Sync + 'static>>,
16}
17
18impl ScriptBuilderError {
19    /// Creates a script builder error from an error message and a source error.
20    pub fn build_error_with_source(
21        message: impl Into<String>,
22        source: impl Error + Send + Sync + 'static,
23    ) -> Self {
24        let message: String = message.into();
25        Self {
26            message: message.into(),
27            source: Some(Box::new(source)),
28        }
29    }
30
31    /// Creates a script builder error from a context message and a Report.
32    ///
33    /// This method uses PrintDiagnostic to stringify the Report since Report doesn't
34    /// implement core::error::Error and cannot be returned as a source error.
35    pub fn build_error_with_report(context: impl Into<String>, report: Report) -> Self {
36        let context: String = context.into();
37        let message = format!("{}: {}", context, PrintDiagnostic::new(&report));
38        Self { message: message.into(), source: None }
39    }
40}
41
42#[cfg(test)]
43mod error_assertions {
44    use super::*;
45
46    /// Asserts at compile time that the passed error has Send + Sync + 'static bounds.
47    fn _assert_error_is_send_sync_static<E: core::error::Error + Send + Sync + 'static>(_: E) {}
48
49    fn _assert_script_builder_error_bounds(err: ScriptBuilderError) {
50        _assert_error_is_send_sync_static(err);
51    }
52}