use std::error::Error as StdError;
use std::io;
use std::result::Result as StdResult;
use thiserror::Error;
pub type Result<T, E = Error> = StdResult<T, E>;
#[derive(Error, Debug)]
pub enum Error {
#[error("Storage error: {0}")]
Storage(String),
#[error("Config error: {0}")]
Config(String),
#[error("Internal error: {0}")]
Internal(String),
#[error("IO error: {0}")]
Io(#[from] io::Error),
#[error("Fjall error: {0}")]
Fjall(#[from] fjall::Error),
#[error("Serialization error: {0}")]
Serialization(String),
#[error("Redis error: {0}")]
Redis(String),
#[error("Invalid data: {0}")]
InvalidData(String),
#[error("Not found: {0}")]
NotFound(String),
}
impl Error {
pub fn io(e: io::Error) -> Self {
Self::Io(e)
}
pub fn conf(msg: impl Into<String>) -> Self {
Self::Config(msg.into())
}
pub fn internal(msg: impl Into<String>) -> Self {
Self::Internal(msg.into())
}
pub fn internal_with_source<E: StdError + Send + Sync + 'static>(
msg: impl Into<String>,
source: E,
) -> Self {
let msg_str: String = msg.into();
Self::Internal(format!("{msg_str}: {source}"))
}
pub fn invalid_data(msg: impl Into<String>) -> Self {
Self::InvalidData(msg.into())
}
pub fn not_found(msg: impl Into<String>) -> Self {
Self::NotFound(msg.into())
}
pub fn redis(msg: impl Into<String>) -> Self {
Self::Redis(msg.into())
}
pub fn storage(msg: impl Into<String>) -> Self {
Self::Storage(msg.into())
}
pub fn wrong_type(msg: impl Into<String>) -> Self {
Self::Redis(msg.into())
}
}