1 //! Defines an `Error` returned during source code generation.
2 
3 use std::fmt;
4 use 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)]
9 pub struct Error {
10     inner: Box<ErrorInner>,
11 }
12 
13 impl Error {
14     /// Create a new error object with the given message.
with_msg<S: Into<String>>(msg: S) -> Error15     pub fn with_msg<S: Into<String>>(msg: S) -> Error {
16         Error {
17             inner: Box::new(ErrorInner::Msg(msg.into())),
18         }
19     }
20 }
21 
22 impl std::error::Error for Error {}
23 
24 impl fmt::Display for Error {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result25     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
26         write!(f, "{}", self.inner)
27     }
28 }
29 
30 impl From<io::Error> for Error {
from(e: io::Error) -> Self31     fn from(e: io::Error) -> Self {
32         Error {
33             inner: Box::new(ErrorInner::IoError(e)),
34         }
35     }
36 }
37 
38 #[derive(Debug)]
39 enum ErrorInner {
40     Msg(String),
41     IoError(io::Error),
42 }
43 
44 impl fmt::Display for ErrorInner {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result45     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 }
52