Skip to main content

osom_lib_hash_tables/
errors.rs

1//! Holds definitions of various array errors.
2use osom_lib_reprc::macros::reprc;
3
4/// Represents a general issue that can occure when dealing
5/// with hash tables.
6#[reprc]
7#[repr(u8)]
8#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
9#[must_use]
10pub enum HashTableError {
11    /// The underlying allocator returned an error,
12    /// likely due to out of memory.
13    AllocationError = 0,
14
15    /// Tried to initialize a hash table or push to it beyond its internal limit.
16    LengthLimitExceeded = 1,
17
18    /// The underlying allocator cloning failed.
19    AllocatorCloningError = 2,
20}
21
22osom_lib_macros::unreachable_from_infallible!(HashTableError);
23
24impl core::fmt::Display for HashTableError {
25    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
26        match self {
27            HashTableError::AllocationError => write!(f, "HashTableError::AllocationError"),
28            HashTableError::LengthLimitExceeded => write!(f, "HashTableError::LengthLimitExceeded"),
29            HashTableError::AllocatorCloningError => write!(f, "HashTableError::AllocatorCloningError"),
30        }
31    }
32}
33
34/// Represents possible errors when trying to clone a hash table.
35#[reprc]
36#[repr(u8)]
37#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
38#[must_use]
39pub enum TryCloneHashTableError {
40    /// The underlying hash table returned an error.
41    HashTableError(HashTableError) = 0,
42
43    /// The key or value returned an error.
44    KeyOrValueError = 1,
45}
46
47osom_lib_macros::unreachable_from_infallible!(TryCloneHashTableError);
48
49impl From<HashTableError> for TryCloneHashTableError {
50    fn from(error: HashTableError) -> Self {
51        Self::HashTableError(error)
52    }
53}
54
55impl core::fmt::Display for TryCloneHashTableError {
56    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
57        match self {
58            TryCloneHashTableError::HashTableError(error) => {
59                write!(f, "TryCloneHashTableError::HashTableError({error})")
60            }
61            TryCloneHashTableError::KeyOrValueError => write!(f, "TryCloneHashTableError::KeyOrValueError"),
62        }
63    }
64}