NAC_Blockchain/nac-ai-compliance/src/error.rs

54 lines
1.4 KiB
Rust

//! 错误类型定义
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<std::io::Error> for Error {
fn from(err: std::io::Error) -> Self {
Self::Io(err.to_string())
}
}
impl From<serde_json::Error> for Error {
fn from(err: serde_json::Error) -> Self {
Self::Serialization(err.to_string())
}
}
/// Result类型别名
pub type Result<T> = std::result::Result<T, Error>;