//! 错误类型定义 use std::fmt; /// 错误类型 #[derive(Debug, Clone)] pub enum Error { /// 验证器未找到 ValidatorNotFound(String), /// 模型未找到 ModelNotFound(String), /// 规则错误 RuleError(String), /// 验证失败 ValidationFailed(String), /// IO错误 Io(String), /// 序列化错误 Serialization(String), /// 其他错误 Other(String), } impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { Self::ValidatorNotFound(msg) => write!(f, "验证器未找到: {}", msg), Self::ModelNotFound(msg) => write!(f, "模型未找到: {}", msg), Self::RuleError(msg) => write!(f, "规则错误: {}", msg), Self::ValidationFailed(msg) => write!(f, "验证失败: {}", msg), Self::Io(msg) => write!(f, "IO错误: {}", msg), Self::Serialization(msg) => write!(f, "序列化错误: {}", msg), Self::Other(msg) => write!(f, "错误: {}", msg), } } } impl std::error::Error for Error {} impl From for Error { fn from(err: std::io::Error) -> Self { Self::Io(err.to_string()) } } impl From for Error { fn from(err: serde_json::Error) -> Self { Self::Serialization(err.to_string()) } } /// Result类型别名 pub type Result = std::result::Result;