use std::error::Error as StdError;
use openraft::ErrorSubject;
use openraft::ErrorVerb;
use openraft::StorageError;
use openraft::alias::LogIdOf;
use openraft::alias::VoteOf;
use openraft::errors::ErrorSource;
use crate::raft::types::RaftCodec;
use crate::raft::types::TypeConfig;
pub trait StoreMeta {
const KEY: &'static str;
type Value: RaftCodec;
fn subject(v: Option<&Self::Value>) -> ErrorSubject<TypeConfig>;
fn read_err(e: impl StdError + 'static) -> StorageError<TypeConfig> {
StorageError::new(
Self::subject(None),
ErrorVerb::Read,
openraft::impls::BoxedErrorSource::from_error(&e),
)
}
fn write_err(v: &Self::Value, e: impl StdError + 'static) -> StorageError<TypeConfig> {
StorageError::new(
Self::subject(Some(v)),
ErrorVerb::Write,
openraft::impls::BoxedErrorSource::from_error(&e),
)
}
fn delete_err(e: impl StdError + 'static) -> StorageError<TypeConfig> {
StorageError::new(
Self::subject(None),
ErrorVerb::Delete,
openraft::impls::BoxedErrorSource::from_error(&e),
)
}
}
pub(crate) struct LastPurged {}
pub(crate) struct Vote {}
pub(crate) struct Committed {}
impl StoreMeta for LastPurged {
const KEY: &'static str = "last_purged_log_id";
type Value = LogIdOf<TypeConfig>;
fn subject(_v: Option<&Self::Value>) -> ErrorSubject<TypeConfig> {
ErrorSubject::Store
}
}
impl StoreMeta for Vote {
const KEY: &'static str = "vote";
type Value = VoteOf<TypeConfig>;
fn subject(_v: Option<&Self::Value>) -> ErrorSubject<TypeConfig> {
ErrorSubject::Vote
}
}
impl StoreMeta for Committed {
const KEY: &'static str = "committed";
type Value = LogIdOf<TypeConfig>;
fn subject(_v: Option<&Self::Value>) -> ErrorSubject<TypeConfig> {
ErrorSubject::Store
}
}