1 use std::error;
2 use std::fmt;
3 use std::io;
4 use std::num;
5 use std::str;
6
7 /// A common error type for the `autocfg` crate.
8 #[derive(Debug)]
9 pub struct Error {
10 kind: ErrorKind,
11 }
12
13 impl error::Error for Error {
description(&self) -> &str14 fn description(&self) -> &str {
15 "AutoCfg error"
16 }
17
cause(&self) -> Option<&error::Error>18 fn cause(&self) -> Option<&error::Error> {
19 match self.kind {
20 ErrorKind::Io(ref e) => Some(e),
21 ErrorKind::Num(ref e) => Some(e),
22 ErrorKind::Utf8(ref e) => Some(e),
23 ErrorKind::Other(_) => None,
24 }
25 }
26 }
27
28 impl fmt::Display for Error {
fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error>29 fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
30 match self.kind {
31 ErrorKind::Io(ref e) => e.fmt(f),
32 ErrorKind::Num(ref e) => e.fmt(f),
33 ErrorKind::Utf8(ref e) => e.fmt(f),
34 ErrorKind::Other(s) => s.fmt(f),
35 }
36 }
37 }
38
39 #[derive(Debug)]
40 enum ErrorKind {
41 Io(io::Error),
42 Num(num::ParseIntError),
43 Utf8(str::Utf8Error),
44 Other(&'static str),
45 }
46
from_io(e: io::Error) -> Error47 pub fn from_io(e: io::Error) -> Error {
48 Error {
49 kind: ErrorKind::Io(e),
50 }
51 }
52
from_num(e: num::ParseIntError) -> Error53 pub fn from_num(e: num::ParseIntError) -> Error {
54 Error {
55 kind: ErrorKind::Num(e),
56 }
57 }
58
from_utf8(e: str::Utf8Error) -> Error59 pub fn from_utf8(e: str::Utf8Error) -> Error {
60 Error {
61 kind: ErrorKind::Utf8(e),
62 }
63 }
64
from_str(s: &'static str) -> Error65 pub fn from_str(s: &'static str) -> Error {
66 Error {
67 kind: ErrorKind::Other(s),
68 }
69 }
70