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
use core::fmt;
use std::error::Error;
use std::io;

pub use crate::parser::errors::*;

#[derive(Debug)]
#[non_exhaustive]
pub enum QueryError {
    NoMatches,
    InvalidRequest(String),
    InvalidSelector(String),
}

impl Error for QueryError {}

impl fmt::Display for QueryError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            QueryError::NoMatches => {
                write!(f, "Query request hasn't have any matches")
            }
            QueryError::InvalidRequest(err) => write!(f, "{}", err),
            QueryError::InvalidSelector(err) => write!(f, "{}", err),
        }
    }
}

#[derive(Debug)]
#[non_exhaustive]
pub enum ReplyError {
    ConnectionError(io::Error),
    ParseError(ParseError),
    QueryError(QueryError),
    InvalidRequest(String),
    RequestFailed(String),
    NoReply,
    InvalidSelector(String),
}

impl Error for ReplyError {}

impl fmt::Display for ReplyError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            ReplyError::ConnectionError(err) => err.fmt(f),
            ReplyError::ParseError(err) => err.fmt(f),
            ReplyError::InvalidRequest(err) => write!(f, "{}", err),
            ReplyError::QueryError(err) => err.fmt(f),
            ReplyError::RequestFailed(err) => write!(f, "{}", err),
            ReplyError::NoReply => {
                write!(f, "No reply was returned to given request")
            }
            ReplyError::InvalidSelector(err) => write!(f, "{}", err),
        }
    }
}

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

impl<T: Into<ParseError>> From<T> for ReplyError {
    fn from(error: T) -> ReplyError {
        ReplyError::ParseError(Into::into(error))
    }
}