use std::fmt;
#[derive(Debug)]
pub enum UpdateError {
Network(String),
GitHub(String),
Version(String),
FileSystem(String),
Permission(String),
Configuration(String),
Other(String),
Unknown(String),
}
impl fmt::Display for UpdateError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
UpdateError::Network(msg) => write!(f, "网络错误: {}", msg),
UpdateError::GitHub(msg) => write!(f, "GitHub API 错误: {}", msg),
UpdateError::Version(msg) => write!(f, "版本解析错误: {}", msg),
UpdateError::FileSystem(msg) => write!(f, "文件系统错误: {}", msg),
UpdateError::Permission(msg) => write!(f, "权限错误: {}", msg),
UpdateError::Configuration(msg) => write!(f, "配置错误: {}", msg),
UpdateError::Other(msg) => write!(f, "其他错误: {}", msg),
UpdateError::Unknown(msg) => write!(f, "未知错误: {}", msg),
}
}
}
impl std::error::Error for UpdateError {}
impl From<anyhow::Error> for UpdateError {
fn from(error: anyhow::Error) -> Self {
UpdateError::Unknown(error.to_string())
}
}
impl From<self_update::errors::Error> for UpdateError {
fn from(error: self_update::errors::Error) -> Self {
match error {
self_update::errors::Error::Network(_) => {
UpdateError::Network(error.to_string())
}
self_update::errors::Error::Release(_) => {
UpdateError::GitHub(error.to_string())
}
_ => UpdateError::Unknown(error.to_string()),
}
}
}
pub type UpdateResult<T> = Result<T, UpdateError>;