use crate::config::ConfigError;
use std::fmt;
use std::io;
use thiserror::Error;
#[derive(Error, Debug)]
pub enum RlgError {
#[error("I/O error: {0}")]
IoError(#[from] io::Error),
#[error("Configuration error: {0}")]
ConfigError(#[from] ConfigError),
#[error("Log format parse error: {0}")]
FormatParseError(String),
#[error("Log level parse error: {0}")]
LevelParseError(String),
#[error("Unsupported log format: {0}")]
UnsupportedFormat(String),
#[error("Log formatting error: {0}")]
FormattingError(String),
#[error("Log rotation error: {0}")]
RotationError(String),
#[error("Network error: {0}")]
NetworkError(String),
#[error("DateTime parse error: {0}")]
DateTimeParseError(String),
#[error("{0}")]
Custom(String),
}
impl RlgError {
pub fn custom<T: fmt::Display>(msg: T) -> Self {
RlgError::Custom(msg.to_string())
}
}
pub type RlgResult<T> = Result<T, RlgError>;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_error_display() {
let err =
RlgError::FormatParseError("Invalid format".to_string());
assert_eq!(
err.to_string(),
"Log format parse error: Invalid format"
);
}
#[test]
fn test_custom_error() {
let err = RlgError::custom("Custom error message");
assert_eq!(err.to_string(), "Custom error message");
}
#[test]
fn test_config_error_conversion() {
let config_err =
ConfigError::ValidationError("Test error".to_string());
let rlg_err: RlgError = config_err.into();
assert!(matches!(rlg_err, RlgError::ConfigError(_)));
}
}