use crate::RespCode;
use core::fmt;
#[derive(Debug, PartialEq)]
#[non_exhaustive]
pub enum SkyhashError {
ParseError,
UnexpectedDataType,
UnexpectedResponse,
UnknownDataType,
InvalidResponse,
Code(RespCode),
}
pub mod errorstring {
pub const DEFAULT_CONTAINER_UNSET: &str = "default-container-unset";
pub const CONTAINER_NOT_FOUND: &str = "container-not-found";
pub const STILL_IN_USE: &str = "still-in-use";
pub const ERR_PROTECTED_OBJECT: &str = "err-protected-object";
pub const ERR_ALREADY_EXISTS: &str = "err-already-exists";
pub const ERR_NOT_READY: &str = "not-ready";
pub const ERR_SNAPSHOT_BUSY: &str = "err-snapshot-busy";
pub const ERR_SNAPSHOT_DISABLED: &str = "err-snapshot-disabled";
}
#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
IoError(std::io::Error),
#[cfg(any(
feature = "ssl",
feature = "sslv",
feature = "aio-ssl",
feature = "aio-sslv"
))]
#[cfg_attr(
docsrs,
doc(cfg(any(
feature = "ssl",
feature = "sslv",
feature = "aio-ssl",
feature = "aio-sslv"
)))
)]
SslError(openssl::ssl::Error),
SkyError(SkyhashError),
ParseError(String),
ConfigurationError(&'static str),
}
impl PartialEq for Error {
fn eq(&self, oth: &Error) -> bool {
use Error::*;
match (self, oth) {
(IoError(a), IoError(b)) => a.kind().eq(&b.kind()),
(SkyError(a), SkyError(b)) => a == b,
(ParseError(a), ParseError(b)) => a == b,
#[cfg(any(
feature = "ssl",
feature = "sslv",
feature = "aio-ssl",
feature = "aio-sslv"
))]
#[cfg_attr(
docsrs,
doc(cfg(any(
feature = "ssl",
feature = "sslv",
feature = "aio-ssl",
feature = "aio-sslv"
)))
)]
(SslError(a), SslError(b)) => a.to_string() == b.to_string(),
(ConfigurationError(a), ConfigurationError(b)) => a == b,
_ => false,
}
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::IoError(eio) => write!(f, "{}", eio),
#[cfg(any(
feature = "ssl",
feature = "sslv",
feature = "aio-ssl",
feature = "aio-sslv"
))]
#[cfg_attr(
docsrs,
doc(cfg(any(
feature = "ssl",
feature = "sslv",
feature = "aio-ssl",
feature = "aio-sslv"
)))
)]
Self::SslError(essl) => write!(f, "{}", essl),
Self::ParseError(apperr) => {
write!(f, "custom type parse error: {}", apperr)
}
Self::SkyError(eproto) => match eproto {
SkyhashError::Code(rcode) => write!(f, "{}", rcode),
SkyhashError::InvalidResponse => {
write!(f, "Invalid Skyhash response received from server")
}
SkyhashError::UnexpectedResponse => {
write!(f, "Unexpected response from server")
}
SkyhashError::ParseError => write!(f, "Client-side datatype parse error"),
SkyhashError::UnexpectedDataType => write!(f, "Wrong type sent by server"),
SkyhashError::UnknownDataType => {
write!(f, "Server sent unknown data type for this client version")
}
},
Self::ConfigurationError(e) => write!(f, "Configuration error: {}", e),
}
}
}
cfg_ssl_any! {
impl From<openssl::ssl::Error> for Error {
fn from(err: openssl::ssl::Error) -> Self {
Self::SslError(err)
}
}
impl From<openssl::error::ErrorStack> for Error {
fn from(e: openssl::error::ErrorStack) -> Self {
let e: openssl::ssl::Error = e.into();
Self::SslError(e)
}
}
}
impl From<std::io::Error> for Error {
fn from(err: std::io::Error) -> Self {
Self::IoError(err)
}
}
impl From<SkyhashError> for Error {
fn from(err: SkyhashError) -> Self {
Self::SkyError(err)
}
}
impl From<std::num::ParseIntError> for Error {
fn from(e: std::num::ParseIntError) -> Self {
Self::ParseError(e.to_string())
}
}
impl From<std::num::ParseFloatError> for Error {
fn from(e: std::num::ParseFloatError) -> Self {
Self::ParseError(e.to_string())
}
}
impl From<std::num::TryFromIntError> for Error {
fn from(e: std::num::TryFromIntError) -> Self {
Self::ParseError(e.to_string())
}
}
impl From<std::convert::Infallible> for Error {
fn from(_: std::convert::Infallible) -> Self {
unsafe { core::hint::unreachable_unchecked() }
}
}
impl From<std::string::FromUtf8Error> for Error {
fn from(e: std::string::FromUtf8Error) -> Self {
Self::ParseError(e.to_string())
}
}
impl std::error::Error for Error {}