Skip to main content

osom_lib_btree/
errors.rs

1//! Holds the definition of [`BTreeError`].
2use osom_lib_reprc::macros::reprc;
3
4use osom_lib_try_clone::TryClone;
5
6/// Represents possible errors when working with [`BTree`][super::btree::BTree].
7#[reprc]
8#[repr(u8)]
9#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy)]
10#[must_use]
11pub enum BTreeError {
12    /// The underlying allocator returned an error.
13    AllocationError = 0,
14
15    /// The tree size is out of range, i.e. exceeds
16    /// [`Length::MAX_LENGTH`][osom_lib_primitives::length::Length::MAX_LENGTH].
17    TreeSizeOutOfRange = 1,
18}
19
20impl TryClone for BTreeError {
21    type Error = core::convert::Infallible;
22
23    fn try_clone(&self) -> Result<Self, Self::Error> {
24        Ok(*self)
25    }
26}
27
28impl core::fmt::Display for BTreeError {
29    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
30        match self {
31            BTreeError::AllocationError => write!(f, "BTreeError::AllocationError"),
32            BTreeError::TreeSizeOutOfRange => write!(f, "BTreeError::TreeSizeOutOfRange"),
33        }
34    }
35}
36
37osom_lib_macros::unreachable_from_infallible!(BTreeError);
38
39/// Represents possible errors when trying to clone a [`BTree`][super::btree::BTree].
40#[reprc]
41#[repr(u8)]
42#[must_use]
43#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy)]
44pub enum BTreeTryCloneError {
45    /// The key cloning failed.
46    KeyCloningError = 1,
47
48    /// The value cloning failed.
49    ValueCloningError = 2,
50
51    /// Other error. Either due to allocator failure or other unexpected error.
52    OtherError = 3,
53}
54
55impl core::fmt::Display for BTreeTryCloneError {
56    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
57        match self {
58            BTreeTryCloneError::OtherError => write!(f, "BTreeTryCloneError::OtherError"),
59            BTreeTryCloneError::KeyCloningError => write!(f, "BTreeTryCloneError::KeyCloningError"),
60            BTreeTryCloneError::ValueCloningError => write!(f, "BTreeTryCloneError::ValueCloningError"),
61        }
62    }
63}
64
65osom_lib_macros::unreachable_from_infallible!(BTreeTryCloneError);