use std::fmt;
pub type Result<T> = std::result::Result<T, ClientError>;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ErrorCode {
UnsupportedScheme,
InvalidUri,
IoError,
QueryError,
FeatureDisabled,
ClientClosed,
Internal,
Network,
Protocol,
AuthRefused,
Engine,
NotFound,
ParamsUnsupported,
}
impl ErrorCode {
pub fn as_str(self) -> &'static str {
match self {
ErrorCode::UnsupportedScheme => "UNSUPPORTED_SCHEME",
ErrorCode::InvalidUri => "INVALID_URI",
ErrorCode::IoError => "IO_ERROR",
ErrorCode::QueryError => "QUERY_ERROR",
ErrorCode::FeatureDisabled => "FEATURE_DISABLED",
ErrorCode::ClientClosed => "CLIENT_CLOSED",
ErrorCode::Internal => "INTERNAL_ERROR",
ErrorCode::Network => "NETWORK_ERROR",
ErrorCode::Protocol => "PROTOCOL_ERROR",
ErrorCode::AuthRefused => "AUTH_REFUSED",
ErrorCode::Engine => "ENGINE_ERROR",
ErrorCode::NotFound => "NOT_FOUND",
ErrorCode::ParamsUnsupported => "PARAMS_UNSUPPORTED",
}
}
}
impl fmt::Display for ErrorCode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Debug, Clone)]
pub struct ClientError {
pub code: ErrorCode,
pub message: String,
}
impl ClientError {
pub fn new(code: ErrorCode, message: impl Into<String>) -> Self {
Self {
code,
message: message.into(),
}
}
pub fn unsupported_scheme(scheme: impl AsRef<str>) -> Self {
Self::new(
ErrorCode::UnsupportedScheme,
format!(
"unsupported URI scheme: '{}'. Expected 'file://', 'memory://' or 'grpc://'.",
scheme.as_ref()
),
)
}
pub fn feature_disabled(feature: &str) -> Self {
Self::new(
ErrorCode::FeatureDisabled,
format!(
"the '{feature}' feature is not enabled. Enable it in Cargo.toml: \
reddb-client = {{ version = \"…\", features = [\"{feature}\"] }}"
),
)
}
}
impl fmt::Display for ClientError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "[{}] {}", self.code, self.message)
}
}
impl std::error::Error for ClientError {}