summaryrefslogtreecommitdiffstats
path: root/src/error.rs
blob: 630dfc899b9cfe5f83017ab280933c82741c0237 (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
use std::io::ErrorKind;

#[derive(Copy, Clone, Debug)]
pub enum JoshutoErrorKind {
    IONotFound,
    IOPermissionDenied,
    IOConnectionRefused,
    IOConnectionReset,
    IOConnectionAborted,
    IONotConnected,
    IOAddrInUse,
    IOAddrNotAvailable,
    IOBrokenPipe,
    IOAlreadyExists,
    IOWouldBlock,
    IOInvalidInput,

    // also used for invalid arguments
    IOInvalidData,
    IOTimedOut,
    IOWriteZero,
    IOInterrupted,
    IOOther,
    IOUnexpectedEof,

    // environment variable not found
    EnvVarNotPresent,

    ParseError,
    UnknownCommand,
}

impl std::convert::From<ErrorKind> for JoshutoErrorKind {
    fn from(err: ErrorKind) -> Self {
        match err {
            ErrorKind::NotFound => JoshutoErrorKind::IONotFound,
            ErrorKind::PermissionDenied => JoshutoErrorKind::IOPermissionDenied,
            ErrorKind::ConnectionRefused => JoshutoErrorKind::IOConnectionRefused,
            ErrorKind::ConnectionReset => JoshutoErrorKind::IOConnectionReset,
            ErrorKind::ConnectionAborted => JoshutoErrorKind::IOConnectionAborted,
            ErrorKind::NotConnected => JoshutoErrorKind::IONotConnected,
            ErrorKind::AddrInUse => JoshutoErrorKind::IOAddrInUse,
            ErrorKind::AddrNotAvailable => JoshutoErrorKind::IOAddrNotAvailable,
            ErrorKind::BrokenPipe => JoshutoErrorKind::IOBrokenPipe,
            ErrorKind::AlreadyExists => JoshutoErrorKind::IOAlreadyExists,
            ErrorKind::WouldBlock => JoshutoErrorKind::IOWouldBlock,
            ErrorKind::InvalidInput => JoshutoErrorKind::IOInvalidInput,
            ErrorKind::InvalidData => JoshutoErrorKind::IOInvalidData,
            ErrorKind::TimedOut => JoshutoErrorKind::IOTimedOut,
            ErrorKind::WriteZero => JoshutoErrorKind::IOWriteZero,
            ErrorKind::Interrupted => JoshutoErrorKind::IOInterrupted,
            ErrorKind::UnexpectedEof => JoshutoErrorKind::IOUnexpectedEof,
            ErrorKind::Other => JoshutoErrorKind::IOOther,
            _ => JoshutoErrorKind::IOOther,
        }
    }
}

pub struct JoshutoError {
    _kind: JoshutoErrorKind,
    _cause: String,
}

#[allow(dead_code)]
impl JoshutoError {
    pub fn new(_kind: JoshutoErrorKind, _cause: String) -> Self {
        JoshutoError { _kind, _cause }
    }

    pub fn kind(&self) -> JoshutoErrorKind {
        self._kind
    }

    pub fn cause(&self) -> &str {
        self._cause.as_str()
    }
}

impl std::string::ToString for JoshutoError {
    fn to_string(&self) -> String {
        self._cause.clone()
    }
}

impl std::convert::From<std::io::Error> for JoshutoError {
    fn from(err: std::io::Error) -> Self {
        JoshutoError {
            _kind: JoshutoErrorKind::from(err.kind()),
            _cause: err.to_string(),
        }
    }
}

pub type JoshutoResult<T> = Result<T, JoshutoError>;