use crate::common::LogId;
use crate::server::replication::AppendEntriesResponse;
use crate::server::replication::ConflictResult;
use crate::server::replication::SuccessResult;
use crate::server::replication::append_entries_response;
impl AppendEntriesResponse {
pub fn success(
node_id: u32,
term: u64,
last_match: Option<LogId>,
) -> Self {
Self {
node_id,
term,
result: Some(append_entries_response::Result::Success(SuccessResult {
last_match,
})),
}
}
pub fn conflict(
node_id: u32,
term: u64,
conflict_term: Option<u64>,
conflict_index: Option<u64>,
) -> Self {
Self {
node_id,
term,
result: Some(append_entries_response::Result::Conflict(ConflictResult {
conflict_term,
conflict_index,
})),
}
}
pub fn higher_term(
node_id: u32,
term: u64,
) -> Self {
Self {
node_id,
term,
result: Some(append_entries_response::Result::HigherTerm(term)),
}
}
pub fn is_success(&self) -> bool {
matches!(
&self.result,
Some(append_entries_response::Result::Success(_))
)
}
pub fn is_conflict(&self) -> bool {
matches!(
&self.result,
Some(append_entries_response::Result::Conflict(_conflict))
)
}
pub fn is_higher_term(&self) -> bool {
matches!(
&self.result,
Some(append_entries_response::Result::HigherTerm(_))
)
}
}