use std::fmt;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug)]
pub enum Error {
RefCounterInit(String),
Acquire(String),
Release(String),
Count(String),
Lock(String),
ServiceStart(String),
ServiceUnhealthy(String),
ServiceRecovery(String),
ServiceInfo(String),
Platform(String),
Io(std::io::Error),
Other(String),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::RefCounterInit(msg) => write!(f, "RefCounter initialization failed: {}", msg),
Error::Acquire(msg) => write!(f, "Failed to acquire reference: {}", msg),
Error::Release(msg) => write!(f, "Failed to release reference: {}", msg),
Error::Count(msg) => write!(f, "Failed to get count: {}", msg),
Error::Lock(msg) => write!(f, "Lock operation failed: {}", msg),
Error::ServiceStart(msg) => write!(f, "Service startup failed: {}", msg),
Error::ServiceUnhealthy(msg) => write!(f, "Service is unhealthy: {}", msg),
Error::ServiceRecovery(msg) => write!(f, "Service recovery failed: {}", msg),
Error::ServiceInfo(msg) => write!(f, "Service info error: {}", msg),
Error::Platform(msg) => write!(f, "Platform error: {}", msg),
Error::Io(e) => write!(f, "I/O error: {}", e),
Error::Other(msg) => write!(f, "{}", msg),
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::Io(e) => Some(e),
_ => None,
}
}
}
impl From<std::io::Error> for Error {
fn from(e: std::io::Error) -> Self {
Error::Io(e)
}
}
impl From<String> for Error {
fn from(s: String) -> Self {
Error::Other(s)
}
}
impl From<&str> for Error {
fn from(s: &str) -> Self {
Error::Other(s.to_string())
}
}