use core::convert::Infallible;
use core::error::Error;
use core::fmt::{Debug, Display, Formatter};
use deep_causality_context_store::ProjectionError;
#[derive(Debug, Clone, PartialEq)]
pub struct StoreError<E, B = Infallible>(pub StoreErrorEnum<E, B>);
impl<E: Debug + Display, B: Debug + Display> Error for StoreError<E, B> {}
#[derive(Debug, Clone, PartialEq)]
pub enum StoreErrorEnum<E, B> {
Storage(E),
Substrate(B),
Projection(ProjectionError),
}
impl<E, B> StoreError<E, B> {
pub const fn new(kind: StoreErrorEnum<E, B>) -> Self {
Self(kind)
}
pub const fn kind(&self) -> &StoreErrorEnum<E, B> {
&self.0
}
#[allow(non_snake_case)]
pub const fn Storage(error: E) -> Self {
Self(StoreErrorEnum::Storage(error))
}
#[allow(non_snake_case)]
pub const fn Substrate(error: B) -> Self {
Self(StoreErrorEnum::Substrate(error))
}
#[allow(non_snake_case)]
pub const fn Projection(error: ProjectionError) -> Self {
Self(StoreErrorEnum::Projection(error))
}
}
impl<E, B> From<ProjectionError> for StoreError<E, B> {
fn from(error: ProjectionError) -> Self {
Self::Projection(error)
}
}
impl<E: Display, B: Display> Display for StoreError<E, B> {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
match &self.0 {
StoreErrorEnum::Storage(error) => write!(f, "StoreError: storage: {error}"),
StoreErrorEnum::Substrate(error) => write!(f, "StoreError: substrate: {error}"),
StoreErrorEnum::Projection(error) => write!(f, "StoreError: projection: {error}"),
}
}
}