Skip to main content

melior/
error.rs

1use std::{
2    convert::Infallible,
3    error,
4    fmt::{self, Display, Formatter},
5    str::Utf8Error,
6};
7
8/// A Melior error.
9#[derive(Debug, Eq, PartialEq)]
10pub enum Error {
11    AttributeExpected(&'static str, String),
12    AttributeNotFound(String),
13    AttributeParse(String),
14    BlockArgumentExpected(String),
15    ElementExpected {
16        r#type: &'static str,
17        value: String,
18    },
19    InvokeFunction,
20    OperationBuild,
21    OperandNotFound(&'static str),
22    OperationResultExpected(String),
23    PositionOutOfBounds {
24        name: &'static str,
25        value: String,
26        index: usize,
27    },
28    ParsePassPipeline(String),
29    ResultNotFound(&'static str),
30    RunPass,
31    TypeExpected(&'static str, String),
32    UnknownDiagnosticSeverity(u32),
33    Utf8(Utf8Error),
34}
35
36impl Display for Error {
37    fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
38        match self {
39            Self::AttributeExpected(r#type, attribute) => {
40                write!(formatter, "{type} attribute expected: {attribute}")
41            }
42            Self::AttributeNotFound(name) => {
43                write!(formatter, "attribute {name} not found")
44            }
45            Self::AttributeParse(string) => {
46                write!(formatter, "failed to parse attribute: {string}")
47            }
48            Self::BlockArgumentExpected(value) => {
49                write!(formatter, "block argument expected: {value}")
50            }
51            Self::ElementExpected { r#type, value } => {
52                write!(formatter, "element of {type} type expected: {value}")
53            }
54            Self::InvokeFunction => write!(formatter, "failed to invoke JIT-compiled function"),
55            Self::OperationBuild => {
56                write!(formatter, "operation build failed")
57            }
58            Self::OperandNotFound(name) => {
59                write!(formatter, "operand {name} not found")
60            }
61            Self::OperationResultExpected(value) => {
62                write!(formatter, "operation result expected: {value}")
63            }
64            Self::ParsePassPipeline(message) => {
65                write!(formatter, "failed to parse pass pipeline:\n{}", message)
66            }
67            Self::PositionOutOfBounds { name, value, index } => {
68                write!(formatter, "{name} position {index} out of bounds: {value}")
69            }
70            Self::ResultNotFound(name) => {
71                write!(formatter, "result {name} not found")
72            }
73            Self::RunPass => write!(formatter, "failed to run pass"),
74            Self::TypeExpected(r#type, actual) => {
75                write!(formatter, "{type} type expected: {actual}")
76            }
77            Self::UnknownDiagnosticSeverity(severity) => {
78                write!(formatter, "unknown diagnostic severity: {severity}")
79            }
80            Self::Utf8(error) => {
81                write!(formatter, "{}", error)
82            }
83        }
84    }
85}
86
87impl error::Error for Error {}
88
89impl From<Utf8Error> for Error {
90    fn from(error: Utf8Error) -> Self {
91        Self::Utf8(error)
92    }
93}
94
95impl From<Infallible> for Error {
96    fn from(_: Infallible) -> Self {
97        unreachable!()
98    }
99}