Skip to main content

ruvio_client/
error.rs

1use std::fmt;
2
3/// A transport failure, a broken RESP frame, or a Ruvio error reply.
4#[derive(Debug)]
5pub enum Error {
6    /// The TCP connection failed or closed.
7    Io(std::io::Error),
8    /// The bytes on the socket were not a complete RESP2 value.
9    Protocol(String),
10    /// Ruvio returned a RESP error. The connection can still run the next command.
11    Server(String),
12}
13
14impl fmt::Display for Error {
15    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
16        match self {
17            Error::Io(error) => write!(formatter, "{error}"),
18            Error::Protocol(message) | Error::Server(message) => write!(formatter, "{message}"),
19        }
20    }
21}
22
23impl std::error::Error for Error {
24    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
25        match self {
26            Error::Io(error) => Some(error),
27            Error::Protocol(_) | Error::Server(_) => None,
28        }
29    }
30}
31
32impl From<std::io::Error> for Error {
33    fn from(error: std::io::Error) -> Self {
34        Error::Io(error)
35    }
36}