use crate::schema::types::SchemaError;
use std::fmt;
use std::io;
#[derive(Debug)]
pub enum FoldDbError {
Schema(SchemaError),
Database(String),
Permission(String),
Config(String),
Network(NetworkErrorKind),
Io(io::Error),
Serialization(String),
Payment(String),
SecurityError(String),
Other(String),
}
#[derive(Debug)]
pub enum NetworkErrorKind {
Connection(String),
Discovery(String),
Message(String),
Authentication(String),
Trust(String),
Config(String),
Timeout(String),
Protocol(String),
}
impl fmt::Display for NetworkErrorKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Connection(msg) => write!(f, "Connection error: {}", msg),
Self::Discovery(msg) => write!(f, "Discovery error: {}", msg),
Self::Message(msg) => write!(f, "Message error: {}", msg),
Self::Authentication(msg) => write!(f, "Authentication error: {}", msg),
Self::Trust(msg) => write!(f, "Trust error: {}", msg),
Self::Config(msg) => write!(f, "Configuration error: {}", msg),
Self::Timeout(msg) => write!(f, "Timeout error: {}", msg),
Self::Protocol(msg) => write!(f, "Protocol error: {}", msg),
}
}
}
impl fmt::Display for FoldDbError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Schema(err) => write!(f, "Schema error: {}", err),
Self::Database(msg) => write!(f, "Database error: {}", msg),
Self::Permission(msg) => write!(f, "Permission error: {}", msg),
Self::Config(msg) => write!(f, "Configuration error: {}", msg),
Self::Network(err) => write!(f, "Network error: {}", err),
Self::Io(err) => write!(f, "IO error: {}", err),
Self::Serialization(msg) => write!(f, "Serialization error: {}", msg),
Self::Payment(msg) => write!(f, "Payment error: {}", msg),
Self::SecurityError(msg) => write!(f, "Security error: {}", msg),
Self::Other(msg) => write!(f, "Error: {}", msg),
}
}
}
impl std::error::Error for FoldDbError {}
impl From<SchemaError> for FoldDbError {
fn from(error: SchemaError) -> Self {
FoldDbError::Schema(error)
}
}
impl From<io::Error> for FoldDbError {
fn from(error: io::Error) -> Self {
FoldDbError::Io(error)
}
}
impl From<serde_json::Error> for FoldDbError {
fn from(error: serde_json::Error) -> Self {
FoldDbError::Serialization(error.to_string())
}
}
impl From<sled::Error> for FoldDbError {
fn from(error: sled::Error) -> Self {
FoldDbError::Database(error.to_string())
}
}
impl From<&str> for NetworkErrorKind {
fn from(msg: &str) -> Self {
NetworkErrorKind::Protocol(msg.to_string())
}
}
pub type FoldDbResult<T> = Result<T, FoldDbError>;