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, Eq)]
83pub struct KeyNotFoundError {
84    pub key: Box<ValueContainer>,
85}
86impl KeyNotFoundError {
87    pub fn new(key: ValueContainer) -> Self {
88        KeyNotFoundError { key: Box::new(key) }
89    }
90}
91
92impl Display for KeyNotFoundError {
93    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
94        write!(f, "Property not found: {}", self.key)
95    }
96}
97
98#[derive(Debug, Clone, PartialEq, Eq)]
99pub enum AccessError {
100    InvalidOperation(String),
101    KeyNotFound(KeyNotFoundError),
102    IndexOutOfBounds(IndexOutOfBoundsError),
103    MapAccessError(MapAccessError),
104    InvalidIndexKey,
105}
106
107impl From<IndexOutOfBoundsError> for AccessError {
108    fn from(err: IndexOutOfBoundsError) -> Self {
109        AccessError::IndexOutOfBounds(err)
110    }
111}
112
113impl From<MapAccessError> for AccessError {
114    fn from(err: MapAccessError) -> Self {
115        AccessError::MapAccessError(err)
116    }
117}
118
119impl From<KeyNotFoundError> for AccessError {
120    fn from(err: KeyNotFoundError) -> Self {
121        AccessError::KeyNotFound(err)
122    }
123}
124
125impl Display for AccessError {
126    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
127        match self {
128            AccessError::MapAccessError(err) => {
129                write!(f, "Map access error: {}", err)
130            }
131            AccessError::InvalidOperation(op) => {
132                write!(f, "Invalid operation: {}", op)
133            }
134            AccessError::KeyNotFound(key) => {
135                write!(f, "{}", key)
136            }
137            AccessError::IndexOutOfBounds(error) => {
138                write!(f, "{}", error)
139            }
140            AccessError::InvalidIndexKey => {
141                write!(f, "Invalid index key")
142            }
143        }
144    }
145}
146
147#[derive(Debug, PartialEq, Eq)]
148pub enum AssignmentError {
149    ImmutableReference,
150    TypeError(Box<TypeError>),
151}
152
153impl Display for AssignmentError {
154    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
155        match self {
156            AssignmentError::ImmutableReference => {
157                write!(f, "Cannot assign to an immutable reference")
158            }
159            AssignmentError::TypeError(e) => {
160                write!(f, "Type error: {}", e)
161            }
162        }
163    }
164}