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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*!
Error type, conversions, and macros

*/
use reqwest;
use semver;
use serde_json;
use std;
use zip::result::ZipError;

pub type Result<T> = std::result::Result<T, Error>;

#[derive(Debug)]
pub enum Error {
    Update(String),
    Network(String),
    Release(String),
    Config(String),
    Io(std::io::Error),
    Zip(ZipError),
    Json(serde_json::Error),
    Reqwest(reqwest::Error),
    SemVer(semver::SemVerError),
}

impl std::fmt::Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        use Error::*;
        match *self {
            Update(ref s) => write!(f, "UpdateError: {}", s),
            Network(ref s) => write!(f, "NetworkError: {}", s),
            Release(ref s) => write!(f, "ReleaseError: {}", s),
            Config(ref s) => write!(f, "ConfigError: {}", s),
            Io(ref e) => write!(f, "IoError: {}", e),
            Json(ref e) => write!(f, "JsonError: {}", e),
            Reqwest(ref e) => write!(f, "ReqwestError: {}", e),
            SemVer(ref e) => write!(f, "SemVerError: {}", e),
            Zip(ref e) => write!(f, "ZipError: {}", e),
        }
    }
}

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

    fn cause(&self) -> Option<&dyn std::error::Error> {
        use Error::*;
        Some(match *self {
            Io(ref e) => e,
            Json(ref e) => e,
            Reqwest(ref e) => e,
            SemVer(ref e) => e,
            _ => return None,
        })
    }

    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        use Error::*;
        Some(match *self {
            Io(ref e) => e,
            Json(ref e) => e,
            Reqwest(ref e) => e,
            SemVer(ref e) => e,
            _ => return None,
        })
    }
}

impl From<std::io::Error> for Error {
    fn from(e: std::io::Error) -> Error {
        Error::Io(e)
    }
}

impl From<serde_json::Error> for Error {
    fn from(e: serde_json::Error) -> Error {
        Error::Json(e)
    }
}

impl From<reqwest::Error> for Error {
    fn from(e: reqwest::Error) -> Error {
        Error::Reqwest(e)
    }
}

impl From<semver::SemVerError> for Error {
    fn from(e: semver::SemVerError) -> Error {
        Error::SemVer(e)
    }
}

impl From<ZipError> for Error {
    fn from(e: ZipError) -> Error {
        Error::Zip(e)
    }
}