Skip to main content

datex_core/shared_values/
errors.rs

1use crate::shared_values::SharedContainerOwnership;
2use core::fmt::Display;
3
4#[derive(Debug, Clone, PartialEq, Eq)]
5pub struct UnexpectedSharedContainerOwnershipError {
6    pub expected: SharedContainerOwnership,
7    pub actual: SharedContainerOwnership,
8}
9
10#[derive(Debug, Clone, PartialEq, Eq)]
11pub struct UnexpectedImmutableSharedContainerError;
12
13#[derive(Debug, Clone, PartialEq, Eq)]
14pub struct UnexpectedImmutableReferenceError;
15
16impl Display for UnexpectedSharedContainerOwnershipError {
17    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
18        write!(
19            f,
20            "Unexpected shared container ownership: expected {:?}, actual {:?}",
21            self.expected, self.actual
22        )
23    }
24}
25
26impl Display for UnexpectedImmutableSharedContainerError {
27    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
28        write!(f, "Unexpected immutable shared container")
29    }
30}
31
32impl Display for UnexpectedImmutableReferenceError {
33    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
34        write!(f, "Unexpected immutable reference")
35    }
36}
37
38use crate::{
39    prelude::*,
40    types::error::TypeError,
41    values::{
42        core_values::map::MapAccessError, value_container::ValueContainer,
43    },
44};
45
46#[derive(Debug, Clone, PartialEq, Eq, Hash)]
47pub enum SharedValueCreationError {
48    InvalidType,
49    MutabilityMismatch,
50}
51
52impl Display for SharedValueCreationError {
53    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
54        match self {
55            SharedValueCreationError::InvalidType => {
56                write!(
57                    f,
58                    "Cannot create shared value from value container: invalid type"
59                )
60            }
61            SharedValueCreationError::MutabilityMismatch => {
62                write!(
63                    f,
64                    "Cannot create mutable shared value for immutable value"
65                )
66            }
67        }
68    }
69}
70
71#[derive(Debug, Clone, PartialEq, Eq, Hash)]
72pub struct IndexOutOfBoundsError {
73    pub index: u32,
74}
75
76impl Display for IndexOutOfBoundsError {
77    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
78        write!(f, "Index out of bounds: {}", self.index)
79    }
80}
81
82#[derive(Debug, Clone, PartialEq)]
83pub struct KeyNotFoundError {
84    pub key: ValueContainer,
85}
86
87impl Display for KeyNotFoundError {
88    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
89        write!(f, "Property not found: {}", self.key)
90    }
91}
92
93#[derive(Debug, Clone, PartialEq)]
94pub enum AccessError {
95    InvalidOperation(String),
96    KeyNotFound(KeyNotFoundError),
97    IndexOutOfBounds(IndexOutOfBoundsError),
98    MapAccessError(MapAccessError),
99    InvalidIndexKey,
100}
101
102impl From<IndexOutOfBoundsError> for AccessError {
103    fn from(err: IndexOutOfBoundsError) -> Self {
104        AccessError::IndexOutOfBounds(err)
105    }
106}
107
108impl From<MapAccessError> for AccessError {
109    fn from(err: MapAccessError) -> Self {
110        AccessError::MapAccessError(err)
111    }
112}
113
114impl From<KeyNotFoundError> for AccessError {
115    fn from(err: KeyNotFoundError) -> Self {
116        AccessError::KeyNotFound(err)
117    }
118}
119
120impl Display for AccessError {
121    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
122        match self {
123            AccessError::MapAccessError(err) => {
124                write!(f, "Map access error: {}", err)
125            }
126            AccessError::InvalidOperation(op) => {
127                write!(f, "Invalid operation: {}", op)
128            }
129            AccessError::KeyNotFound(key) => {
130                write!(f, "{}", key)
131            }
132            AccessError::IndexOutOfBounds(error) => {
133                write!(f, "{}", error)
134            }
135            AccessError::InvalidIndexKey => {
136                write!(f, "Invalid index key")
137            }
138        }
139    }
140}
141
142#[derive(Debug)]
143pub enum AssignmentError {
144    ImmutableReference,
145    TypeError(Box<TypeError>),
146}
147
148impl Display for AssignmentError {
149    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
150        match self {
151            AssignmentError::ImmutableReference => {
152                write!(f, "Cannot assign to an immutable reference")
153            }
154            AssignmentError::TypeError(e) => {
155                write!(f, "Type error: {}", e)
156            }
157        }
158    }
159}