aboutsummaryrefslogtreecommitdiff
path: root/rustc_version/src/errors.rs
blob: 54557b6e28389d45f520535480811d55544d336b (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
70
71
72
73
74
75
76
77
78
79
use std::{self, error, fmt, io, str};
use semver::{self, Identifier};

/// The error type for this crate.
#[derive(Debug)]
pub enum Error {
    /// An error ocurrend when executing the `rustc` command.
    CouldNotExecuteCommand(io::Error),
    /// The output of `rustc -vV` was not valid utf-8.
    Utf8Error(str::Utf8Error),
    /// The output of `rustc -vV` was not in the expected format.
    UnexpectedVersionFormat,
    /// An error ocurred in parsing a `VersionReq`.
    ReqParseError(semver::ReqParseError),
    /// An error ocurred in parsing the semver.
    SemVerError(semver::SemVerError),
    /// The pre-release tag is unknown.
    UnknownPreReleaseTag(Identifier),
}
use Error::*;

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        use std::error::Error;
        match *self {
            CouldNotExecuteCommand(ref e) => write!(f, "{}: {}", self.description(), e),
            Utf8Error(_) => write!(f, "{}", self.description()),
            UnexpectedVersionFormat => write!(f, "{}", self.description()),
            ReqParseError(ref e) => write!(f, "{}: {}", self.description(), e),
            SemVerError(ref e) => write!(f, "{}: {}", self.description(), e),
            UnknownPreReleaseTag(ref i) => write!(f, "{}: {}", self.description(), i),
        }
    }
}

impl error::Error for Error {
    fn cause(&self) -> Option<&error::Error> {
        match *self {
            CouldNotExecuteCommand(ref e) => Some(e),
            Utf8Error(ref e) => Some(e),
            UnexpectedVersionFormat => None,
            ReqParseError(ref e) => Some(e),
            SemVerError(ref e) => Some(e),
            UnknownPreReleaseTag(_) => None,
        }
    }

    fn description(&self) -> &str {
        match *self {
            CouldNotExecuteCommand(_) => "could not execute command",
            Utf8Error(_) => "invalid UTF-8 output from `rustc -vV`",
            UnexpectedVersionFormat => "unexpected `rustc -vV` format",
            ReqParseError(_) => "error parsing version requirement",
            SemVerError(_) => "error parsing version",
            UnknownPreReleaseTag(_) => "unknown pre-release tag",
        }
    }
}

macro_rules! impl_from {
    ($($err_ty:ty => $variant:ident),* $(,)*) => {
        $(
            impl From<$err_ty> for Error {
                fn from(e: $err_ty) -> Error {
                    Error::$variant(e)
                }
            }
        )*
    }
}

impl_from! {
    str::Utf8Error => Utf8Error,
    semver::SemVerError => SemVerError,
    semver::ReqParseError => ReqParseError,
}

/// The result type for this crate.
pub type Result<T> = std::result::Result<T, Error>;