aboutsummaryrefslogtreecommitdiff
path: root/src/error.rs
blob: cc3b235de1e954811223de0ba8ca16cbbc34d8af (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
// Copyright (C) 2019 Robin Krahl <robin.krahl@ireas.org>
// SPDX-License-Identifier: MIT

use std::fmt;
use std::io;
use std::process;
use std::str;

#[derive(Debug)]
pub enum Error {
    DialogError(dialog::Error),
    IoError(io::Error),
    Error(String),
    UrlParseError(url::ParseError),
    Utf8Error(str::Utf8Error),
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match *self {
            Error::DialogError(ref err) => write!(f, "Dialog error: {:?}", err),
            Error::IoError(ref err) => write!(f, "IO error: {}", err),
            Error::Error(ref string) => write!(f, "Error: {}", string),
            Error::UrlParseError(ref err) => write!(f, "URL parse error: {}", err),
            Error::Utf8Error(ref err) => write!(f, "UTF-8 error: {}", err),
        }
    }
}

impl From<&str> for Error {
    fn from(string: &str) -> Error {
        Error::Error(string.to_string())
    }
}

impl From<dialog::Error> for Error {
    fn from(error: dialog::Error) -> Error {
        Error::DialogError(error)
    }
}

impl From<io::Error> for Error {
    fn from(error: io::Error) -> Error {
        Error::IoError(error)
    }
}

impl From<(&str, process::ExitStatus)> for Error {
    fn from(data: (&str, process::ExitStatus)) -> Error {
        let (command, status) = data;
        let msg = match status.code() {
            Some(code) => format!("{} failed with error code {}", command, code),
            None => format!("{} was terminated by a signal", command),
        };
        Error::Error(msg)
    }
}

impl From<url::ParseError> for Error {
    fn from(error: url::ParseError) -> Error {
        Error::UrlParseError(error)
    }
}

impl From<str::Utf8Error> for Error {
    fn from(error: str::Utf8Error) -> Error {
        Error::Utf8Error(error)
    }
}