use std::fmt;
use std::io;
#[derive(Debug)]
pub enum EditorError {
StateError(String),
StorageError(String),
EventError(String),
PluginError(String),
IoError(io::Error),
ConfigError(String),
TransactionError(String),
HistoryError(String),
EngineError(String),
CacheError(String),
Unknown(String),
}
impl fmt::Display for EditorError {
fn fmt(
&self,
f: &mut fmt::Formatter<'_>,
) -> fmt::Result {
match self {
EditorError::StateError(msg) => write!(f, "State error: {}", msg),
EditorError::StorageError(msg) => {
write!(f, "Storage error: {}", msg)
},
EditorError::EventError(msg) => write!(f, "Event error: {}", msg),
EditorError::PluginError(msg) => write!(f, "Plugin error: {}", msg),
EditorError::IoError(err) => write!(f, "IO error: {}", err),
EditorError::ConfigError(msg) => write!(f, "Config error: {}", msg),
EditorError::TransactionError(msg) => {
write!(f, "Transaction error: {}", msg)
},
EditorError::HistoryError(msg) => {
write!(f, "History error: {}", msg)
},
EditorError::EngineError(msg) => write!(f, "Engine error: {}", msg),
EditorError::CacheError(msg) => write!(f, "Cache error: {}", msg),
EditorError::Unknown(msg) => write!(f, "Unknown error: {}", msg),
}
}
}
impl std::error::Error for EditorError {}
impl From<io::Error> for EditorError {
fn from(err: io::Error) -> Self {
EditorError::IoError(err)
}
}
impl From<String> for EditorError {
fn from(err: String) -> Self {
EditorError::Unknown(err)
}
}
impl From<&str> for EditorError {
fn from(err: &str) -> Self {
EditorError::Unknown(err.to_string())
}
}
pub type EditorResult<T> = Result<T, EditorError>;
pub mod error_utils {
use super::*;
pub fn map_error<T, E: std::error::Error>(
result: Result<T, E>,
context: &str,
) -> EditorResult<T> {
result.map_err(|e| EditorError::Unknown(format!("{}: {}", context, e)))
}
pub fn state_error(msg: impl Into<String>) -> EditorError {
EditorError::StateError(msg.into())
}
pub fn storage_error(msg: impl Into<String>) -> EditorError {
EditorError::StorageError(msg.into())
}
pub fn event_error(msg: impl Into<String>) -> EditorError {
EditorError::EventError(msg.into())
}
pub fn plugin_error(msg: impl Into<String>) -> EditorError {
EditorError::PluginError(msg.into())
}
pub fn config_error(msg: impl Into<String>) -> EditorError {
EditorError::ConfigError(msg.into())
}
pub fn transaction_error(msg: impl Into<String>) -> EditorError {
EditorError::TransactionError(msg.into())
}
pub fn history_error(msg: impl Into<String>) -> EditorError {
EditorError::HistoryError(msg.into())
}
pub fn engine_error(msg: impl Into<String>) -> EditorError {
EditorError::EngineError(msg.into())
}
pub fn cache_error(msg: impl Into<String>) -> EditorError {
EditorError::CacheError(msg.into())
}
}