cranelift_srcgen/
error.rs

1//! Defines an `Error` returned during source code generation.
2
3use std::fmt;
4use std::io;
5
6/// An error that occurred when the cranelift_codegen_meta crate was generating
7/// source files for the cranelift_codegen crate.
8#[derive(Debug)]
9pub struct Error {
10    inner: Box<ErrorInner>,
11}
12
13impl Error {
14    /// Create a new error object with the given message.
15    pub fn with_msg<S: Into<String>>(msg: S) -> Error {
16        Error {
17            inner: Box::new(ErrorInner::Msg(msg.into())),
18        }
19    }
20}
21
22impl std::error::Error for Error {}
23
24impl fmt::Display for Error {
25    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
26        write!(f, "{}", self.inner)
27    }
28}
29
30impl From<io::Error> for Error {
31    fn from(e: io::Error) -> Self {
32        Error {
33            inner: Box::new(ErrorInner::IoError(e)),
34        }
35    }
36}
37
38#[derive(Debug)]
39enum ErrorInner {
40    Msg(String),
41    IoError(io::Error),
42}
43
44impl fmt::Display for ErrorInner {
45    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
46        match *self {
47            ErrorInner::Msg(ref s) => write!(f, "{s}"),
48            ErrorInner::IoError(ref e) => write!(f, "{e}"),
49        }
50    }
51}