//! Holds the definition of [`BTreeError`].

use osom_lib_alloc::traits::AllocationError;
use osom_lib_reprc::macros::reprc;

/// Represents possible errors when working with [`BTree`][super::btree::BTree].
#[reprc]
#[repr(u8)]
#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy)]
pub enum BTreeError {
    /// The underlying allocator returned an error.
    AllocationError = 0,

    /// The tree size is out of range, i.e. exceeds
    /// [`Length::MAX_LENGTH`][osom_lib_primitives::length::Length::MAX_LENGTH].
    TreeSizeOutOfRange = 1,
}

impl core::fmt::Display for BTreeError {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            BTreeError::AllocationError => write!(f, "BTreeError::AllocationError"),
            BTreeError::TreeSizeOutOfRange => write!(f, "BTreeError::TreeSizeOutOfRange"),
        }
    }
}

impl From<AllocationError> for BTreeError {
    fn from(_: AllocationError) -> Self {
        Self::AllocationError
    }
}

osom_lib_macros::unreachable_from_infallible!(BTreeError);

/// Represents possible errors when trying to clone a [`BTree`][super::btree::BTree].
#[reprc]
#[repr(u8)]
#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy)]
pub enum BTreeTryCloneError {
    /// The key cloning failed.
    KeyCloningError = 1,

    /// The value cloning failed.
    ValueCloningError = 2,

    /// Other error. Either due to allocator failure or other unexpected error.
    OtherError = 3,
}

impl core::fmt::Display for BTreeTryCloneError {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            BTreeTryCloneError::OtherError => write!(f, "BTreeTryCloneError::OtherError"),
            BTreeTryCloneError::KeyCloningError => write!(f, "BTreeTryCloneError::KeyCloningError"),
            BTreeTryCloneError::ValueCloningError => write!(f, "BTreeTryCloneError::ValueCloningError"),
        }
    }
}

impl From<AllocationError> for BTreeTryCloneError {
    fn from(_: AllocationError) -> Self {
        Self::OtherError
    }
}

osom_lib_macros::unreachable_from_infallible!(BTreeTryCloneError);
