1use std::error; 2use std::fmt; 3use std::io; 4use std::num; 5use std::str; 6 7/// A common error type for the `autocfg` crate. 8#[derive(Debug)] 9pub struct Error { 10 kind: ErrorKind, 11} 12 13impl error::Error for Error { 14 fn description(&self) -> &str { 15 "AutoCfg error" 16 } 17 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 28impl fmt::Display for 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)] 40enum ErrorKind { 41 Io(io::Error), 42 Num(num::ParseIntError), 43 Utf8(str::Utf8Error), 44 Other(&'static str), 45} 46 47pub fn from_io(e: io::Error) -> Error { 48 Error { 49 kind: ErrorKind::Io(e), 50 } 51} 52 53pub fn from_num(e: num::ParseIntError) -> Error { 54 Error { 55 kind: ErrorKind::Num(e), 56 } 57} 58 59pub fn from_utf8(e: str::Utf8Error) -> Error { 60 Error { 61 kind: ErrorKind::Utf8(e), 62 } 63} 64 65pub fn from_str(s: &'static str) -> Error { 66 Error { 67 kind: ErrorKind::Other(s), 68 } 69} 70