datex_core/values/value_container/
mod.rs1use crate::{
4 traits::convert_value_container::ConvertValueContainer, utils::sheep::Sheep,
5};
6mod equality;
7mod identity;
8mod serde_dif;
9mod try_clone;
10use super::value::Value;
11use crate::{
12 prelude::*,
13 shared_values::SharedContainer,
14 types::{
15 r#type::Type,
16 type_definition::TypeDefinition,
17 type_definition_with_metadata::{
18 TypeDefinitionWithMetadata, TypeMetadata,
19 },
20 },
21};
22
23pub mod borrowed_value_container;
24
25mod apply;
26mod update_handler;
27use crate::{
28 shared_values::{
29 OwnedSharedContainer, ReferencedSharedContainer,
30 collapsed_container_value::{
31 CollapsedContainerValue, CollapsedContainerValueMut,
32 },
33 traits::SharedContainerCommon,
34 },
35 traits::convert_parts::FromParts,
36 utils::{
37 impl_display_for_datex_value::impl_display_for_datex_value,
38 sheep_mut::SheepMut,
39 },
40 values::core_values::endpoint::Endpoint,
41};
42use core::{
43 hash::{Hash, Hasher},
44 ops::FnOnce,
45};
46
47pub mod classification;
48pub mod convert_parts;
49mod convert_value_container;
50mod datex_hash;
51pub mod error;
52pub mod get_core_lib_type_id;
53pub mod get_datex_type;
54pub mod method_calls;
55#[cfg(feature = "ast")]
56mod to_datex_expression_data;
57mod to_instructions;
58
59#[derive(Debug, Eq, Clone)]
60pub enum ValueContainer {
61 Local(Value),
62 Shared(SharedContainer),
63}
64
65impl ValueContainer {
66 pub fn new_from_option(value: Option<ValueContainer>) -> ValueContainer {
69 match value {
70 Some(value) => value,
71 None => ValueContainer::Local(Value::null()),
72 }
73 }
74
75 pub fn local(value: impl Into<Value>) -> Self {
77 ValueContainer::Local(value.into())
78 }
79
80 pub fn owner(&self) -> Endpoint {
81 match self {
82 ValueContainer::Local(_value) => Endpoint::LOCAL,
83 ValueContainer::Shared(shared) => {
84 shared.pointer_address().endpoint()
85 }
86 }
87 }
88
89 pub fn value_container(&self) -> Sheep<'_, ValueContainer> {
91 match self {
92 ValueContainer::Local(_) => Sheep::Borrowed(self),
93 ValueContainer::Shared(shared) => {
94 Sheep::Ref(shared.value_container())
95 }
96 }
97 }
98
99 pub fn value_container_mut(&mut self) -> SheepMut<'_, ValueContainer> {
101 match self {
102 ValueContainer::Local(_) => SheepMut::Borrowed(self),
103 ValueContainer::Shared(shared) => {
104 SheepMut::Ref(shared.value_container_mut())
105 }
106 }
107 }
108
109 pub fn collapsed_value(&self) -> CollapsedContainerValue<'_> {
110 match self {
111 ValueContainer::Local(value) => {
112 CollapsedContainerValue::new_local(value)
113 }
114 ValueContainer::Shared(shared) => shared.collapsed_value(),
115 }
116 }
117
118 pub fn collapsed_value_mut(&mut self) -> CollapsedContainerValueMut<'_> {
119 match self {
120 ValueContainer::Local(value) => {
121 CollapsedContainerValueMut::new_local(value)
122 }
123 ValueContainer::Shared(shared) => shared.collapsed_value_mut(),
124 }
125 }
126
127 pub fn get_cloned_value(&self) -> Value {
131 let val = self.collapsed_value();
132 val.borrow().clone()
133 }
134
135 pub fn try_as<T>(&self) -> Option<&T>
139 where
140 T: ConvertValueContainer,
141 {
142 T::try_borrow_from_value_container(self).ok()
143 }
144
145 pub fn try_as_mut<T>(&mut self) -> Option<&mut T>
149 where
150 T: ConvertValueContainer,
151 {
152 T::try_borrow_mut_from_value_container(self).ok()
153 }
154
155 pub fn try_as_str(&self) -> Option<&str> {
159 match self {
160 ValueContainer::Local(value) => value.try_as_str(),
161 ValueContainer::Shared(_shared) => None,
162 }
163 }
164
165 pub fn try_into_value<T>(self) -> Result<T, ValueContainer>
169 where
170 T: ConvertValueContainer,
171 {
172 T::try_from_value_container(self)
173 }
174
175 pub fn try_convert_into_value<T>(self) -> Result<T, ()>
178 where
179 T: ConvertValueContainer + FromParts,
180 {
181 T::try_convert_from_value_container(self)
182 }
183
184 pub fn without_local_observers(self) -> ValueContainer {
187 match self {
188 ValueContainer::Local(value) => {
189 ValueContainer::Local(value.without_local_observers())
190 }
191 val => val,
192 }
193 }
194
195 pub fn get_cloned(&self) -> ValueContainer {
199 match self {
200 ValueContainer::Local(value) => {
201 ValueContainer::Local(value.clone())
202 }
203 ValueContainer::Shared(shared) => shared.value_container().clone(),
204 }
205 }
206
207 pub fn actual_type(&self) -> TypeDefinition {
209 match self {
210 ValueContainer::Local(local) => local.actual_type(),
211 ValueContainer::Shared(shared) => shared.actual_type(),
212 }
213 }
214
215 pub fn actual_container_type(&self) -> TypeDefinitionWithMetadata {
217 match self {
218 ValueContainer::Local(value) => TypeDefinitionWithMetadata::new(
219 value.actual_type(),
220 TypeMetadata::default(),
221 ),
222 ValueContainer::Shared(shared) => {
223 let inner_type =
224 shared.value_container().actual_container_type();
225 TypeDefinitionWithMetadata::new(
226 TypeDefinition::Box(Box::new(Type::from(inner_type))),
227 TypeMetadata::Shared {
228 mutability: shared.container_mutability(),
229 ownership: shared.ownership(),
230 },
231 )
232 }
233 }
234 }
235
236 pub fn allowed_or_actual_type(&self) -> Sheep<'_, TypeDefinition> {
239 match self {
240 ValueContainer::Local(value) => Sheep::Owned(value.actual_type()),
241 ValueContainer::Shared(shared) => Sheep::Ref(shared.allowed_type()),
242 }
243 }
244
245 pub fn maybe_shared(&self) -> Option<&SharedContainer> {
247 if let ValueContainer::Shared(shared) = self {
248 Some(shared)
249 } else {
250 None
251 }
252 }
253
254 pub fn maybe_shared_mut(&mut self) -> Option<&mut SharedContainer> {
256 if let ValueContainer::Shared(shared) = self {
257 Some(shared)
258 } else {
259 None
260 }
261 }
262
263 pub fn with_maybe_shared<F, R>(&self, f: F) -> Option<R>
265 where
266 F: FnOnce(&SharedContainer) -> R,
267 {
268 if let ValueContainer::Shared(shared) = self {
269 Some(f(shared))
270 } else {
271 None
272 }
273 }
274
275 pub fn shared_unchecked(&self) -> &SharedContainer {
277 match self {
278 ValueContainer::Shared(shared) => shared,
279 _ => {
280 panic!("Cannot convert ValueContainer to SharedContainer")
281 }
282 }
283 }
284 pub fn shared_unchecked_mut(&mut self) -> &mut SharedContainer {
285 match self {
286 ValueContainer::Shared(shared) => shared,
287 _ => {
288 panic!("Cannot convert ValueContainer to SharedContainer")
289 }
290 }
291 }
292
293 pub fn is_uninitialized(&self) -> bool {
295 match self {
296 ValueContainer::Local(value) => value.is_uninitialized(),
297 ValueContainer::Shared(shared) => shared.is_uninitialized(),
298 }
299 }
300
301 pub fn is_null(&self) -> bool {
302 match self {
303 ValueContainer::Local(value) => value.is_null(),
304 ValueContainer::Shared(shared) => {
305 shared.value_container().is_null()
306 }
307 }
308 }
309}
310
311impl<T: Into<Value>> From<T> for ValueContainer {
312 fn from(value: T) -> Self {
313 ValueContainer::Local(value.into())
314 }
315}
316
317impl From<SharedContainer> for ValueContainer {
318 fn from(shared: SharedContainer) -> Self {
319 ValueContainer::Shared(shared)
320 }
321}
322
323impl From<OwnedSharedContainer> for ValueContainer {
324 fn from(shared: OwnedSharedContainer) -> Self {
325 ValueContainer::Shared(shared.into())
326 }
327}
328
329impl From<ReferencedSharedContainer> for ValueContainer {
330 fn from(shared: ReferencedSharedContainer) -> Self {
331 ValueContainer::Shared(shared.into())
332 }
333}
334
335impl Hash for ValueContainer {
336 fn hash<H: Hasher>(&self, state: &mut H) {
337 match self {
338 ValueContainer::Local(value) => value.hash(state),
339 ValueContainer::Shared(pointer) => pointer.hash(state),
340 }
341 }
342}
343
344impl_display_for_datex_value!(
345 ValueContainer,
346 impl core::fmt::Display for ValueContainer {
347 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
348 match self {
349 ValueContainer::Local(value) => core::write!(f, "{value}"),
350 ValueContainer::Shared(shared) => {
352 if shared.is_borrowed() {
353 write!(f, "{}", shared.to_string_omit_content())
354 } else {
355 let value = shared.collapsed_value();
356 write!(f, "shared ({})", value.borrow().as_ref())
357 }
358 }
359 }
360 }
361 }
362);