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 fmt::Display for Error {
22    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
23        write!(f, "{}", self.inner)
24    }
25}
26
27impl From<io::Error> for Error {
28    fn from(e: io::Error) -> Self {
29        Error {
30            inner: Box::new(ErrorInner::IoError(e)),
31        }
32    }
33}
34
35#[derive(Debug)]
36enum ErrorInner {
37    Msg(String),
38    IoError(io::Error),
39}
40
41impl fmt::Display for ErrorInner {
42    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
43        match *self {
44            ErrorInner::Msg(ref s) => write!(f, "{}", s),
45            ErrorInner::IoError(ref e) => write!(f, "{}", e),
46        }
47    }
48}