#[derive(Debug)]
pub enum LogError {
Io(std::io::Error),
Format(std::fmt::Error),
Config { message: String },
FileOperation { path: String, reason: String },
InvalidLogLevel { level: String },
InitializationError { message: String },
Parse(std::num::ParseIntError),
Custom { message: String },
}
impl std::fmt::Display for LogError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
LogError::Io(err) => write!(f, "IO error: {}", err),
LogError::Format(err) => write!(f, "Format error: {}", err),
LogError::Config { message } => write!(f, "Configuration error: {}", message),
LogError::FileOperation { path, reason } => write!(f, "File operation error: {} - {}", path, reason),
LogError::InvalidLogLevel { level } => write!(f, "Invalid log level: {}", level),
LogError::InitializationError { message } => write!(f, "Initialization error: {}", message),
LogError::Parse(e) => write!(f, "Parse error: {}", e),
LogError::Custom { message } => write!(f, "Custom error: {}", message),
}
}
}
impl std::error::Error for LogError {}
impl From<std::io::Error> for LogError {
fn from(err: std::io::Error) -> Self {
LogError::Io(err)
}
}
impl From<std::fmt::Error> for LogError {
fn from(err: std::fmt::Error) -> Self {
LogError::Format(err)
}
}
impl From<std::num::ParseIntError> for LogError {
fn from(err: std::num::ParseIntError) -> Self {
LogError::Parse(err)
}
}
impl From<String> for LogError {
fn from(message: String) -> Self {
LogError::Custom { message }
}
}
impl From<&str> for LogError {
fn from(message: &str) -> Self {
LogError::Custom { message: message.to_string() }
}
}
pub type LogResult<T> = Result<T, LogError>;
impl LogError {
pub fn config<S: Into<String>>(message: S) -> Self {
LogError::Config {
message: message.into(),
}
}
pub fn file_operation<P: Into<String>, R: Into<String>>(path: P, reason: R) -> Self {
LogError::FileOperation {
path: path.into(),
reason: reason.into(),
}
}
pub fn invalid_log_level<S: Into<String>>(level: S) -> Self {
LogError::InvalidLogLevel {
level: level.into(),
}
}
pub fn initialization_error<S: Into<String>>(message: S) -> Self {
LogError::InitializationError {
message: message.into(),
}
}
pub fn custom<S: Into<String>>(message: S) -> Self {
LogError::Custom {
message: message.into(),
}
}
}