summaryrefslogtreecommitdiffstats
path: root/src/error.rs
blob: f24fa3d8da8c3d504a94ddec5a9d39cd853ff633 (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
use std::error::Error as StdError;
use std::fmt;
use std::num::TryFromIntError;

#[derive(Debug)]
pub enum Error {
    Io(std::io::Error),
    ParseRange(ParseRangeError),
    TryFromInt(TryFromIntError),
    Config(String),
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Self::Io(io) => write!(f, "{}", io),
            Self::ParseRange(pr) => write!(f, "{}", pr),
            Self::TryFromInt(tfi) => write!(f, "{}", tfi),
            Self::Config(c) => write!(f, "{}", c),
        }
    }
}

impl StdError for Error {}

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

impl From<ParseRangeError> for Error {
    fn from(e: ParseRangeError) -> Self {
        Self::ParseRange(e)
    }
}

impl From<TryFromIntError> for Error {
    fn from(e: TryFromIntError) -> Self {
        Self::TryFromInt(e)
    }
}

#[derive(Debug)]
pub struct ParseRangeError {
    source_str: String,
}

impl ParseRangeError {
    pub fn new(source_str: &str) -> Self {
        ParseRangeError {
            source_str: String::from(source_str),
        }
    }
}

impl fmt::Display for ParseRangeError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.source_str)
    }
}

impl StdError for ParseRangeError {}