Skip to main content

autophagy_mlir/
error.rs

1use core::fmt;
2use std::{
3    error,
4    fmt::{Display, Formatter},
5};
6
7#[derive(Debug)]
8pub enum Error {
9    AddFn(String),
10    Melior(melior::Error),
11    NotSupported(&'static str),
12    Syn(syn::Error),
13    StructFieldNotDefined(String, String),
14    StructNotDefined(String),
15    TypeNotDefined(String),
16    ValueExpected(String),
17    VariableNotDefined(String),
18}
19
20impl Display for Error {
21    fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
22        match self {
23            Self::AddFn(name) => {
24                write!(formatter, "failed to add a function: {}", name)
25            }
26            Self::Melior(error) => write!(formatter, "{}", error),
27            Self::NotSupported(name) => {
28                write!(formatter, "{name} not supported")
29            }
30            Self::Syn(error) => write!(formatter, "{}", error),
31            Self::StructFieldNotDefined(r#type, field) => {
32                write!(
33                    formatter,
34                    "struct type field \"{}\" not defined: {}",
35                    field, r#type
36                )
37            }
38            Self::StructNotDefined(message) => {
39                write!(formatter, "struct type not defined: {}", message)
40            }
41            Self::TypeNotDefined(message) => {
42                write!(formatter, "type not defined: {}", message)
43            }
44            Self::ValueExpected(message) => write!(formatter, "value expected: {}", message),
45            Self::VariableNotDefined(name) => {
46                write!(formatter, "variable not defined: {name}")
47            }
48        }
49    }
50}
51
52impl error::Error for Error {}
53
54impl From<melior::Error> for Error {
55    fn from(error: melior::Error) -> Self {
56        Self::Melior(error)
57    }
58}
59
60impl From<syn::Error> for Error {
61    fn from(error: syn::Error) -> Self {
62        Self::Syn(error)
63    }
64}