async_memcached/
error.rs

1use crate::parser::Status;
2use std::{fmt, io};
3
4/// Error type for [`Client`](crate::Client) operations.
5#[derive(Debug)]
6pub enum Error {
7    /// Connect error.
8    /// Useful for distinguishing between transitive I/O errors and connection errors.
9    Connect(io::Error),
10    /// I/O-related error.
11    Io(io::Error),
12    /// A protocol-level error i.e. a failed operation or message that
13    /// does not match the protocol specification.
14    Protocol(Status),
15    /// A parsing error surfaced from nom
16    ParseError(nom::error::ErrorKind),
17}
18
19impl PartialEq for Error {
20    fn eq(&self, other: &Self) -> bool {
21        match (self, other) {
22            (Self::Connect(e1), Self::Connect(e2)) => e1.kind() == e2.kind(),
23            (Self::Io(e1), Self::Io(e2)) => e1.kind() == e2.kind(),
24            (Self::Protocol(s1), Self::Protocol(s2)) => s1 == s2,
25            _ => false,
26        }
27    }
28}
29
30impl std::error::Error for Error {
31    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
32        match self {
33            Self::Io(ref e) => Some(e),
34            _ => None,
35        }
36    }
37}
38
39impl fmt::Display for Error {
40    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
41        match self {
42            Self::Connect(e) => write!(f, "connect: {}", e),
43            Self::Io(e) => write!(f, "io: {}", e),
44            Self::Protocol(e) => write!(f, "protocol: {}", e),
45            Self::ParseError(e) => write!(f, "parse error: {:?}", e),
46        }
47    }
48}
49
50impl From<std::io::Error> for Error {
51    fn from(e: std::io::Error) -> Self {
52        Error::Io(e)
53    }
54}
55
56impl From<Status> for Error {
57    fn from(s: Status) -> Self {
58        Error::Protocol(s)
59    }
60}
61
62impl From<nom::error::ErrorKind> for Error {
63    fn from(e: nom::error::ErrorKind) -> Self {
64        Error::ParseError(e)
65    }
66}