use crate::FdbError;
use crate::tuple::PackError;
use std::fmt;
#[derive(Debug)]
pub enum LeaderElectionError {
ElectionDisabled,
ProcessNotFound(String),
NotInitialized,
InvalidState(String),
Fdb(FdbError),
PackError(PackError),
UnregisteredCandidate,
}
impl fmt::Display for LeaderElectionError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::ElectionDisabled => write!(f, "Election is currently disabled"),
Self::ProcessNotFound(id) => write!(f, "Process not found: {id}"),
Self::NotInitialized => write!(f, "Global configuration not initialized"),
Self::InvalidState(msg) => write!(f, "Invalid state: {msg}"),
Self::Fdb(e) => write!(f, "Database error: {e}"),
Self::PackError(e) => write!(f, "Pack error: {e:?}"),
Self::UnregisteredCandidate => write!(f, "Candidate not registered"),
}
}
}
impl std::error::Error for LeaderElectionError {}
impl From<FdbError> for LeaderElectionError {
fn from(error: FdbError) -> Self {
Self::Fdb(error)
}
}
impl From<PackError> for LeaderElectionError {
fn from(error: PackError) -> Self {
Self::PackError(error)
}
}
pub type Result<T> = std::result::Result<T, LeaderElectionError>;