use std::fmt;
use std::fmt::Formatter;
use display_more::DisplayOptionExt;
use display_more::DisplayResultExt;
use crate::RaftTypeConfig;
use crate::StorageError;
use crate::core::ApplyResult;
use crate::raft_state::io_state::log_io_id::LogIOId;
use crate::type_config::alias::SnapshotMetaOf;
#[derive(Debug)]
pub(crate) enum Response<C>
where C: RaftTypeConfig
{
BuildSnapshotDone(Option<SnapshotMetaOf<C>>),
InstallSnapshot((LogIOId<C>, Option<SnapshotMetaOf<C>>)),
Apply(ApplyResult<C>),
}
impl<C> fmt::Display for Response<C>
where C: RaftTypeConfig
{
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
Self::BuildSnapshotDone(meta) => {
write!(f, "BuildSnapshotDone({})", meta.display())
}
Self::InstallSnapshot((io_id, meta)) => {
write!(f, "InstallSnapshot(io_id:{}, meta:{})", io_id, meta.display())
}
Self::Apply(result) => {
write!(f, "{}", result)
}
}
}
}
#[derive(Debug)]
pub(crate) struct CommandResult<C>
where C: RaftTypeConfig
{
pub(crate) result: Result<Response<C>, StorageError<C>>,
}
impl<C> fmt::Display for CommandResult<C>
where C: RaftTypeConfig
{
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "sm::Result({})", self.result.display())
}
}
impl<C> CommandResult<C>
where C: RaftTypeConfig
{
pub(crate) fn new(result: Result<Response<C>, StorageError<C>>) -> Self {
Self { result }
}
}