cranelift_codegen_meta/
error.rs

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