use std::fmt;
use super::primitives::{PositionOutOfBoundsError, TypeMismatchError};
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AttributeInvariantViolation {
ChunkIndexOutOfRange,
LastChunkLengthInconsistent,
LengthMismatch,
EmptyChunkVec,
SwapRemoveOnEmpty,
PushFromTypeMismatch,
LockPoisoned,
StillShared,
}
impl fmt::Display for AttributeInvariantViolation {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
AttributeInvariantViolation::ChunkIndexOutOfRange => {
write!(f, "chunk index out of range")
}
AttributeInvariantViolation::LastChunkLengthInconsistent => {
write!(f, "last chunk length inconsistent with stored data")
}
AttributeInvariantViolation::LengthMismatch => {
write!(f, "total length does not match sum of chunk lengths")
}
AttributeInvariantViolation::EmptyChunkVec => {
write!(
f,
"chunk vector is empty when at least one chunk is required"
)
}
AttributeInvariantViolation::SwapRemoveOnEmpty => {
write!(f, "swap-remove attempted on empty attribute")
}
AttributeInvariantViolation::PushFromTypeMismatch => {
write!(f, "push_from source attribute has a different element type")
}
AttributeInvariantViolation::LockPoisoned => {
write!(f, "RwLock poisoned by a panicking writer")
}
AttributeInvariantViolation::StillShared => {
write!(f, "Arc still has multiple owners and cannot be unwrapped")
}
}
}
}
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AttributeError {
Position(PositionOutOfBoundsError),
TypeMismatch(TypeMismatchError),
IndexOverflow(&'static str),
InternalInvariant(AttributeInvariantViolation),
}
impl fmt::Display for AttributeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
AttributeError::Position(e) => write!(f, "{e}"),
AttributeError::TypeMismatch(e) => write!(f, "{e}"),
AttributeError::IndexOverflow(which) => {
write!(f, "index overflow constructing {}", which)
}
AttributeError::InternalInvariant(violation) => {
write!(f, "internal storage invariant violated: {}", violation)
}
}
}
}
impl std::error::Error for AttributeError {}
impl From<PositionOutOfBoundsError> for AttributeError {
fn from(e: PositionOutOfBoundsError) -> Self {
AttributeError::Position(e)
}
}
impl From<TypeMismatchError> for AttributeError {
fn from(e: TypeMismatchError) -> Self {
AttributeError::TypeMismatch(e)
}
}