bspc_rs/
errors.rs

1//! Contains errors, that are used in this crate.
2//!
3//! It also has different conversions between differenet types of errors.
4
5use core::fmt;
6use std::error::Error;
7use std::io;
8
9pub use crate::parser::errors::*;
10
11#[derive(Debug)]
12#[non_exhaustive]
13pub enum QueryError {
14    NoMatches,
15    InvalidRequest(String),
16    InvalidSelector(String),
17}
18
19impl Error for QueryError {}
20
21impl fmt::Display for QueryError {
22    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
23        match self {
24            QueryError::NoMatches => {
25                write!(f, "Query request hasn't have any matches")
26            }
27            QueryError::InvalidRequest(err) => write!(f, "{}", err),
28            QueryError::InvalidSelector(err) => write!(f, "{}", err),
29        }
30    }
31}
32
33#[derive(Debug)]
34#[non_exhaustive]
35pub enum ReplyError {
36    ConnectionError(io::Error),
37    ParseError(ParseError),
38    QueryError(QueryError),
39    InvalidRequest(String),
40    RequestFailed(String),
41    NoReply,
42    InvalidSelector(String),
43}
44
45impl Error for ReplyError {}
46
47impl fmt::Display for ReplyError {
48    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
49        match self {
50            ReplyError::ConnectionError(err) => err.fmt(f),
51            ReplyError::ParseError(err) => err.fmt(f),
52            ReplyError::InvalidRequest(err) => write!(f, "{}", err),
53            ReplyError::QueryError(err) => err.fmt(f),
54            ReplyError::RequestFailed(err) => write!(f, "{}", err),
55            ReplyError::NoReply => {
56                write!(f, "No reply was returned to given request")
57            }
58            ReplyError::InvalidSelector(err) => write!(f, "{}", err),
59        }
60    }
61}
62
63impl From<io::Error> for ReplyError {
64    fn from(error: io::Error) -> ReplyError {
65        ReplyError::ConnectionError(error)
66    }
67}
68
69impl<T: Into<ParseError>> From<T> for ReplyError {
70    fn from(error: T) -> ReplyError {
71        ReplyError::ParseError(Into::into(error))
72    }
73}