1 | use std::error; |
---|---|
2 | use std::fmt; |
3 | use std::io; |
4 | use std::num; |
5 | use std::process; |
6 | use std::str; |
7 | |
8 | /// A common error type for the `autocfg` crate. |
9 | #[derive(Debug)] |
10 | pub struct Error { |
11 | kind: ErrorKind, |
12 | } |
13 | |
14 | impl error::Error for Error { |
15 | fn description(&self) -> &str { |
16 | "AutoCfg error" |
17 | } |
18 | |
19 | fn cause(&self) -> Option<&dynerror::Error> { |
20 | match self.kind { |
21 | ErrorKind::Io(ref e: &Error) => Some(e), |
22 | ErrorKind::Num(ref e: &ParseIntError) => Some(e), |
23 | ErrorKind::Utf8(ref e: &Utf8Error) => Some(e), |
24 | ErrorKind::Process(_) | ErrorKind::Other(_) => None, |
25 | } |
26 | } |
27 | } |
28 | |
29 | impl fmt::Display for Error { |
30 | fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { |
31 | match self.kind { |
32 | ErrorKind::Io(ref e: &Error) => e.fmt(f), |
33 | ErrorKind::Num(ref e: &ParseIntError) => e.fmt(f), |
34 | ErrorKind::Utf8(ref e: &Utf8Error) => e.fmt(f), |
35 | ErrorKind::Process(ref status: &ExitStatus) => { |
36 | // Same message as the newer `ExitStatusError` |
37 | write!(f, "process exited unsuccessfully:{} ", status) |
38 | } |
39 | ErrorKind::Other(s: &'static str) => s.fmt(f), |
40 | } |
41 | } |
42 | } |
43 | |
44 | #[derive(Debug)] |
45 | enum ErrorKind { |
46 | Io(io::Error), |
47 | Num(num::ParseIntError), |
48 | Process(process::ExitStatus), |
49 | Utf8(str::Utf8Error), |
50 | Other(&'static str), |
51 | } |
52 | |
53 | pub fn from_exit(status: process::ExitStatus) -> Error { |
54 | Error { |
55 | kind: ErrorKind::Process(status), |
56 | } |
57 | } |
58 | |
59 | pub fn from_io(e: io::Error) -> Error { |
60 | Error { |
61 | kind: ErrorKind::Io(e), |
62 | } |
63 | } |
64 | |
65 | pub fn from_num(e: num::ParseIntError) -> Error { |
66 | Error { |
67 | kind: ErrorKind::Num(e), |
68 | } |
69 | } |
70 | |
71 | pub fn from_utf8(e: str::Utf8Error) -> Error { |
72 | Error { |
73 | kind: ErrorKind::Utf8(e), |
74 | } |
75 | } |
76 | |
77 | pub fn from_str(s: &'static str) -> Error { |
78 | Error { |
79 | kind: ErrorKind::Other(s), |
80 | } |
81 | } |
82 |