use std::fmt;
#[derive(Debug)]
pub enum Error {
Io(std::io::Error),
Parse(String),
Crypto(String),
Config(String),
SectionNotFound(String),
SizeMismatch { expected: usize, actual: usize },
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::Io(e) => write!(f, "IO错误: {}", e),
Error::Parse(e) => write!(f, "解析错误: {}", e),
Error::Crypto(e) => write!(f, "加密错误: {}", e),
Error::Config(e) => write!(f, "配置错误: {}", e),
Error::SectionNotFound(name) => write!(f, "未找到section: {}", name),
Error::SizeMismatch { expected, actual } => {
write!(f, "大小不匹配: 期望 {}, 实际 {}", expected, actual)
}
}
}
}
impl std::error::Error for Error {}
impl From<std::io::Error> for Error {
fn from(e: std::io::Error) -> Self {
Error::Io(e)
}
}
impl From<serde_json::Error> for Error {
fn from(e: serde_json::Error) -> Self {
Error::Parse(format!("JSON错误: {}", e))
}
}
pub type Result<T> = std::result::Result<T, Error>;