1use std::fmt::{Debug, Display, Formatter};
8use std::{fmt, io};
9
10use rustc_smir::bridge::SmirError;
11
12macro_rules! error {
13 ($fmt: literal $(,)?) => { Error(format!($fmt)) };
14 ($fmt: literal, $($arg:tt)*) => { Error(format!($fmt, $($arg)*)) };
15}
16
17pub(crate) use error;
18
19#[derive(Clone, Copy, PartialEq, Eq)]
21pub enum CompilerError<T> {
22 Failed,
24 Interrupted(T),
26 Skipped,
29}
30
31#[derive(Clone, Debug, Eq, PartialEq)]
33pub struct Error(pub(crate) String);
34
35impl SmirError for Error {
36 fn new(msg: String) -> Self {
37 Self(msg)
38 }
39
40 fn from_internal<T: Debug>(err: T) -> Self {
41 Self(format!("{err:?}"))
42 }
43}
44
45impl From<&str> for Error {
46 fn from(value: &str) -> Self {
47 Self(value.into())
48 }
49}
50
51impl Display for Error {
52 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
53 Display::fmt(&self.0, f)
54 }
55}
56
57impl<T> Display for CompilerError<T>
58where
59 T: Display,
60{
61 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
62 match self {
63 CompilerError::Failed => write!(f, "Compilation Failed"),
64 CompilerError::Interrupted(reason) => write!(f, "Compilation Interrupted: {reason}"),
65 CompilerError::Skipped => write!(f, "Compilation Skipped"),
66 }
67 }
68}
69
70impl<T> Debug for CompilerError<T>
71where
72 T: Debug,
73{
74 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
75 match self {
76 CompilerError::Failed => write!(f, "Compilation Failed"),
77 CompilerError::Interrupted(reason) => write!(f, "Compilation Interrupted: {reason:?}"),
78 CompilerError::Skipped => write!(f, "Compilation Skipped"),
79 }
80 }
81}
82
83impl std::error::Error for Error {}
84
85impl<T> std::error::Error for CompilerError<T> where T: Display + Debug {}
86
87impl From<io::Error> for Error {
88 fn from(value: io::Error) -> Self {
89 Error(value.to_string())
90 }
91}