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}
16
17impl PartialEq for Error {
18    fn eq(&self, other: &Self) -> bool {
19        match (self, other) {
20            (Self::Connect(e1), Self::Connect(e2)) => e1.kind() == e2.kind(),
21            (Self::Io(e1), Self::Io(e2)) => e1.kind() == e2.kind(),
22            (Self::Protocol(s1), Self::Protocol(s2)) => s1 == s2,
23            _ => false,
24        }
25    }
26}
27
28impl std::error::Error for Error {
29    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
30        match self {
31            Self::Io(ref e) => Some(e),
32            _ => None,
33        }
34    }
35}
36
37impl fmt::Display for Error {
38    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
39        match self {
40            Self::Connect(e) => write!(f, "connect: {}", e),
41            Self::Io(e) => write!(f, "io: {}", e),
42            Self::Protocol(e) => write!(f, "protocol: {}", e),
43        }
44    }
45}
46
47impl From<std::io::Error> for Error {
48    fn from(e: std::io::Error) -> Self {
49        Error::Io(e)
50    }
51}
52
53impl From<Status> for Error {
54    fn from(s: Status) -> Self {
55        Error::Protocol(s)
56    }
57}