Skip to main content

osom_lib_arrays/
errors.rs

1//! Holds definitions of various array errors.
2use osom_lib_primitives::length::LengthError;
3use osom_lib_reprc::macros::reprc;
4
5/// Represents a general issue that can occure when dealing
6/// with arrays.
7#[reprc]
8#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
9#[must_use]
10pub enum ArrayError {
11    /// The underlying allocator returned an error,
12    /// likely due to out of memory.
13    AllocationError = 0,
14
15    /// Tried to initialize an array or push to array beyond its internal limit.
16    LengthLimitExceeded = 1,
17}
18
19impl core::fmt::Display for ArrayError {
20    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
21        match self {
22            ArrayError::AllocationError => write!(f, "ArrayError::AllocationError"),
23            ArrayError::LengthLimitExceeded => write!(f, "ArrayError::LengthLimitExceeded"),
24        }
25    }
26}
27
28impl From<LengthError> for ArrayError {
29    fn from(_: LengthError) -> Self {
30        Self::LengthLimitExceeded
31    }
32}
33
34osom_lib_macros::unreachable_from_infallible!(ArrayError);
35
36#[reprc]
37#[repr(u8)]
38#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
39#[must_use]
40pub enum ArrayTryCloneError {
41    /// The underlying allocator returned an error,
42    /// likely due to out of memory.
43    ArrayError(ArrayError) = 0,
44
45    /// Tried to clone internal item in the array, but cloning failed.
46    ItemCloningError = 1,
47}
48
49osom_lib_macros::unreachable_from_infallible!(ArrayTryCloneError);
50
51impl core::fmt::Display for ArrayTryCloneError {
52    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
53        match self {
54            ArrayTryCloneError::ArrayError(err) => write!(f, "ArrayError::ArrayError({err})"),
55            ArrayTryCloneError::ItemCloningError => write!(f, "ArrayError::ItemCloningError"),
56        }
57    }
58}
59
60impl From<ArrayError> for ArrayTryCloneError {
61    fn from(err: ArrayError) -> Self {
62        Self::ArrayError(err)
63    }
64}
65
66/// Represents an error that occures when the array is empty.
67#[reprc]
68#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
69#[must_use]
70pub struct ArrayIsEmptyError;
71
72osom_lib_macros::unreachable_from_infallible!(ArrayIsEmptyError);
73
74impl core::fmt::Display for ArrayIsEmptyError {
75    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
76        write!(f, "ArrayIsEmptyError")
77    }
78}