log-full 0.0.1

A simple, asynchronous log library
Documentation
//! 错误处理模块
//! 
//! 提供统一的错误类型定义和处理机制

/// 日志库的主要错误类型
#[derive(Debug)]
pub enum LogError {
    /// IO 错误
    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 {}

// 为LogError实现From trait以支持自动类型转换
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(),
        }
    }
    
    
}