use crate::common::NodeStatus;
use crate::server::cluster::ClusterConfUpdateResponse;
use crate::server::cluster::cluster_conf_update_response::ErrorCode;
impl ClusterConfUpdateResponse {
pub fn success(
node_id: u32,
term: u64,
version: u64,
) -> Self {
Self {
id: node_id,
term,
version,
success: true,
error_code: ErrorCode::Unspecified.into(),
}
}
pub fn higher_term(
node_id: u32,
term: u64,
version: u64,
) -> Self {
Self {
id: node_id,
term,
version,
success: false,
error_code: ErrorCode::TermOutdated.into(),
}
}
pub fn not_leader(
node_id: u32,
term: u64,
version: u64,
) -> Self {
Self {
id: node_id,
term,
version,
success: false,
error_code: ErrorCode::NotLeader.into(),
}
}
pub fn version_conflict(
node_id: u32,
term: u64,
version: u64,
) -> Self {
Self {
id: node_id,
term,
version,
success: false,
error_code: ErrorCode::VersionConflict.into(),
}
}
#[allow(unused)]
pub fn invalid_change(
node_id: u32,
term: u64,
version: u64,
) -> Self {
Self {
id: node_id,
term,
version,
success: false,
error_code: ErrorCode::InvalidChange.into(),
}
}
pub fn internal_error(
node_id: u32,
term: u64,
version: u64,
) -> Self {
Self {
id: node_id,
term,
version,
success: false,
error_code: ErrorCode::InternalError.into(),
}
}
#[allow(unused)]
pub fn is_higher_term(&self) -> bool {
self.error_code == <ErrorCode as Into<i32>>::into(ErrorCode::TermOutdated)
}
}
impl NodeStatus {
pub fn is_promotable(&self) -> bool {
matches!(self, NodeStatus::Promotable)
}
pub fn is_i32_promotable(value: i32) -> bool {
matches!(value, v if v == (NodeStatus::Promotable as i32))
}
}