use thiserror::Error;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug, Error)]
pub enum Error {
#[error("IO error: {0}")]
Io(#[from] std::io::Error),
#[error("Block not found: {0}")]
BlockNotFound(String),
#[error("CID error: {0}")]
Cid(String),
#[error("Serialization error: {0}")]
Serialization(String),
#[error("Deserialization error: {0}")]
Deserialization(String),
#[error("Network error: {0}")]
Network(String),
#[error("Storage error: {0}")]
Storage(String),
#[error("Encryption error: {0}")]
Encryption(String),
#[error("Invalid data: {0}")]
InvalidData(String),
#[error("Invalid input: {0}")]
InvalidInput(String),
#[error("Not found: {0}")]
NotFound(String),
#[error("Protocol error: {0}")]
Protocol(String),
#[error("Not implemented: {0}")]
NotImplemented(String),
#[error("Internal error: {0}")]
Internal(String),
#[error("Initialization error: {0}")]
Initialization(String),
#[error("Verification error: {0}")]
Verification(String),
}
impl Error {
#[inline]
pub const fn is_io(&self) -> bool {
matches!(self, Error::Io(_))
}
#[inline]
pub const fn is_not_found(&self) -> bool {
matches!(self, Error::BlockNotFound(_) | Error::NotFound(_))
}
#[inline]
pub const fn is_serialization(&self) -> bool {
matches!(self, Error::Serialization(_) | Error::Deserialization(_))
}
#[inline]
pub const fn is_network(&self) -> bool {
matches!(self, Error::Network(_))
}
#[inline]
pub const fn is_storage(&self) -> bool {
matches!(self, Error::Storage(_))
}
#[inline]
pub const fn is_validation(&self) -> bool {
matches!(self, Error::InvalidData(_) | Error::InvalidInput(_))
}
#[inline]
pub const fn is_cid(&self) -> bool {
matches!(self, Error::Cid(_))
}
#[inline]
pub const fn is_verification(&self) -> bool {
matches!(self, Error::Verification(_))
}
pub const fn category(&self) -> &'static str {
match self {
Error::Io(_) => "io",
Error::BlockNotFound(_) => "not_found",
Error::Cid(_) => "cid",
Error::Serialization(_) => "serialization",
Error::Deserialization(_) => "deserialization",
Error::Network(_) => "network",
Error::Storage(_) => "storage",
Error::Encryption(_) => "encryption",
Error::InvalidData(_) => "invalid_data",
Error::InvalidInput(_) => "invalid_input",
Error::NotFound(_) => "not_found",
Error::Protocol(_) => "protocol",
Error::NotImplemented(_) => "not_implemented",
Error::Internal(_) => "internal",
Error::Initialization(_) => "initialization",
Error::Verification(_) => "verification",
}
}
pub const fn is_recoverable(&self) -> bool {
matches!(self, Error::Io(_) | Error::Network(_) | Error::Storage(_))
}
pub const fn is_internal(&self) -> bool {
matches!(self, Error::Internal(_))
}
}