1use std::error;
2use std::fmt;
3use std::io;
4use std::num;
5use std::process;
6use std::str;
7
8/// A common error type for the `autocfg` crate.
9#[derive(Debug)]
10pub struct Error {
11 kind: ErrorKind,
12}
13
14impl 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
29impl 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)]
45enum ErrorKind {
46 Io(io::Error),
47 Num(num::ParseIntError),
48 Process(process::ExitStatus),
49 Utf8(str::Utf8Error),
50 Other(&'static str),
51}
52
53pub fn from_exit(status: process::ExitStatus) -> Error {
54 Error {
55 kind: ErrorKind::Process(status),
56 }
57}
58
59pub fn from_io(e: io::Error) -> Error {
60 Error {
61 kind: ErrorKind::Io(e),
62 }
63}
64
65pub fn from_num(e: num::ParseIntError) -> Error {
66 Error {
67 kind: ErrorKind::Num(e),
68 }
69}
70
71pub fn from_utf8(e: str::Utf8Error) -> Error {
72 Error {
73 kind: ErrorKind::Utf8(e),
74 }
75}
76
77pub fn from_str(s: &'static str) -> Error {
78 Error {
79 kind: ErrorKind::Other(s),
80 }
81}
82

Provided by KDAB

Privacy Policy
Learn Rust with the experts
Find out more