Skip to main content

ruxla_ir/
error.rs

1//! Errors.
2
3/// A specialized result type.
4pub type Result<T, E = Error> = std::result::Result<T, E>;
5
6/// Errors found in the IR or while evaluating it.
7#[derive(Clone, PartialEq, Eq, Debug, thiserror::Error)]
8#[non_exhaustive]
9pub enum Error {
10    /// A primitive was applied to inputs or parameters it does not accept.
11    #[error("{prim}: {message}")]
12    Type {
13        /// The primitive.
14        prim: &'static str,
15        /// What is wrong.
16        message: String,
17    },
18    /// A primitive or rule is not implemented for these inputs yet.
19    #[error("{prim}: unsupported: {message}")]
20    Unsupported {
21        /// The primitive.
22        prim: &'static str,
23        /// What is not supported.
24        message: String,
25    },
26    /// A function or serialized function is not well formed.
27    #[error("malformed IR: {0}")]
28    Malformed(String),
29}
30
31impl Error {
32    /// Creates an [`Error::Type`].
33    pub fn type_error(prim: &'static str, message: impl Into<String>) -> Self {
34        Self::Type {
35            prim,
36            message: message.into(),
37        }
38    }
39
40    /// Creates an [`Error::Unsupported`].
41    pub fn unsupported(prim: &'static str, message: impl Into<String>) -> Self {
42        Self::Unsupported {
43            prim,
44            message: message.into(),
45        }
46    }
47}