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 {
description(&self) -> &str15 fn description(&self) -> &str {
16 "AutoCfg error"
17 }
18
cause(&self) -> Option<&error::Error>19 fn cause(&self) -> Option<&error::Error> {
20 match self.kind {
21 ErrorKind::Io(ref e) => Some(e),
22 ErrorKind::Num(ref e) => Some(e),
23 ErrorKind::Utf8(ref e) => Some(e),
24 ErrorKind::Process(_) | ErrorKind::Other(_) => None,
25 }
26 }
27 }
28
29 impl fmt::Display for Error {
fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error>30 fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
31 match self.kind {
32 ErrorKind::Io(ref e) => e.fmt(f),
33 ErrorKind::Num(ref e) => e.fmt(f),
34 ErrorKind::Utf8(ref e) => e.fmt(f),
35 ErrorKind::Process(ref status) => {
36 // Same message as the newer `ExitStatusError`
37 write!(f, "process exited unsuccessfully: {}", status)
38 }
39 ErrorKind::Other(s) => 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
from_exit(status: process::ExitStatus) -> Error53 pub fn from_exit(status: process::ExitStatus) -> Error {
54 Error {
55 kind: ErrorKind::Process(status),
56 }
57 }
58
from_io(e: io::Error) -> Error59 pub fn from_io(e: io::Error) -> Error {
60 Error {
61 kind: ErrorKind::Io(e),
62 }
63 }
64
from_num(e: num::ParseIntError) -> Error65 pub fn from_num(e: num::ParseIntError) -> Error {
66 Error {
67 kind: ErrorKind::Num(e),
68 }
69 }
70
from_utf8(e: str::Utf8Error) -> Error71 pub fn from_utf8(e: str::Utf8Error) -> Error {
72 Error {
73 kind: ErrorKind::Utf8(e),
74 }
75 }
76
from_str(s: &'static str) -> Error77 pub fn from_str(s: &'static str) -> Error {
78 Error {
79 kind: ErrorKind::Other(s),
80 }
81 }
82