summaryrefslogtreecommitdiffstats
path: root/third_party/rust/autocfg/src/error.rs
blob: 4624835451b05ce906f295546fb198481042abd9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
use std::error;
use std::fmt;
use std::io;
use std::num;
use std::str;

/// A common error type for the `autocfg` crate.
#[derive(Debug)]
pub struct Error {
    kind: ErrorKind,
}

impl error::Error for Error {
    fn description(&self) -> &str {
        "AutoCfg error"
    }

    fn cause(&self) -> Option<&error::Error> {
        match self.kind {
            ErrorKind::Io(ref e) => Some(e),
            ErrorKind::Num(ref e) => Some(e),
            ErrorKind::Utf8(ref e) => Some(e),
            ErrorKind::Other(_) => None,
        }
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        match self.kind {
            ErrorKind::Io(ref e) => e.fmt(f),
            ErrorKind::Num(ref e) => e.fmt(f),
            ErrorKind::Utf8(ref e) => e.fmt(f),
            ErrorKind::Other(s) => s.fmt(f),
        }
    }
}

#[derive(Debug)]
enum ErrorKind {
    Io(io::Error),
    Num(num::ParseIntError),
    Utf8(str::Utf8Error),
    Other(&'static str),
}

pub fn from_io(e: io::Error) -> Error {
    Error {
        kind: ErrorKind::Io(e),
    }
}

pub fn from_num(e: num::ParseIntError) -> Error {
    Error {
        kind: ErrorKind::Num(e),
    }
}

pub fn from_utf8(e: str::Utf8Error) -> Error {
    Error {
        kind: ErrorKind::Utf8(e),
    }
}

pub fn from_str(s: &'static str) -> Error {
    Error {
        kind: ErrorKind::Other(s),
    }
}